<template>
  <div class="p-6 bg-gray-50 condition">
    <transition>
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="never">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="professionalList">
        <el-table-column label="序号" align="center" type="index" width="100" />
        <el-table-column label="提资人" align="center" prop="userName" />
        <el-table-column label="专业" align="center" prop="userMajorName" />
        <el-table-column label="电话" align="center" prop="phone" />
        <el-table-column label="邮箱" align="center" prop="email" />
        <el-table-column label="流程状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="wf_business_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5" v-if="scope.row.status === 'draft' || scope.row.status === 'cancel' || scope.row.status === 'back'">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">审核</el-button>
              </el-col>
              <el-col :span="1.5" v-if="scope.row.status === 'finish'">
                <el-button link type="primary" icon="Download" @click="handleDownload(scope.row)">导出</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button link type="warning" icon="View" v-if="scope.row.status != 'draft'" @click="handleViewInfo(scope.row)">查看流程</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button link type="warning" icon="View" v-if="scope.row.status != 'draft'" @click="handleFile(scope.row)">查看文件</el-button>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <el-dialog title="提取文件列表" v-model="viewVisible" width="500px">
      <el-table :loading="loadingFlie" :data="fileList" style="width: 100%" border>
        <el-table-column prop="fileName" label="文件名称" align="center">
          <template #default="scope">
            <el-link
              :key="scope.row.fileId"
              :href="scope.row.fileUrl"
              target="_blank"
              :type="scope.row.status == '1' ? 'primary' : 'info'"
              :underline="false"
            >
              {{ scope.row.fileName }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="是否变更" align="center" width="120">
          <template #default="scope">
            <el-tag :type="scope.row.type == 1 ? 'success' : 'info'">{{ scope.row.type == 1 ? '否' : '是' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="120" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'info'">{{ scope.row.status == 1 ? '使用中' : '已作废' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="170" align="center">
          <template #default="scope">
            <el-button type="success" link icon="View" @click="handleViewFile(scope.row)"> 查看 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <span>
          <el-button type="primary" @click="viewVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DataCollectionForm" lang="ts">
import { ref, reactive, computed, onMounted } from 'vue';
import { useUserStoreHook } from '@/store/modules/user';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { wf_business_status } = toRefs<any>(proxy?.useDict('wf_business_status'));
import { extractList, getFileList } from '@/api/design/Professional';

// 获取用户 store
const userStore = useUserStoreHook();
const total = ref(0);
// 从 store 中获取当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
const professionalList = ref([]);
const showSearch = ref(true);
const loading = ref(false);
const loadingFlie = ref(false);
const viewVisible = ref(false); //文件列表展示
const fileList = ref([]);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: currentProject.value?.id,
    phone: undefined,
    status: undefined,
    params: {}
  }
});
const queryFormRef = ref<ElFormInstance>();
const { queryParams } = toRefs(data);
// 查询提资清单表
const getList = async () => {
  let res = await extractList({ ...queryParams.value });
  if (res.code == 200) {
    professionalList.value = res.rows;
    total.value = res.total; //默认第一个
  }
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 新增按钮操作 */
const handleAdd = (row) => {
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/design-management/Professional/indexEdit`,
    query: {
      id: row.id,
      type: 'add'
    }
  });
};
const handleViewInfo = (row) => {
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/design-management/Professional/indexEdit`,
    query: {
      id: row.id,
      type: 'view'
    }
  });
};
const handleUpdate = (row) => {
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/design-management/Professional/indexEdit`,
    query: {
      id: row.id,
      type: 'update'
    }
  });
};
const handleDownload = (row) => {
  // 导出接口
  proxy?.download(
    'design/extract/exportWord',
    {
      id: row.id
    },
    `互提资料.zip`
  );
};
const handleViewFile = (row) => {
  window.open(row.docUrl, '_blank');
};
const handleFile = async (row) => {
  // 查看文件
  viewVisible.value = true;
  loadingFlie.value = true;
  let res = await getFileList(row.id);
  if (res.code == 200) {
    fileList.value = res.data;
  }
  loadingFlie.value = false;
};
// 页面挂载时初始化数据
onMounted(() => {
  getList();
});
</script>

<style lang="scss">
.condition {
  .el-tabs__header {
    height: 84vh !important;
  }
}
</style>
