import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { fun_UpdateModuleState } from '@/api/demo/formModule';
import { useMessage } from '@/hooks/web/useMessage';
import { getMenuList } from '@/api/demo/system';

type CheckedType = boolean | string | number;

export const formModuleColumns: BasicColumn[] = [
  {
    title: '编码',
    dataIndex: 'code',
    width: 160,
  },
  {
    title: '名称',
    dataIndex: 'name',
    width: 160,
  },
  {
    title: '上级功能',
    dataIndex: 'pmoduleId',
    width: 160,
  },
  {
    title: '状态',
    dataIndex: 'enabledMark',
    width: 80,
    customRender: ({ record }) => {
      return h(Switch, {
        checked: record.enabledMark === 1,
        checkedChildren: '启用',
        unCheckedChildren: '停用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          fun_UpdateModuleState({ id: record.id, state: newStatus })
            .then(() => {
              record.enabledMark = newStatus;
              createMessage.success(`状态更新成功`);
            })
            .catch(() => {
              createMessage.error('状态更新失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建人',
    dataIndex: 'createUserName',
    width: 120,
  },
  {
    title: '创建时间',
    dataIndex: 'createDate',
    width: 160,
  },
  {
    title: 'id',
    dataIndex: 'id',
    ifShow: false,
  },
  {
    title: 'code',
    dataIndex: 'code',
    ifShow: false,
  },
  {
    title: 'formCode',
    dataIndex: 'formCode',
    ifShow: false,
  },
  {
    title: 'formVerison',
    dataIndex: 'formVerison',
    ifShow: false,
  },
  {
    title: 'icon',
    dataIndex: 'icon',
    ifShow: false,
  },
  {
    title: 'type',
    dataIndex: 'type',
    ifShow: false,
  },
  {
    title: 'moduleId',
    dataIndex: 'moduleId',
    ifShow: false,
  },
  {
    title: 'sortCode',
    dataIndex: 'sortCode',
    ifShow: false,
  },
  {
    title: 'description',
    dataIndex: 'description',
    ifShow: false,
  },
  {
    title: 'createUserId',
    dataIndex: 'createUserId',
    ifShow: false,
  },
  {
    title: 'modifyDate',
    dataIndex: 'modifyDate',
    ifShow: false,
  },
  {
    title: 'modifyUserId',
    dataIndex: 'modifyUserId',
    ifShow: false,
  },
  {
    title: 'modifyUserName',
    dataIndex: 'modifyUserName',
    ifShow: false,
  },
  {
    title: 'scheme',
    dataIndex: 'scheme',
    ifShow: false,
  },
  {
    title: 'keyWord',
    dataIndex: 'keyWord',
    ifShow: false,
  },
  {
    title: 'tenantId',
    dataIndex: 'tenantId',
    ifShow: false,
  },
  {
    title: 'isMain',
    dataIndex: 'isMain',
    ifShow: false,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '关键字',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'code',
    component: 'Input',
    label: '编号',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    required: true,
    componentProps: {
      placeholder: '请输入',
    },
  },
  {
    field: 'name',
    component: 'Input',
    label: '名称',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    required: true,
    componentProps: {
      placeholder: '请输入',
    },
  },
  {
    field: 'icon',
    component: 'IconPicker',
    label: '图标',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    required: true,
    componentProps: {
      placeholder: '请输入',
    },
  },
  {
    field: 'formCode',
    component: 'Input',
    label: '表单选择',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    required: true,
    componentProps: {
      placeholder: '请输入',
    },
  },
  {
    field: 'formVerison',
    component: 'ApiSelect',
    label: '表单版本',
    required: true,
    colProps: {
      span: 24,
    },
  },
  {
    field: 'pmoduleId',
    component: 'ApiTreeSelect',
    label: '上级',
    colProps: {
      span: 24,
    },
    componentProps: () => {
      return {
        api: getMenuList,
        // 接口返回数据
        resultField: 'result',
        labelField: 'name',
        valueField: 'id',
      };
    },
  },
  {
    field: 'enabledMark',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
  },
  {
    field: 'sortCode',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 0,
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'description',
    component: 'InputTextArea',
    label: '描述',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    componentProps: {
      placeholder: '请输入',
    },
  },
];

export const searchFormSchema_Modal: FormSchema[] = [
  {
    field: 'keyword',
    label: '关键字',
    component: 'Input',
    colProps: { span: 12 },
  },
];

export const modalColumns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'name',
    width: 200,
  },
  {
    dataIndex: 'scheme',
    ifShow: false,
  },
  {
    title: '分类',
    dataIndex: 'category',
    width: 80,
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 80,
  },
];
