<template>
  <a-form :rules="rules" :model="problemForm" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }" ref="problemForm">
    <a-form-item label="问题类别">
      <a-select v-model:value="t1" placeholder="请选择" @change="problemListChange">
        <a-select-option v-for="(item, index) in supervisionState" :key="index" :value="item.items">{{ item.name }}
        </a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="检查项目" style="text-align:left">
      <a-select v-model:value="t2" placeholder="请选择" @change="currentCheckPointListChange">
        <a-select-option v-for="(item, index) in currentCheckPointList" :key="index" :value="item.items">{{ item.name }}
        </a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="问题描述" style="text-align:left">
      <a-select v-model:value="t3" placeholder="请选择" @change="currentProblemDescListChange">
        <a-select-option v-for="(item, index) in currentProblemDescList" :key="index" :value="item.pblmDesc">{{ item.pblmDesc }}
        </a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="问题描述" style="text-align:left">
      <a-textarea :rows="2" placeholder="请输入内容" v-model:value="problemForm.inspPblmDesc"></a-textarea>
    </a-form-item>
    <a-form-item label="列为典型问题" style="text-align:left">
      <a-radio-group v-model:value="problemForm.ifCasePblm">
        <a-radio value="1">是</a-radio>
        <a-radio value="0">否</a-radio>
      </a-radio-group>
    </a-form-item>
    <a-form-item label="上传照片">
      <upload ref="uploadFile" :url="`/pdcApi/file/insert?bizId=`" @uploadOver="removeNewSupervision"></upload>>
    </a-form-item>
    <a-form-item :wrapper-col="{ span: 16, offset: 8 }" style="margin-top: 50px">
      <a-button type="primary" @click="onSubmit('problemForm')" style="width: 100px;float:right">
        保存</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
import { submitUpload } from '../../../api/duChaAPI';
import request from '../../../utils/gw_ajax_request';
import upload from '../../../widgets/uploadFile.vue';
export default {
  props: [
    'objId',
    'objName',
    'villType',
    'objType',
    'problemMiddleType',
    'problemSmallType',
    'engId',
    'currentObjectRgstrId',
    'problData',
  ],
  data() {
    return {
      problemForm: {
        t1: '',
        t2: '',
        t3: '',
        objId: '',
        regid: '',
        objType: '',
        // inspPblmName: "",
        inspPblmCate: '',
        inspPblmDesc: '',
        ifCasePblm: '0',

        pblmStat: '',
        dataStat: '',
        nm: '',
        recPers: '',
        note: '',
        gwComFiles: [
          {
            id: '',
            filePath: '',
            bizId: '',
          },
        ],
      },
      inspPblmCate: '',
      imgList: [],
      videoList: [],
      audioList: [],
      pblmStat: '',
      gwComFiles: [],
      problemList: [],
      currentCheckPointList: [],
      currentProblemDescList: [],
      t1: '',
      t2: '',
      t3: '',
      xiangqingmiaoshu: '',
      wenTiLeiBie: '',
      jianChaXiangMu: '',
      rules: {
        inspPblmDesc: [
          {
            required: true,
            message: '请输入描述',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  components: {
    upload,
  },
  computed: {
    recPers() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '';
    },
  },
  created() {
    if (this.problData) {
      this.getproblemSmallType();
    } else {
      this.getproblemType();
    }
  },
  methods: {
    getproblemSmallType() {
      request
        .get(`/dc/insp/pblms/getPblmsByQuesNum/${this.problData}/12`)
        .then((res) => {
          if (res.success) {
            this.problemList = res.data;
            this.problemList = res.data;
            this.t1 = this.problemList[0];
            this.currentCheckPointList = this.problemList[0].items;
            this.t2 = this.currentCheckPointList[0];
            this.currentProblemDescList = this.problemList[0].items[0].items;
            this.t3 = this.currentProblemDescList[0];
          }
        });
    },
    getproblemType() {
      const result = [];
      request
        .get('/dc/insp/pblms/getPblmType', {
          params: {
            type: '12',
            pguid: '12000000000000000000000000000000',
          },
        })
        .then((res) => {
          if (res.success) {
            res.data.forEach((ele) => {
              let haveinspPblmsName = false;
              result.forEach((inspPblm) => {
                if (inspPblm['name'] == ele.inspPblmsName) {
                  //
                  let havechekPoint = false;
                  inspPblm['items'].forEach((chekPoint) => {
                    if (chekPoint['name'] == ele.checkPoint) {
                      //
                      chekPoint['items'].push({
                        pblmDesc: ele.pblmDesc,
                        sn: ele.sn,
                        guid: ele.guid,
                      });
                      havechekPoint = true;
                    }
                  });
                  if (!havechekPoint) {
                    const newCheckPoint = {};
                    newCheckPoint['name'] = ele.checkPoint;
                    newCheckPoint['sort2'] = ele.sort2;
                    newCheckPoint['items'] = [
                      {
                        pblmDesc: ele.pblmDesc,
                        sn: ele.sn,
                        guid: ele.guid,
                      },
                    ];
                    inspPblm['items'].push(newCheckPoint);
                  }
                  haveinspPblmsName = true;
                }
              });
              if (!haveinspPblmsName) {
                const haveinspPblmsObj = {};
                haveinspPblmsObj['name'] = ele.inspPblmsName;
                haveinspPblmsObj['sort1'] = ele.sort1;
                haveinspPblmsObj['items'] = [
                  {
                    name: ele.checkPoint,
                    sort2: ele.sort2,
                    items: [
                      {
                        pblmDesc: ele.pblmDesc,
                        sn: ele.sn,
                        guid: ele.guid,
                      },
                    ],
                  },
                ];
                result.push(haveinspPblmsObj);
              }
            });

            this.problemList = result;
          }
        });
    },
    getYanZhongChengDu() {
      this.inspPblmCate = this.t3.inspPblmCate;
    },
    onSubmit(problemForm) {
      this.$refs[problemForm].validate((valid) => {
        if (valid) {
          this.problemForm.recPers = this.recPers;
          this.problemForm.objId = this.objId;
          this.problemForm.regid = this.currentObjectRgstrId;
          this.problemForm.nm = this.objName;
          this.problemForm.objType = '12';
          this.problemForm.pblmStat = '0';
          this.problemForm['inspPblmName'] = this.t1.name;
          this.problemForm['inspPblmsName'] = this.t2.name; // 检查项目
          this.problemForm['inspPblmDesc'] = this.problemForm.inspPblmDesc;
          this.problemForm['inspPlbmType'] = this.t2.sort;
          this.problemForm['inspPblmCode'] = this.t3.sn;
          this.problemForm['pblmsTypeId'] = this.t3.guid;
          this.problemForm['reviConc'] = '0';
          console.log(this.problemForm);
          if (!this.problemForm.inspPblmName) {
            this.$message.info('请选择问题类别');
            return;
          }
          if (!this.problemForm.inspPblmsName) {
            this.$message.info('请选择检查项目');
            return;
          }
          if (!this.problemForm.inspPblmName) {
            this.$message.info('请选择检查事项');
            return;
          }
          if (!this.problemForm.inspPblmDesc) {
            this.$message.info('请填写详细描述');
            return;
          }
          request.post('/dc/insp/pblm', this.problemForm).then((res) => {
            if (res.success) {
              this.pblmId = res.data.pblmId;
              this.gwComFiles = [];
              this.pblmStat = res.data.pblmStat;
              if (this.$refs.uploadFile.submitUpload(res.data.pblmId)) {
                this.removeNewSupervision();
              }
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 文件上传
    submitUpload() {
      this.$refs.upload.submit();
    },
    updatefilelist(arg) {
      request
        .post('/dc/insp/pblm/update', {
          pblmId: this.pblmId,
          gwComFiles: arg,
          pblmStat: this.pblmStat,
        })
        .then((res) => {
          this.removeNewSupervision();
        });
    },
    removeNewSupervision(arg) {
      if (arg) {
        console.log(arg);
        var args = this.gwComFiles.concat(arg);
        this.updatefilelist(args);
      }
      this.$message.success('保存成功');
      this.$emit('closeDialog');
      this.gwComFiles = [];
      this.$refs.uploadFile.init();
    },
    problemListChange: function (item) {
      this.t1 = item;
      this.currentCheckPointList = item.items;
    },
    currentCheckPointListChange: function (item) {
      this.t2 = item;
      this.currentProblemDescList = item.items;
    },
    currentProblemDescListChange: (item) => {
      console.log(item);
    },
  },
  watch: {
    t1: {
      handler: function (val, oldVal) {
        // console.log("详情"+val.pblmDesc)
        this.wenTiLeiBie = val.inspPblmsName;
        this.getYanZhongChengDu();
      },
      deep: true,
    },
    t2: {
      handler: function (val, oldVal) {
        this.jianChaXiangMu = val.checkPointName;
        this.getYanZhongChengDu();
      },
      deep: true,
    },
    t3: {
      handler: function (val, oldVal) {
        this.xiangqingmiaoshu = val.pblmDesc;
        this.getYanZhongChengDu();
      },
      deep: true,
    },
  },
};
</script>

<style lang='less' scoped>
.media_item {
  float: left;
  width: 60px;
  font-size: 32px;
}
</style>
