import { BasicColumn, FormSchema } from '/@/components/Table';
import { message } from 'ant-design-vue';
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '领域名称',
    align: 'left',
    dataIndex: 'domainName',
    width: 200,
  },
  {
    title: '等级',
    align: 'left',
    dataIndex: 'level',
    customRender: ({ text }) => {
      return !text ? '' : text + '级';
    },
    width: 100,
  },
  {
    title: '领域说明',
    align: 'left',
    ellipsis: true,
    dataIndex: 'domainDesc',
    width: 300,
  },
  {
    title: '更新时间',
    align: 'left',
    dataIndex: 'updatedTime',
    width: 140,
    // customRender:({text}) =>{
    //   return !text?"":(text.length>10?text.substr(0,10):text)
    // },
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '领域名称',
    field: 'domainName',
    component: 'JInput',
    colProps: { span: 6 },
  },
  {
    label: '等级',
    field: 'level',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'domainlevel',
      placeholder: '请选择等级',
    },
    colProps: { span: 6 },
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '一级领域名称',
    field: 'fdomainName',
    component: 'Input',
    dynamicDisabled: true,
    show: ({ values }) => {
      if (values && values.level === '2') {
        return true;
      }
      return false;
    },
  },
  {
    label: '一级领域名称',
    field: 'tdomainName',
    component: 'Input',
    dynamicDisabled: true,
    show: ({ values }) => {
      if (values && values.level === '3') {
        return true;
      }
      return false;
    },
  },
  {
    label: '二级领域名称',
    field: 'fdomainName',
    component: 'Input',
    dynamicDisabled: true,
    show: ({ values }) => {
      if (values && values.level === '3') {
        return true;
      }
      return false;
    },
  },
  {
    label: '等级',
    field: 'level',
    defaultValue: '1',
    dynamicDisabled: true,
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'domainlevel',
      placeholder: '请选择等级',
    },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请选择等级!' }];
    },
  },
  {
    label: '领域名称',
    field: 'domainName',
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入领域名称!' }];
    },
  },
  // {
  //   label: '领域编码',
  //   field: 'domainCode',
  //   component: 'Input',
  //   dynamicRules: ({model,schema}) => {
  //         return [
  //                { required: true, message: '请输入领域编码!'},
  //         ];
  //    },
  // },
  {
    label: '领域说明',
    field: 'domainDesc',
    component: 'InputTextArea',
    componentProps: {
      rows: 5,
    },
  },
  {
    label: '',
    field: 'upperDomainCode',
    component: 'Input',
    show: false,
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];

export const formChangeSchema: FormSchema[] = [
  {
    label: '父级领域等级',
    field: 'upperDomainlevel',
    component: 'JSearchSelect',
    colProps: { lg: 12 },
    itemProps: { labelCol: { xs: { span: 24 }, sm: { span: 6 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 18 } } },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请选择父级领域等级!' }];
    },
    componentProps: ({ schema, tableAction, formActionType, formModel }) => {
      if (formModel.level == '2' && formModel.hasChild == '1') {
        return {
          options: [{ label: '1级', value: '1' }],
          placeholder: '请选择',
          allowClear: true,
          onChange: (e: any) => {
            formModel.upperDomainlevel = null;
            formModel.upperDomainCode = null;
          },
        };
      } else {
        return {
          options: [
            { label: '1级', value: 1 },
            { label: '2级', value: '2' },
          ],
          placeholder: '请选择',
          allowClear: true,
          onChange: (e: any) => {
            const { updateSchema } = formActionType;
            formModel.upperDomainlevel = null;
            formModel.upperDomainCode = null;
            updateSchema({
              field: 'upperDomainCode',
              componentProps: {
                dict: "st_domain,domain_name,domain_code,level='0'",
                onclick: (e: any) => {
                  if (!formModel.upperDomainlevel) {
                    message.warn('请先选择父级领域等级！');
                    return;
                  }
                },
              },
            });
            if (e === undefined) {
              return;
            }
            updateSchema({
              field: 'upperDomainCode',
              componentProps: {
                dict: "st_domain,domain_name,domain_code,level='" + e + "'",
                onclick: (e: any) => {
                  if (!formModel.upperDomainlevel) {
                    message.warn('请先选择父级领域等级！');
                    return;
                  }
                },
              },
            });
          },
        };
      }
    },
  },
  {
    label: '父级领域',
    field: 'upperDomainCode',
    component: 'JSearchSelect',
    colProps: { lg: 12 },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请选择父级领域!' }];
    },
    itemProps: { labelCol: { xs: { span: 24 }, sm: { span: 6 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 18 } } },
    componentProps: ({ schema, tableAction, formActionType, formModel }) => {
      if (formModel.level == '2' && formModel.hasChild == '1') {
        return {
          dict: "st_domain,domain_name,domain_code,level='" + (parseInt(formModel.level) - 1) + "'",
          placeholder: '请选择',
          allowClear: true,
          onclick: (e: any) => {
            if (!formModel.upperDomainlevel) {
              message.warn('请先选择父级领域等级！');
              return;
            }
          },
        };
      } else {
        return {
          dict: "st_domain,domain_name,domain_code,level='" + (parseInt(formModel.level) - 1) + "'",
          placeholder: '请选择',
          allowClear: true,
          onclick: (e: any) => {
            if (!formModel.upperDomainlevel) {
              message.warn('请先选择父级领域等级！');
              return;
            }
          },
        };
      }
    },
  },
  {
    label: '等级',
    field: 'level',
    defaultValue: '1',
    dynamicDisabled: true,
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'domainlevel',
      placeholder: '请选择等级',
    },
    itemProps: { labelCol: { xs: { span: 24 }, sm: { span: 3 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 21 } } },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请选择等级!' }];
    },
  },
  {
    label: '领域名称',
    field: 'domainName',
    dynamicDisabled: true,
    component: 'Input',
    itemProps: { labelCol: { xs: { span: 24 }, sm: { span: 3 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 21 } } },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入领域名称!' }];
    },
  },
  {
    label: '领域说明',
    field: 'domainDesc',
    dynamicDisabled: true,
    itemProps: { labelCol: { xs: { span: 24 }, sm: { span: 3 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 21 } } },
    component: 'InputTextArea',
    componentProps: {
      rows: 5,
    },
  },
  {
    label: '',
    field: 'domainCode',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'hasChild',
    component: 'Input',
    show: false,
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];

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