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

export const modelTypeDict: FormSchema[] = [
  {
    label: '字典名称',
    field: 'dictName',
    component: 'NInput',
    required: true
  },
  {
    label: '字典类型',
    field: 'dictType',
    component: 'NInput',
    required: true
  },
  {
    label: '是否启用',
    field: 'status',
    component: 'NSwitch',
    defaultValue: 0,
    componentProps: {
      checkedValue: 0,
      uncheckedValue: 1
    }
  },
  {
    label: '备注',
    field: 'remark',
    component: 'NInput',
    componentProps: {
      type: 'textarea'
    }
  },
  {
    label: 'dictId',
    field: 'dictId',
    component: 'NInput',
    show: false
  }
];
const listClassOptions: Record<string, string>[] = [
  { label: 'Text', value: 'text' },
  { label: 'Default', value: 'default' },
  { label: 'Tertiary', value: 'tertiary' },
  { label: 'Primary', value: 'primary' },
  { label: 'Info', value: 'info' },
  { label: 'Success', value: 'success' },
  { label: 'Warning', value: 'warning' },
  { label: 'Error', value: 'error' }
];

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

  {
    key: 'dictValue',
    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: 1,
        checkedValue: 0,
        onUpdateValue: async e => {
          const oldStatus = rowData.status;
          rowData.status = e;
          const { response } = await fetchUpdateDictData(rowData);
          if (response.data.code != '200') {
            rowData.status = oldStatus;
          }
        }
      });
    }
  },
  {
    key: 'dictSort',
    title: '字典排序',
    align: 'center',
    minWidth: 80,
    resizable: true,
    ellipsis: {
      tooltip: true
    }
  },
  {
    key: 'remark',
    title: '备注',
    align: 'center',
    minWidth: 80,
    resizable: true,
    ellipsis: {
      tooltip: true
    }
  },
  {
    key: 'createTime',
    title: '创建时间',
    align: 'center',
    minWidth: 80,
    resizable: true,
    ellipsis: {
      tooltip: true
    }
  }
];
function renderTagLabel(option: { label: string; value: string }) {
  if (option.value === 'text') {
    return option.label;
  }
  return h(NTag, { size: 'small', type: option.value as any }, () => option.label);
}
