<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch">
        <el-card shadow="hover" class="mb-[10px]">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="90px">
            <el-form-item label="编号" prop="gid">
              <el-input v-model="queryParams.gid" 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 label="请假时间" prop="leaveDate" style="width: 330px">
              <el-date-picker
                clearable
                v-model="queryParams.leaveDate as Array<string>"
                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">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain @click="LeaveOpen">请假</el-button>
              </el-col>
              <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>{{ 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)" v-hasPermi="['project:summarize: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>
        <Leave v-model="visible" @update="getList" />
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { getLeaveList } from '@/api/leave/log';
import { LeaveLogVO, LeaveLogQuery } from '@/api/leave/log/types';
import Leave from './leave.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const type = computed(() => proxy?.$route.query.type as string);

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

const route = useRoute();
const gid = computed(() => route.query.gid || '');

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: 'leaveReason'
  },
  {
    label: '请假类型',
    prop: 'leaveType'
  },
  {
    label: '请假时间',
    prop: 'createTime'
  },
  {
    label: '审批结果',
    prop: 'status'
  }
]);

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

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

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await getLeaveList({
    startDate: queryParams.value.leaveDate?.[0] || '',
    endDate: queryParams.value.leaveDate?.[1] || '',
    gid: queryParams.value.gid,
    status: queryParams.value.status,
    ...page.value
  });
  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 handleShow = (row: LeaveLogVO) => {
  proxy.$router.push({
    path: '/leave/detail',
    query: {
      id: row.gid
    }
  });
};
const visible = ref(false);
const LeaveOpen = () => {
  visible.value = true;
};
onActivated(() => {
  getList();
  if (type.value == 'leave') {
    LeaveOpen();
  }
});
onMounted(() => {
  getList();
  if (type.value == 'leave') {
    LeaveOpen();
  }
});
</script>
