export const clewMixins = {
  data() {
    return {};
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    purchaseSubmit(callback) {
      this.requiredFlag = true;
      // 每一个层级都是一道题的题目，子级就是题，被选中和填写的题要带上题目一块上传（题的同级也要上传）
      // 第一级题目下的题默认都要上传
      let params = {
        customerClueItemList: [],
      };
      // 复制第一级题目
      params.customerClueItemList.push(...this.deepClone(this.taskGather, 0));
      this.filterOption(this.taskGather, params);
      console.log(JSON.stringify(params));
      // 必填验证
      if (this.requiredFlag) {
        callback && callback(params);
      } else {
        this.$toast(this.requiredMessage);
      }
    },
    filterOption(optionList, params) {
      for (let val = 0; val < optionList.length; val++) {
        if (
          optionList[val].isMust == '0' &&
          optionList[val].searchValue == null &&
          optionList[val].answerType == 'dx'
        ) {
          // 题目必填校验
          this.requiredFlag = false;
          this.requiredMessage = '请选择' + optionList[val].customerClueName;
          return;
        } else if (optionList[val].isMust == '0') {
          // 子级题校验
          let customerClueOptionList = optionList[val].customerClueOptionList;
          if (customerClueOptionList.length) {
            for (let i = 0; i < customerClueOptionList.length; i++) {
              // 选中的题目Y:选中，N:未选中
              if (customerClueOptionList[i].value == 'Y') {
                if (customerClueOptionList[i].customerClueItemList) {
                  // 必填校验
                  this.isRequiredFlag(customerClueOptionList[i].customerClueItemList);
                  // 赋值选中题
                  let customerClueItemList = params.customerClueItemList;
                  customerClueItemList.push(
                    ...this.deepClone(customerClueOptionList[i].customerClueItemList, 0)
                  );
                  if (customerClueOptionList[i].customerClueItemList.length) {
                    this.filterOption(customerClueOptionList[i].customerClueItemList, params);
                  }
                }
              }
            }
          }
        }
      }
    },
    // 深拷贝指定拷贝层级
    deepClone(obj, num) {
      // 检查是否为对象或数组
      if (obj === null || typeof obj !== 'object') {
        return obj; // 基本类型直接返回
      }
      // 创建一个数组或对象
      const copy = Array.isArray(obj) ? [] : {};
      // 遍历对象的每个属性
      for (const key in obj) {
        // 每个题只复制两层子级
        if (obj.hasOwnProperty(key) && num < 2) {
          // 递归调用深拷贝
          if (key == 'customerClueOptionList' || key == 'customerClueItemList') {
            num = num + 1;
          }
          // 填写服务商代码 answerValue 添加 FSQ
          if (key == 'customerClueName') {
            if (obj[key].indexOf('填写服务商代码') != -1) {
              obj['answerValue'] = 'FSQ' + obj['answerValue'];
            }
          }
          copy[key] = this.deepClone(obj[key], num);
        }
      }
      return copy;
    },
    isRequiredFlag(optionList) {
      // console.log(optionList);
      for (let i = 0; i < optionList.length; i++) {
        // 是否必填
        if (optionList[i].isMust == 0) {
          // 输入框
          if (optionList[i].answerType == 'wb' || optionList[i].answerType == 'sz') {
            if (!optionList[i].answerValue) {
              // 必填类型
              this.requiredFlag = false;
              this.requiredMessage = optionList[i].remark;
              return;
            } else {
              // 条件校验
              if (optionList[i].minTextLength) {
                // 输入内容长度校验
                if (optionList[i].answerValue.length < optionList[i].minTextLength) {
                  this.requiredFlag = false;
                  this.requiredMessage =
                    optionList[i].customerClueName + '长度必须等于' + optionList[i].minTextLength;
                  return;
                }
              }
            }
          } else if (optionList[i].answerType == 'zp') {
            // 照片
            if (!optionList[i].fileInfoList || !optionList[i].fileInfoList.length) {
              this.requiredFlag = false;
              this.requiredMessage = optionList[i].remark;
              return;
            }
          } else if (optionList[i].answerType == 'bg') {
            // 表格
            let tableData = optionList[i].tableData;
            let filterBGData = this.filterBGData(tableData);
            if (filterBGData.hasAllFilledRow && filterBGData.filledRow == '') {
              optionList[i].answerValue = JSON.stringify(tableData);
            } else {
              this.requiredFlag = false;
              this.requiredMessage =
                filterBGData.filledRow != ''
                  ? filterBGData.filledRow
                  : '请填写' + optionList[i].customerClueName;
              return;
            }
          }
        }
      }
    },
    filterBGData(tableData) {
      // 如果一行中有一个输入框输入了内容，其他输入框没有内容，就弹框提示当前行其它输入框也要填写，
      // 整个表格必须有一行所有输入框都输入了内容
      let hasAllFilledRow = false; // 是否有整行都填写
      let filledRow = ''; // 有内容行
      tableData.data.forEach((row, rowIndex) => {
        const hasContent = Object.values(row).some(
          (cell, index) => index !== 0 && cell.trim() !== ''
        );
        const allFilled = Object.values(row).every((cell, index) => {
          return cell.trim() !== '';
        });
        if (hasContent && !allFilled) {
          filledRow = `${row.typeName}请填写完整`;
          return;
        }
        if (allFilled) {
          hasAllFilledRow = true;
        }
      });
      return { hasAllFilledRow, filledRow };
      // let data = [];
      // const isValid = tableData.data.some((row) => {
      //   if (Object.values(row).every((value) => value.trim() !== '')) {
      //     data.push(row);
      //   }
      // });
      // return data;
    },
  },
};
