import { Switch } from 'ant-design-vue';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { useMessage } from '/@/hooks/web/useMessage';
import { setDictItemStatus } from '/@/api/system/dict';
import { h } from 'vue';

export const columns: BasicColumn[] = [
  {
    title: '文本值',
    dataIndex: 'itemText',
    width: 160,
  },
  {
    title: '字典值',
    dataIndex: 'itemValue',
    width: 180,
  },
  {
    title: '描述',
    dataIndex: 'description',
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange (checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          const data ={
            id:  record.id,
            status:  newStatus, 
           
          }
          setDictItemStatus( paramsToFormData(data))
            .then(() => {
              record.status = newStatus;
              createMessage.success(`修改成功`);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },

  {
    title: '排序',
    dataIndex: 'sort',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
];
  function  paramsToFormData (obj) {
  const data = preProcessData(obj);
  const formData = new FormData();
  Object.keys(data).forEach((key) => {
    if (data[key] instanceof Array) {
      data[key].forEach((item) => {
        formData.append(key, item);
      });
      return;
    }
    formData.append(key, obj[key]);
  });
  return formData;
}
 function preProcessData (data) {
  Object.keys(data).forEach((item) => {
    if (typeof data[item] === 'undefined' || data[item] === null || data[item] === '') {
      delete data[item];
    }
  });
  return data;
}
export const searchFormSchema: FormSchema[] = [
  {
    field: 'itemText',
    label: '文本值',
    component: 'Input',
    colProps: { span: 6 },
    componentProps: {
      maxLength: 32,
      placeholder: '请输入文本值',
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 32) {
              return Promise.reject('字数不超过32个字');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    field: 'dictId',
    label: '文本值',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
      placeholder: '请输入文本值',
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 36) {
              return Promise.reject('字数不超过36个字');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'itemText',
    label: '文本值',
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 32,
      placeholder: '请输入文本值',
    },
  },
  {
    field: 'itemValue',
    label: '字典值',
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入字典值',
    },
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 1,
    componentProps: {
      maxLength: 32,
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '禁用', value: 0 },
        { label: '启用', value: 1 },
      ],
    },
  },
  {
    label: '备注',
    field: 'description',
    component: 'InputTextArea',
    componentProps: {
      rows: 4,
      allowClear: false,
      maxLength: 255,
    },
  },
  {
    label: '图片上传',
    field: 'upload',
    component: 'Input',
    slot: 'upload',
  },
];
