import { h } from 'vue';
import { NSwitch } from 'naive-ui';
import { fetchUpdateDictData } from '@/service/api/system-manage';
import type { FormSchema } from '@/components/zt/Form/types/form';
import DictTag from '@/components/zt/dict-tag/index.vue';

export const modelTypeDict: FormSchema[] = [
  {
    label: '字典名称',
    field: 'name',
    component: 'NInput',
    required: true
  },
  {
    label: '字典类型',
    field: 'code',
    component: 'NInput',
    required: true
  },
  {
    label: '是否启用',
    field: 'status',
    component: 'NSwitch',
    defaultValue: 1,
    componentProps: {
      checkedValue: 1,
      uncheckedValue: 0
    }
  },
  {
    label: '备注',
    field: 'remark',
    component: 'NInput',
    componentProps: {
      type: 'textarea'
    }
  },
  {
    label: 'dictId',
    field: 'id',
    component: 'NInput',
    show: false
  }
];

export const modelDataDict: FormSchema[] = [
  {
    label: '字典类型',
    field: 'id',
    component: 'NInput',
    show: false
  },
  {
    label: '字典类型',
    field: 'typeCode',
    component: 'NInput',
    componentProps: {
      disabled: true
    }
  },
  {
    label: '字典标签',
    field: 'name',
    component: 'NInput',
    required: true
  },
  {
    label: '字典键值',
    field: 'value',
    component: 'NInput',
    required: true
  },
  {
    label: '是否启用',
    field: 'status',
    component: 'NSwitch',
    defaultValue: 1,
    componentProps: {
      checkedValue: 1,
      uncheckedValue: 0
    }
  },
  {
    label: '字典排序',
    field: 'dictSort',
    component: 'NInputNumber',
    defaultValue: 1,
    componentProps: {
      min: 1
    }
  },
  {
    label: '备注',
    field: 'remark',
    component: 'NInput',
    componentProps: {
      type: 'textarea'
    }
  }
];
export const columns: NaiveUI.TableColumn<Api.System.DictData>[] = [
  {
    key: 'name',
    title: '字典标签',
    align: 'center',
    minWidth: 80,
    resizable: true,
    ellipsis: {
      tooltip: true
    },
    render(row) {
      return h(DictTag, { dictData: row });
    }
  },

  {
    key: 'value',
    title: '字典键值',
    align: 'center',
    minWidth: 80,
    resizable: true,
    ellipsis: {
      tooltip: true
    }
  },
  {
    title: '字典状态',
    key: 'status',
    align: 'center',
    render(rowData) {
      return h(NSwitch, {
        value: Number(rowData.status),
        uncheckedValue: 0,
        checkedValue: 1,
        onUpdateValue: async e => {
          const oldStatus = rowData.status;
          rowData.status = e;
          const { response } = await fetchUpdateDictData(rowData);
          if (response.data.code != '200') {
            rowData.status = oldStatus;
          }
        }
      });
    }
  },
  {
    key: 'remark',
    title: '备注',
    align: 'center',
    minWidth: 80,
    resizable: true,
    ellipsis: {
      tooltip: true
    }
  },
  {
    key: 'createTime',
    title: '创建时间',
    align: 'center',
    minWidth: 80,
    resizable: true,
    ellipsis: {
      tooltip: true
    }
  }
];
