<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="expertName">
              <el-input v-model="queryParams.expertName" placeholder="请输入专家姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工作单位" prop="workUnit">
              <el-input v-model="queryParams.workUnit" placeholder="请输入工作单位" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="入库时间" prop="storageTime" style="width: 330px">
              <el-date-picker
                clearable
                v-model="queryParams.storageTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </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>

        <el-card shadow="never" class="mt-[10px] custom-card">
          <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="extractLogList">
            <el-table-column label="序号" width="50px" type="index"></el-table-column>
            <el-table-column
              :label="label"
              :align="align || 'center'"
              prop="expertName"
              :min-width="minWidth || '100px'"
              v-for="{ label, prop, align, minWidth } in headers"
              :key="prop"
            >
              <template #default="{ row }">
                <span v-if="prop == 'points'">{{ +row.points }}分</span>
                <span v-else-if="prop == 'checkStatus'">{{ statusOptions[row.checkStatus] }}</span>
                <span v-else>{{ row[prop] || '-' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="120px" fixed="right" class-name="small-padding fixed-width">
              <template #default="{ row }">
                <el-button link type="primary" v-if="row.checkStatus == 3" @click="handleShow(row, 'review')">积分复核</el-button>
                <el-button link type="primary" v-else-if="row.checkStatus != 3" @click="handleShow(row, 'reviewshow')">积分详情</el-button>
                <el-button link type="success" v-if="row.checkStatus == 1" @click="Thaw(row)">解冻</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="page.total > 0" :total="page.total" v-model:page="page.pageNum" v-model:limit="page.pageSize" @pagination="getList" />
        </el-card>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { getTableList } from '@/api/points/review';
import { ReviewPoints } from '@/api/points/detail';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const extractLogList = ref<any[]>([]);
const loading = ref(true);
const showSearch = ref(true);

const queryFormRef = ref<ElFormInstance>();

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}
const headers = ref<Header[]>([
  {
    label: '专家姓名',
    prop: 'expertName'
  },
  {
    label: '工作单位',
    prop: 'workUnit'
  },
  {
    label: '职务',
    prop: 'expertPost'
  },
  {
    label: '分值',
    prop: 'points'
  },
  {
    label: '专家排名',
    prop: 'rank'
  },
  {
    label: '入库时间',
    prop: 'inTime'
  },
  {
    label: '复核状态',
    prop: 'checkStatus'
  }
]);

const statusOptions = {
  1: '冻结',
  2: '认可',
  3: '待复核'
};
const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  expertName: '',
  workUnit: '',
  storageTime: []
});

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  let startDate = queryParams.value.storageTime[0] || '';
  let endDate = queryParams.value.storageTime[1] || '';
  const res = await getTableList({ ...queryParams.value, ...page.value, startDate, endDate });
  extractLogList.value = res.rows.map((e:any) => {
    return {
      ...e,
      inTime: e.inTime?.split('.')[0] || ''
    };
  });
  page.value.total = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const handleShow = async (row: any, type:any) => {
  proxy?.$router.push({
    path: '/points/detail',
    query: { id: row.expertId, type }
  });
};
const Thaw = (row:any) => {
  ElMessageBox.confirm(`是否确定解冻该专家积分`, '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      let params = {
        checkStatus: 2,
        expertId: row.expertId
      };
      let res = await ReviewPoints(params);
      if (res.code != 200) {
        ElNotification({
          title: '提示',
          message: res.msg,
          type: 'error'
        });
        return;
      }
      ElNotification({
        title: '提示',
        message: '专家积分解冻成功',
        type: 'success'
      });
      await getList();
    })
    .catch(() => {});
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.custom-card ::v-deep(.el-card__header) {
  display: flex !important;
  align-items: center !important;
  justify-content: space-between; /* 左右两端 */
}

.custom-card ::v-deep(.el-card__header)::before {
  content: "对积分排行榜前10名中，积分低于10分且持续时间不少于3个月的专家，进行定期审核，审核周期为每7天一次";
  color: #666;
  font-weight: 600;
  margin-right: 10px;
  white-space: nowrap;
  /* 不需要 display:inline-block，默认即可 */
}

</style>

