import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';

//列表数据
export const columns: BasicColumn[] = [
  {
    title: 'id',
    align: 'center',
    dataIndex: 'id',
    sorter: true,
    width: 0,
    ifShow: false, // 修正：统一使用ifShow而不是ifShow: true
  },
  {
    title: '指标编号',
    align: 'center',
    dataIndex: 'mtrcNo',
    sorter: true,
    width: 80,
    resizable: true,
  },
  {
    title: '指标名称',
    align: 'left',
    dataIndex: 'mtrcName',
    width: 180,
    resizable: true,
  },
  {
    title: '领域名称',
    align: 'left',
    dataIndex: 'domainName',
    width: 200,
    resizable: true,
  },
  {
    title: '采集方法',
    align: 'center',
    dataIndex: 'collMethod',
    sorter: true,
    resizable: true,
    width: 100,
    customRender: ({ text }) => {
      return render.renderDict(text, 'colletway');
    },
  },
  {
    title: '是否统计',
    align: 'center',
    dataIndex: 'mtrcTp',
    sorter: true,
    resizable: true,
    width: 100,
    customRender: ({ text }) => {
      return render.renderDict(text, 'metrictype');
    },
  },
  {
    title: '低风险',
    align: 'center',
    dataIndex: 'tolVal',
    sorter: true,
    width: 100,
    resizable: true,
  },
  {
    title: '中风险',
    align: 'center',
    dataIndex: 'warnVal',
    sorter: true,
    width: 100,
    resizable: true,
  },
  {
    title: '高风险',
    align: 'center',
    dataIndex: 'intvVal',
    sorter: true,
    width: 100,
    resizable: true, // 补充：添加resizable属性保持一致性
  },
  {
    title: '监测频率',
    align: 'center',
    dataIndex: 'collFreq',
    sorter: true,
    resizable: true,
    width: 100,
    customRender: ({ text }) => {
      return render.renderDict(text, 'colletrate');
    },
  },
  {
    title: '指标说明',
    align: 'left',
    dataIndex: 'des',
    width: 100,
    resizable: true,
  },
  {
    title: '',
    align: 'center',
    dataIndex: 'mtrcCtp',
    width: 0,
    ifShow: false
  },
  {
    title: '',
    align: 'center',
    dataIndex: 'params',
    width: 0,
    ifShow: false
  },
];

//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '指标名称',
    field: 'mtrcName',
    component: 'Input',
    colProps: { lg: 4, md: 8 },
  },
  {
    label: '频率',
    field: 'collFreq',
    component: 'JDictSelectTag',
    colProps: { lg: 4, md: 8 },
    componentProps: {
      dictCode: 'colletrate',
      placeholder: '请选择频率', // 补充：添加placeholder
    },
  },
  {
    label: '是否统计',
    field: 'mtrcTp',
    component: 'JDictSelectTag',
    colProps: { lg: 4, md: 8 },
    componentProps: {
      dictCode: 'metrictype',
      stringToNumber: false,
      placeholder: '请选择',
    },
  },
];

//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '指标名称',
    field: 'mtrcName',
    component: 'Input',
    colProps: { lg: 12 },
    required: true,
    componentProps: { // 补充：添加componentProps
      placeholder: '请输入指标名称',
    },
  },
  {
    label: '采集方法',
    field: 'collMethod',
    component: 'JDictSelectTag',
    colProps: { lg: 12 },
    componentProps: {
      dictCode: 'colletway',
      stringToNumber: true,
      placeholder: '请选择采集方式',
    },
    required: true
  },
  {
    label: '指标编码',
    field: 'mtrcNo',
    component: 'Input',
    colProps: { lg: 12 },
    componentProps: {
      placeholder: '不输入，系统将自动生成',
    },
    dynamicRules: ({ model, schema }) => {
      const rulesAry: {}[] = [];
      if (model.mtrcNo) {
        if (model.mtrcCtp == 1) {
          rulesAry.push({
            pattern: /^(ZB)\d{5}$/,
            message: '字符串必须以ZB开头，后跟5位数字',
          });
        } else {
          rulesAry.push({
            pattern: /^(MS)\d{5}$/,
            message: '字符串必须以MS开头，后跟5位数字',
          });
        }
      }
      rulesAry.push(rules.duplicateCheckRule('metric_monitor_set', 'mtrc_no', model, schema, true));
      return rulesAry;
    },
    show: true,
  },
  {
    label: '是否统计',
    field: 'mtrcTp',
    component: 'JDictSelectTag',
    defaultValue: 1,
    colProps: { lg: 12 },
    componentProps: {
      dictCode: 'metrictype',
      stringToNumber: true,
      type: 'radio',
    },
    required: true
  },
  {
    label: '所属领域',
    field: 'domainNo',
    component: 'JCascaderDomain',
    colProps: { lg: 12 },
    componentProps: {
      dict: 'st_domain,domain_name,domain_code',
      pidField: 'upper_domain_code',
      pidValue: '0',
      hasChildField: 'has_child',
      maxHeight: '165px',
      placeholder: '请选择所属领域', // 补充：添加placeholder
    },
    required: true
  },
  
  {
    label: '指标类别',
    field: 'mtrcClass',
    component: 'JDictSelectTag',
    colProps: { lg: 12 },
    componentProps: {
      dictCode: 'mtrc_class',
      stringToNumber: true,
      placeholder: '请选择指标分类',
    },
    required: true
  },
  {
    label: '监测频率',
    field: 'collFreq',
    component: 'JDictSelectTag',
    colProps: { lg: 12 },
    componentProps: {
      dictCode: 'colletrate',
      stringToNumber: true,
      placeholder: '请选择监测频率', 
    },
    required: true
  },
  {
    label: '计量单位',
    field: 'unit',
    component: 'Input',
    colProps: { lg: 12 },
    required: false,
    componentProps: {
      placeholder: '请输入计量单位',
    },
  },
  {
    label: '参数',
    field: 'params',
    component: 'JSelectMetric',
    colProps: { lg: 24 },
    componentProps: {
      rowKey: 'mtrcNo',
      labelKey: 'mtrcName',
      placeholder: '请选择参数', 
    },
    required: true
  },
  {
    label: '指标公式',
    field: 'formulas',
    component: 'InputTextArea',
    colProps: { lg: 24 },
    componentProps: {
      rows: 2,
      placeholder: '请输入指标公式', // 补充：添加placeholder
    },
    required: true
  },
  {
    label: '低风险',
    field: 'tolVal',
    component: 'InputNumber',
    colProps: { lg: 24 },
    slot: 'tolVal',
    required: false,
    componentProps: { // 补充：添加componentProps
      placeholder: '请输入低风险值',
    },
  },
  {
    label: '中风险',
    field: 'warnVal',
    component: 'InputNumber',
    slot: 'warnVal',
    colProps: { lg: 24 },
    required: false,
    componentProps: { // 补充：添加componentProps
      placeholder: '请输入中风险值',
    },
  },
  {
    label: '高风险',
    field: 'intvVal',
    component: 'InputNumber',
    slot: 'intvVal',
    colProps: { lg: 24 },
    required: false,
    componentProps: { // 补充：添加componentProps
      placeholder: '请输入高风险值',
    },
  },
  {
    label: '指标说明',
    field: 'des',
    component: 'InputTextArea',
    colProps: { lg: 24 },
    componentProps: {
      rows: 3,
      placeholder: '请输入指标说明', // 补充：添加placeholder
    },
  },
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'mtrcCtp',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'stTolVal',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'noticeReceiveLow',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'noticeReceiveHight',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'noticeReceiveMiddle',
    component: 'Input',
    show: false,
  },
];

export const StMetricColumns: BasicColumn[] = [
  {
    title: '指标编号',
    align: 'left',
    dataIndex: 'metricNo',
    width: 80,
  },
  {
    title: '指标名称',
    align: 'left',
    dataIndex: 'metricName',
    width: 150, // 补充：添加宽度
  },
  {
    title: 'id',
    align: 'center',
    dataIndex: 'id',
    sorter: true,
    width: 50,
    ifShow: false,
  },
];

export const MetricColumns: BasicColumn[] = [
  {
    title: '指标编号',
    align: 'center',
    dataIndex: 'mtrcNo',
    width: 80,
    ifShow: false,
  },
  {
    title: '领域名称',
    align: 'left',
    dataIndex: 'domainName',
    resizable: true,
    width: 200,
  },
  {
    title: '指标名称',
    align: 'left',
    dataIndex: 'mtrcName',
    width: 200,
  },
  {
    title: '监测频率',
    align: 'center',
    dataIndex: 'collFreq',
    width: 80,
    customRender: ({ text }) => {
      return render.renderDict(text, 'colletrate');
    },
  },
];

//搜索表单
export const searchStandSchema: FormSchema[] = [
  {
    label: '指标名称',
    field: 'metricName',
    component: 'Input',
    colProps: { lg: 6, md: 7 },
    itemProps: { 
      labelCol: { md: { span: 6 }, lg: { span: 6 } }, 
      wrapperCol: { md: { span: 20 }, lg: { span: 20 } } 
    },
    componentProps: { // 补充：添加componentProps
      placeholder: '请输入指标名称',
    },
  },
  {
    label: '采集方式',
    field: 'collectCode',
    component: 'JDictSelectTag',
    colProps: { lg: 6, md: 8 },
    itemProps: { 
      labelCol: { md: { span: 6 }, lg: { span: 6 } }, 
      wrapperCol: { md: { span: 20 }, lg: { span: 20 } } 
    },
    componentProps: {
      dictCode: 'colletway',
      stringToNumber: false,
      placeholder: '请选择采集方式',
    },
  },
  {
    label: '指标类型',
    field: 'metricTypeCode',
    component: 'JDictSelectTag',
    itemProps: { 
      labelCol: { md: { span: 6 }, lg: { span: 6 } }, 
      wrapperCol: { md: { span: 20 }, lg: { span: 20 } } 
    },
    colProps: { lg: 6, md: 10 },
    componentProps: {
      dictCode: 'metrictype',
      placeholder: '请选择指标类型',
    },
  },
];

//表单数据 - 修正：变量名重复，改为searchStandSchema2
export const searchStandSchema2: FormSchema[] = [
  {
    label: '领域',
    field: 'domainNo',
    component: 'JCascaderDomain',
    colProps: { lg: 6, md: 6 },
    componentProps: {
      dict: 'st_domain,domain_name,domain_code',
      pidField: 'upper_domain_code',
      pidValue: '0',
      hasChildField: 'has_child',
      maxHeight: '165px',
      placeholder: '请选择领域', // 补充：添加placeholder
    },
  },
  {
    label: '名称',
    field: 'mtrcName',
    component: 'Input',
    colProps: { lg: 6, md: 6 },
    componentProps: { // 补充：添加componentProps
      placeholder: '请输入名称',
    },
  },
  {
    label: '频率',
    field: 'collFreq',
    component: 'JDictSelectTag',
    colProps: { lg: 6, md: 6 },
    componentProps: {
      dictCode: 'colletrate',
      placeholder: '请选择频率', // 补充：添加placeholder
    },
  },
];

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