import { show_message } from '@/utils/tdesign_tool';
import { NotRequiredForm } from '@/constants/token';
import { Validationrules } from '@/utils/tool';

export const FilterForm = (list: any[]) => {
  const params: any = {};
  // 遍历整个列表
  for (let i = 0; i < list.length; i++) {
    const item: any = list[i];
    for (let j = 0; j < item.lists.length; j++) {
      const it = item.lists[j];
      // 校验规则
      const message = Validationrules(it.rules, it.value, it.name);
      if (message) {
        // 提示错误信息
        show_message(message);
        return false;
      } else {
        const not_submit = NotRequiredForm.findIndex(
          (notItem: any) => notItem == it.type
        );
        if (not_submit !== -1) {
          // 不需要提交的字段
          continue;
        }
        // 类型为图片尺寸且当前选择的是自定义
        if (it.type == 'radio_group_size' && it.value == '') {
          // 找到options列表里的自定义模块
          const customObj = it.options.find((opt: any) => opt.type == 'custom');
          if (customObj) {
            if (customObj.value1 && customObj.value2) {
              params[it.name] = customObj.value1 + 'x' + customObj.value2;
            } else {
              show_message('自定义图片尺寸未填写完整');
              return false;
            }
          }
        } else if (it.name == 'number') {
          // 转换为number类型
          params[it.name] = parseFloat(it.value + '');
        } else {
          // 添加params
          params[it.name] = it.value;
        }
      }
    }
  }
  return params;
};

// 过滤form表单里的type
export const FilterFormType = (list: any[]) => {
  const obj: any = {
    list: [],
  };
  if (list.length) {
    // 修改数据
    list.forEach((item: any) => {
      item.lists.forEach((it: any) => {
        if (it.type == 'text') {
          it.component_type = 'input';
        } else if (it.type == 'select') {
          it.component_type = 'select';
        } else if (it.type == 'textarea') {
          // 多行文本输入框
          it.component_type = 'textarea';
        } else if (it.type == 'radio_group_size') {
          // 单选按钮
          it.component_type = 'radio_group_size';
        } else if (it.type == 'radiogroup') {
          it.component_type = 'radiogroup';
        } else if (it.type == 'custom-number') {
          it.component_type = 'custom-number';
        } else if (it.type == 'select_model') {
          it.component_type = 'select_model';
        } else if (it.type == 'local_upload') {
          it.component_type = 'local_upload';
        } else if (it.type == 'button') {
          it.component_type = 'button';
        } else if (it.type == 'upload') {
          // 获取上传策略
          obj.al_config = true;
          // 上传
          it.component_type = 'upload';
        } else if (it.type == 'label') {
          it.component_type = 'label';
        }
        // 保存提交的类型
        if (it.name == 'select_image') {
          // 将列表传给右侧模块
          obj[it.name] = it;
        }
        //   判断value的值
        if (!it.value) {
          it.value = '';
        }
      });
    });
  }
  obj.list = list;
  return obj;
};
