<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 HH:mm:ss"
                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>
              <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 === 'leaveType'">
              <dict-tag :options="qjlx" :value="row[prop]" />
            </template>
            <template v-else-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" @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 { getListData } from '@/api/leave/list';
import { LeaveListVO, LeaveListQuery } from '@/api/leave/list/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { qjlx } = toRefs<any>(proxy?.useDict('qjlx'));
const { approval_status } = toRefs<any>(proxy?.useDict('approval_status'));
const extractLogList = ref<LeaveListVO[]>([]);
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: 'createBy'
  },
  {
    label: '请假事由',
    prop: 'leaveReason'
  },
  {
    label: '请假类型',
    prop: 'leaveType'
  },
  {
    label: '请假时间',
    prop: 'createTime'
  },
  {
    label: '审批结果',
    prop: 'status'
  }
]);

const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<LeaveListQuery>({
  gid: '',
  createBy: '',
  leaveDate: []
});

watchEffect(() => {
  queryParams.value.gid = gid.value as string;
});
/** 获取字典对应label */
/*const getDictLabel = (dict: Array<{ dictLabel: string; dictValue: string }>, value: string | number) => {
  console.log(dict);
  console.log(value);
  if (!dict || dict.length === 0) return value;
  const item = dict.find(d => d.value === String(value));
  console.log(item);
  return item ? item.label : value;
};*/


/** 查询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 getListData(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 handleShow = (row: LeaveListVO) => {
  proxy.$router.push({
    path: '/leave/detail',
    query: {
      id: row.gid,
      type: 'superadmin'
    }
  });
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
