import { BasicColumn, FormSchema, render } from '@jdlinker/ui';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { setMaster } from './service';

//列表数据
export const columns: BasicColumn[] = [
  {
    title: '配置名称',
    align: 'center',
    dataIndex: 'name'
  },
  {
    title: '存储器',
    align: 'center',
    dataIndex: 'storage',
    customRender: ({ text }) => {
      return render.renderDict(text, 'storage_type');
    }
  },
  {
    title: '开启',
    align: 'center',
    dataIndex: 'master',
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.master === true,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? true : false;
          setMaster(record.id, newStatus)
            .then(() => {
              record.master = newStatus;
            })
            .catch(() => {})
            .finally(() => {
              record.pendingStatus = false;
            });
        }
      });
    }
  },
  {
    title: '备注',
    align: 'center',
    dataIndex: 'remark'
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'createTime'
  }
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '配置名称',
    field: 'name',
    component: 'Input'
  },
  {
    label: '存储器',
    field: 'storage',
    component: 'Input'
  }
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '配置名称',
    field: 'name',
    component: 'Input',
    dynamicRules: () => {
      return [{ required: true, message: '请输入配置名称!' }];
    }
  },
  {
    label: '存储器',
    field: 'storage',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'storage_type',
      placeholder: '请选择存储器',
      stringToNumber: true
    },
    dynamicRules: () => {
      return [{ required: true, message: '请输入存储器!' }];
    }
  },
  {
    label: '备注',
    field: 'remark',
    component: 'Input'
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  }
];

/**
 * 流程表单调用这个方法获取formSchema
 * @param _formData
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
