<template>
  <div id="">
    <p style="margin-bottom: 5px;">问题类别 / 检查项目 / 问题描述</p>
    <a-cascader
      v-model:value="selectedOptions"
      :options="problemList"
      popup-class-name="cascader_option_width"
      :show-search="{filter }"
      allow-clear
      placeholder=""
      @change="handleSubCat"
      ref="refSubCat">
    </a-cascader>
  </div>
</template>

<script>
import request from '../../../utils/gw_ajax_request';
export default {
  props: ['objType', 'vmodelData', 't3', 'type', 'objId'],
  data() {
    return {
      selectedOptions: [],
      problemList: [],
      checkedNodeLabel: '',
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.selectedOptions = this.vmodelData ? this.vmodelData.model : [];
      this.checkedNodeLabel = this.t3 ? this.t3 : '';
      this.getproblemSmallType();
    });
  },
  methods: {
    getproblemSmallType() {
      const result = [];
      const params = {
        type: this.objType,
        sort1: this.objType == 109 ? null : this.type,
        objId: this.objId,
        inspPblmsName:
          this.objType == 109
            ? this.type == '1'
              ? '万人以下供水工程问题清单'
              : this.type == '2'
                ? '万人以上供水工程问题清单'
                : ''
            : null,
        isTree: '否',
      };
      request.post('/dc/insp/pblms/getPblmList', params).then((res) => {
        if (res.success) {
          res.data.forEach((ele) => {
            let haveinspPblmsName = false;
            result.forEach((inspPblm) => {
              if (inspPblm['inspPblmsName'] == ele.inspPblmsName) {
                //
                let havechekPoint = false;
                inspPblm['checkPoints'].forEach((chekPoint) => {
                  if (chekPoint['checkPointName'] == ele.checkPoint) {
                    //
                    chekPoint['pblmDescs'].push({
                      pblmDesc: ele.pblmDesc,
                      sn: ele.sn,
                      guid: ele.guid,
                    });
                    havechekPoint = true;
                  }
                });
                if (!havechekPoint) {
                  const newCheckPoint = {};
                  newCheckPoint['checkPointName'] = ele.checkPoint;
                  newCheckPoint['sort2'] = ele.sort2;
                  newCheckPoint['pblmDescs'] = [
                    {
                      pblmDesc: ele.pblmDesc,
                      sn: ele.sn,
                      guid: ele.guid,
                    },
                  ];
                  inspPblm['checkPoints'].push(newCheckPoint);
                }
                haveinspPblmsName = true;
              }
            });
            if (!haveinspPblmsName) {
              const haveinspPblmsObj = {};
              haveinspPblmsObj['inspPblmsName'] = ele.inspPblmsName;
              haveinspPblmsObj['sort1'] = ele.sort1;
              haveinspPblmsObj['checkPoints'] = [
                {
                  checkPointName: ele.checkPoint,
                  sort2: ele.sort2,
                  pblmDescs: [
                    {
                      pblmDesc: ele.pblmDesc,
                      sn: ele.sn,
                      guid: ele.guid,
                    },
                  ],
                },
              ];
              result.push(haveinspPblmsObj);
            }
          });
          this.problemList = result;

          this.problemList.forEach((ele, index) => {
            ele['label'] = ele.inspPblmsName;
            ele['value'] = ele.inspPblmsName;
            ele['children'] = ele.checkPoints;
            ele.children.forEach((elem, index) => {
              elem['label'] = elem.checkPointName;
              elem['value'] = elem.checkPointName;
              elem['children'] = elem.pblmDescs;
              elem.children.forEach((element) => {
                element['label'] = element.pblmDesc;
                element['value'] = element.guid;
              });
            });
          });
        }
      });
    },
    handleSubCat(values, selectedOptions) {
      this.checkedNodeLabel = selectedOptions[2].label
        ? selectedOptions[2].label
        : '';
    },
    filter(inputValue, path) {
      return path.some(
        (option) =>
          option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1,
      );
    },
  },
  watch: {
    selectedOptions(val) {
      this.$nextTick(() => {
        if (val.length) {
          this.$emit('modelData', {
            model: val,
            dataList: this.problemList,
            label: this.checkedNodeLabel,
          });
        } else {
          this.$emit('modelData', { model: val, dataList: this.problemList });
        }
      });
    },
  },
};
</script>

<style lang='less' scoped>
:deep(.ant-cascader-menus) {
  max-width: 1000px;
  overflow: hidden;
}
</style>
