<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="专家姓名" prop="evaluateUser">
              <el-input v-model="queryParams.evaluateUser" placeholder="请输入专家姓名"></el-input>
            </el-form-item>
            <el-form-item label="共同参与项目" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入共同参与项目"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="全部">
                <el-option v-for="(label, value) in statusOptions" :key="value" :label="label" :value="value"> </el-option>
              </el-select>
            </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">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="mutualList">
        <el-table-column
          :label="label"
          :align="align || 'center'"
          prop="evaluateUser"
          :min-width="minWidth || '100px'"
          v-for="{ label, prop, align, minWidth } in headers"
          :key="prop"
        >
          <template #default="{ row }">
            <span v-if="prop == 'itemStage'">{{ getStage(+row.itemStage) || '-' }}</span>
            <span v-else>{{ row[prop] || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" min-width="150px">
          <template #default="scope">
            <el-button v-if="scope.row.status == '未评价'" link type="primary" @click="handleEvaluate(scope.row)"> 评价 </el-button>
            <el-button v-else link type="primary" @click="handleShow(scope.row)">详情</el-button>
          </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" />
      <evaluateDialog v-model="showEvaluateDialog" :row="activeRow" type="mutual" @updatelist="getList" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { listMutual } from '@/api/evaluate/mutual';
import { MutualVO } from '@/api/evaluate/mutual/types';
import evaluateDialog from '../evaluateDialog/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { item_stage } = toRefs<any>(proxy?.useDict('item_stage'));

const mutualList = ref<MutualVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();

const queryFormRef = ref<ElFormInstance>();

const statusOptions = {
  0: '未评价',
  1: '已评价'
};

const stageOptions = computed(() => item_stage.value);
const getStage = (id:any) => {
  let item = stageOptions.value.find((item:any) => item.value == id);
  return item?.label || '';
};

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}
const headers = ref<Header[]>([
  {
    label: '专家姓名',
    prop: 'evaluateUser'
  },
  {
    label: '共同参与项目',
    prop: 'productName'
  },
  {
    label: '工作单位',
    prop: 'workUnit'
  },
  {
    label: '项目阶段',
    prop: 'itemStage'
  },
  {
    label: '职务',
    prop: 'expertPost'
  },
  {
    label: '评价时间',
    prop: 'createTime'
  },
  {
    label: '状态',
    prop: 'status'
  }
]);

const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  evaluateUser: '',
  productName: '',
  status: '',
});

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMutual(queryParams.value);
  mutualList.value = res.rows || [];
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 查看按钮操作 */
const handleShow = (row?: MutualVO) => {
  const _id = row?.id;
  router.push({
    path: '/project/evaluate/detail',
    query: { id: _id, from: 'mutual' }
  });
};
const showEvaluateDialog = ref(false);
const activeRow = ref(null);
const handleEvaluate = (row?: MutualVO) => {
  showEvaluateDialog.value = true;
  activeRow.value = row;
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
<style scoped>
.disUoloadSty :deep(.el-upload--picture-card) {
  display: none; /* 上传按钮隐藏 */
}
</style>
