import type { CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import { dict } from '@fast-crud/fast-crud';
import dayjs from 'dayjs';
import { queryDepartment, addDepartment, editDepartment, delDepartment } from './api';
export default function createCrudOptions(): CreateCrudOptionsRet {
  return {
    crudOptions: {
      container: {
        is: 'fs-layout-card'
      },
      request: {
        pageRequest: async ({ page, query }) => {
          const { total, data } = await queryDepartment(page.offset + 1, page.limit, query);
          return { records: data, total, currentPage: page.offset, pageSize: page.limit };
        },
        addRequest: ({ form }) => {
          return addDepartment(form);
        },
        editRequest: ({ form }) => {
          editDepartment(form);
        },
        delRequest: ({ row }) => {
          return delDepartment(row.id);
        }
      },
      columns: {
        id: {
          title: 'ID',
          key: 'id',
          type: 'text',
          search: { show: true },
          column: {
            width: 50,
            align: 'center'
          },
          form: {
            show: false
          }
        },
        depname: {
          title: '部门名称',
          search: { show: true },
          key: 'depname',
          type: 'text',
          column: {
            width: 150,
            align: 'center'
          },
          form: {
            show: true
          }
        },
        address: {
          title: '部门地址',
          key: 'address',
          type: 'text',
          search: { show: true },
          column: {
            align: 'center'
          }
        },
        createTime: {
          key: 'createTime',
          title: '创建时间',
          type: 'datetime',
          column: {
            width: 250,
            align: 'center'
          },
          search: { show: true },
          valueBuilder(context) {
            const { value, row, key } = context;
            if (value) {
              row[key] = dayjs(value).valueOf();
            }
          },
          valueResolve(context) {
            const { value, form, key } = context;
            if (value) {
              form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
            }
          }
        },
        modifyTime: {
          title: '修改时间',
          key: 'modifyTime',
          type: 'datetime',
          align: 'center',
          column: {
            width: 250,
            align: 'center'
          },
          valueBuilder(context) {
            const { value, row, key } = context;
            if (value) {
              row[key] = dayjs(value).valueOf();
            }
          },
          valueResolve(context) {
            const { value, form, key } = context;
            if (value) {
              form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
            }
          }
        },
        disabled: {
          key: 'disabled',
          title: '状态',
          type: 'dict-select',
          dict: dict({
            data: [
              { value: 'N', label: '启用' },
              { value: 'Y', label: '禁用' }
            ]
          }),
          search: { show: true },
          column: {
            width: 90
          }
        }
      }
    }
  };
}
