<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]">

          <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'"
              :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>{{ row[prop] || '-' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" min-width="120px" fixed="right" class-name="small-padding fixed-width">
              <template #default="{ row }">
                <el-button link type="primary" @click="handleShow(row)" v-hasPermi="['project:extract:show']">积分详情</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/expertPointManage';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

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

const route = useRoute();

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',
    minWidth: 130
  }
]);

const statusOptions = {
  0: '停用',
  1: '启用'
};
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) => {
    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) => {
  proxy?.$router.push({
    path: '/points/detail',
    query: { id: row.expertId, type: 'log' }
  });
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
