<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="leaveDate" style="width: 330px">
              <el-date-picker
                clearable
                v-model="queryParams.leaveDate as Array<string>"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="申请人" prop="createBy">
              <el-input v-model="queryParams.createBy" placeholder="请输入申请人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="审批结果" prop="status">
              <el-select v-model="queryParams.status" clearable>
                <el-option label="/" value="0" />
                <el-option v-for="(label, value) in approvalResultsOptions" :key="value" :label="label" :value="value" />
              </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="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 }">

            <template v-if="prop === 'status'">
              <dict-tag :options="approval_status" :value="row[prop]" />
            </template>
            <template v-else>
              <span>{{ row[prop] || '-' }}</span>
            </template>
          </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" v-if="row.status != '待审批'" @click="handleAction(row, false)">详情</el-button>
            <el-button link v-else type="primary" @click="handleAction(row, true)">审批</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/leave/approval';
import { LeaveApprovalVO, LeaveApprovalQuery } from '@/api/leave/approval/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { approval_status } = toRefs<any>(proxy?.useDict('approval_status'));
const extractLogList = ref<LeaveApprovalVO[]>([]);
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: 'gid'
  },
  {
    label: '假期开始时间',
    prop: 'startDate'
  },
  {
    label: '假期截止时间',
    prop: 'endDate'
  },
  {
    label: '申请人',
    prop: 'createBy'
  },
  {
    label: '11请假事由',
    prop: 'leaveReason'
  },
  {
    label: '请假时间',
    prop: 'createTime'
  },
  {
    label: '审批结果',
    prop: 'status'
  }
]);

const approvalResultsOptions = {
  1: '待审批',
  2: '通过',
  3: '审批驳回'
};
const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<LeaveApprovalQuery>({
  createBy: '',
  status: '',
  leaveDate: []
});

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  let params:any = { ...queryParams.value, ...page.value };
  params.startDate = params.leaveDate?.[0] || '';
  params.endDate = params.leaveDate?.[1] || '';
  delete params.leaveDate;

  const res = await getTableList(params);
  extractLogList.value = res.rows;
  page.value.total = res.total;
  loading.value = false;
};

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

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

const handleAction = (row: LeaveApprovalVO, isApproval: boolean) => {
  let query = {
    id: row.gid,
    type: isApproval ? 'approval' : 'approvalShow'
  };
  proxy.$router.push({
    path: '/leave/detail',
    query
  });
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
