<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="90px">
            <el-form-item label="抽取流水号" prop="extractionSn">
              <el-input v-model="queryParams.extractionSn" placeholder="请输入抽取流水号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="项目编号" prop="itemCode">
              <el-input v-model="queryParams.itemCode" placeholder="请输入项目编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="项目名称" prop="itemName">
              <el-input v-model="queryParams.itemName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="抽取结果" prop="extractionStatus">
              <el-select v-model="queryParams.extractionStatus" clearable>
                <el-option v-for="(label, value) in extractResultsOptions" :key="value" :label="label" :value="value" />
              </el-select>
            </el-form-item>
            <el-form-item label="抽取时间" prop="extractDate" style="width: 330px">
              <el-date-picker
                clearable
                v-model="queryParams.extractDate"
                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>
      </div>
    </transition>

    <el-card shadow="never">
      <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 == 'extractionStatus'">
              {{ extractResultsOptions[row.extractionStatus] || '-' }}
            </span>
            <span v-else-if="prop == 'reviewSn'">
              {{ stageObj[row.reviewSn] || '-' }}
            </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">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleShow(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>
</template>

<script setup lang="ts">
import { getTableList } from '@/api/project/extractLog';
import { ExtractLogVO } from '@/api/project/extractLog/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { item_stage } = toRefs<any>(proxy?.useDict('item_stage'));
const stageObj = computed(() => {
  let obj = {};
  item_stage.value.forEach((e:any) => {
    obj[e.value] = e.label;
  });
  return obj;
});

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

const route = useRoute();
const itemId = computed(() => route.query.itemId);
const reviewSn = computed(() => route.query.reviewSn);

const queryFormRef = ref<ElFormInstance>();

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}
const headers = ref<Header[]>([
  {
    label: '抽取流水号',
    prop: 'extractionSn'
  },
  {
    label: '项目编号',
    prop: 'projectId'
  },
  {
    label: '项目名称',
    prop: 'projectName'
  },
  {
    label: '项目阶段',
    prop: 'reviewSn'
  },
  {
    label: '已抽取人数',
    prop: 'extractedSum'
  },
  {
    label: '抽取时间',
    prop: 'extractionTime'
  },
  {
    label: '抽取人',
    prop: 'extractionUser'
  },
  // {
  //   label: '抽取结果',
  //   prop: 'extractionStatus'
  // }
]);

const extractResultsOptions = {
  1: '部分抽取',
  2: '抽取完成',
  3: '抽取失败'
};
const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  extractionSn: '',
  itemCode: '',
  itemName: '',
  extractionStatus: '',
  extractDate: []
});

watchEffect(() => {
  queryParams.value.itemCode = itemId.value as string;
});

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const params = {
    ...queryParams.value,
    ...page.value
  };
  params.startExtractionTime = params.extractDate?.[0] || '';
  params.endExtractionTime = params.extractDate?.[1] || '';
  if (itemId.value && itemId.value == queryParams.value.itemCode) {
    params.reviewSn = reviewSn.value;
  }
  delete params.extractDate;
  delete params.total;
  const res: any = await getTableList(params);
  extractLogList.value = res.rows.map((e:any) => {
    return {
      ...e,
      projectId: e.zjkProductVo.productSn,
      projectName: e.zjkProductVo.productName,
      reviewSn: e.zjkReviewPhase?.reviewSn
    };
  });
  page.value.total = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  proxy?.$router.replace({
    path:'/project/extractLog'
  });
  handleQuery();
};
const handleShow = (row: any) => {
  let query: any = {
    type: 'logview'
  };
  if (row) {
    query.id = row.id;
  }
  proxy?.$router.push({
    path: '/project/extractExtract',
    query
  });
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
