import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { selectAllSysAreaList, selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';

// 岗位列表接口
import { requestClient } from '#/api/request';

// 获取岗位列表
const getPostList = async () => {
  const resp = await requestClient.get('/system/post/list');
  return resp || [];
};

// 获取证件名称列表
const getLicenseNameList = async () => {
  const resp = await requestClient.get('/sysLicenseRule/rule/list');
  return resp || [];
};

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    fieldName: 'areaId',
    label: '片区',
    componentProps: {
      placeholder: '请选择片区',
      clearable: true,
      api: async () => {
        const resp = await selectAllSysAreaList();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.areaName,
              value: item.id,
            }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '油站',
    componentProps: {
      placeholder: '请选择油站',
      clearable: true,
      api: async () => {
        const resp = await selectAllSysStation();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.stationName,
              value: item.id.toString(),
            }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'licenseRuleId',
    label: '证件名称',
    componentProps: {
      placeholder: '请选择证件名称',
      clearable: true,
      api: async () => {
        const resp = await getLicenseNameList();
        const data = resp?.rows || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.licenseName,
              value: item.id,
            }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
    },
    fieldName: 'surveyStartTime',
    label: '处理时间',
  },
  {
    component: 'Select',
    fieldName: 'completionStatus',
    label: '状态',
    componentProps: {
      placeholder: '请选择完成状态',
      options: [
        { label: '按时完成', value: 2 },
        { label: '逾期完成', value: 1 },
      ],
    },
  },

  {
    component: 'Input',
    fieldName: 'executorName',
    label: '执行人',
    componentProps: {
      placeholder: '请输入执行人',
    },
  },

];


export const columns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
  {
    field: 'taskName',
    title: '任务名称',
    minWidth: 150,
  },
  {
    field: 'stationId',
    title: '油站',
    minWidth: 120,
  },
  {
    field: 'licenseName',
    title: '证件名称',
    minWidth: 120,
  },
  {
    field: 'taskType',
    title: '任务类型',
    minWidth: 120,
  },
  {
    field: 'executorName',
    title: '执行人',
    minWidth: 120,
  },
  {
    field: 'position',
    title: '岗位',
    minWidth: 120,
  },
  {
    field: 'isComment',
    title: '评论',
    minWidth: 100,
    align: 'center',
    formatter: (cellValue: number) => {
      return cellValue === 1 ? '已评' : '未评';
    },
  },
  {
    field: 'deadline',
    title: '截止时间',
    minWidth: 180,
  },
  {
    field: 'processTime',
    title: '处理时间',
    minWidth: 180,
  },
  {
    field: 'completionStatus',
    title: '完成状态',
    minWidth: 120,
    align: 'center',
    formatter: ({ row }) => {
      return row.completionStatus === 2 ? '按时完成' : '逾期完成';
    },
  },
  {
    field: 'createType',
    title: '创建',
    minWidth: 100,
    align: 'center',
    formatter: (cellValue: number) => {
      return cellValue === 0 ? '系统' : '新增';
    },
  },
];


