<template>
  <div id="">
    <a-form
      :model="problemForm"
      :label-col="{span: 6}"
      :wrapper-col="{span: 18}"
      :rules="rules"
      ref="formRef">
      <a-form-item>
        <a-button type="link" @click="selectPblm" style="float: left;">选择问题</a-button>
      </a-form-item>
      <a-form-item label="问题类别" style="text-align:left">
        <a-select v-model:value="t1" placeholder="请选择" disabled>
          <a-select-option v-for="(item, index) in problemList" :key="index" :value="item.inspPblmsName">{{
            item.inspPblmsName }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="检查项目" style="text-align:left">
        <a-select v-model:value="t2" placeholder="请选择" disabled>
          <a-select-option v-for="(item, index) in currentCheckPointList" :key="index" :value="item.checkPointName">{{
            item.checkPointName
          }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="问题描述" style="text-align:left">
        <a-textarea v-model:value="t3" :rows="4" placeholder="" disabled />
      </a-form-item>
      <a-form-item label="详细描述" prop="inspPblmDesc">
        <a-textarea v-model:value="problemForm.inspPblmDesc" :rows="2" placeholder="请输入内容" required />
      </a-form-item>
      <a-form-item label="备注">
        <a-textarea v-model:value="problemForm.note" :rows="2" placeholder="请输入内容" />
      </a-form-item>
      <a-form-item prop="proposeZgDate" label="建议整改时限" v-if="flag == '1'">
        <a-date-picker
          v-model:value="problemForm.proposeZgDate"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          allow-clear
          style="width: 300px !important;"
          :disabled-date="disabledDate"
          placeholder="建议整改时限"></a-date-picker>
      </a-form-item>
      <a-form-item prop="proposeZgZgxx" label="建议整改要求" v-if="flag == '1'">
        <a-textarea v-model:value="problemForm.proposeZgZgxx" :rows="2" placeholder="请输入内容" />
      </a-form-item>
      <a-form-item label="上传照片">
        <upload ref="uploadFile" :url="`/file/insert?bizId=`" @uploadOver="removeNewSupervision"></upload>
      </a-form-item>
      <a-form-item :wrapper-col="{ span: 18, offset: 6 }" style="margin-top: 50px">
        <a-button type="primary" @click="onSubmit" style="width: 100px;float:right">保存</a-button>
      </a-form-item>
    </a-form>
    <a-modal v-model:open="pblmEascaderVisible" class="pblmEascader" title="选择问题" width="60%">
      <pblmEascader :objType="rowData.type" @modelData="modelData" :vmodelData="newPblmValue" :t3="t3"></pblmEascader>
      <template #footer>
        <a-button @click="pblmEascaderVisible=false">返 回</a-button>
        <a-button type="primary" @click="submitEascader">确 定</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import moment from 'moment';
import { submitUpload } from '../../../api/duChaAPI.js';
import request from '../../../utils/gw_ajax_request.js';
import upload from '../../../widgets/uploadFileNew.vue';
import pblmEascader from '../../duChaTianBao/project/pblmEascader';
export default {
  props: [
    'objId',
    'objName',
    'villType',
    'objType',
    'problemMiddleType',
    'problemSmallType',
    'engId',
    'currentObjectRgstrId',
    'newModelData',
    'rowData',
    'flag',
  ],
  data() {
    return {
      moment,
      problemForm: {
        t1: '',
        t2: '',
        t3: '',
        villType: '',
        objId: '',
        regid: '',
        objType: '',
        inspPblmCate: '',
        inspPblmDesc: '',
        ifCasePblm: '0',
        pblmStat: '',
        dataStat: '',
        nm: '',
        recPers: '',
        note: '',
        gwComFiles: [
          {
            id: '',
            filePath: '',
            bizId: '',
          },
        ],
        // zgDate: null,
        // pblmSggtn: '',
        proposeZgDate: null,
        proposeZgZgxx: '',
      },
      inspPblmCate: '',
      inspPlamCatelist: ['一般', '较重', '严重'],
      imgList: [],
      videoList: [],
      audioList: [],
      pblmStat: '',
      gwComFiles: [],
      problemList: [],
      currentCheckPointList: [],
      currentProblemDescList: [],
      t1: '',
      t2: '',
      t3: '',
      xiangqingmiaoshu: '',
      wenTiLeiBie: '',
      jianChaXiangMu: '',
      rules: {
        inspPblmDesc: [
          {
            required: true,
            message: '请输入描述',
            trigger: 'blur',
          },
        ],
        proposeZgZgxx: [
          {
            required: true,
            message: '请输入建议整改要求',
            trigger: 'blur',
          },
        ],
      },
      pblmEascaderVisible: false,
      newPblmValue: [],
    };
  },
  components: {
    upload,
    pblmEascader,
  },
  computed: {
    recPers() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '';
    },
  },
  mounted() {
    this.syncNewModel(this.newModelData);
  },
  watch: {
    newModelData(val) {
      this.syncNewModel(val);
    },
  },
  methods: {
    syncNewModel(data) {
      if (!data) return;
      this.newPblmValue = data;
      this.t1 = data.model[0];
      this.t2 = data.model[1];
      this.t3 = data.label;
      this.getYanZhongChengDu();
    },
    disabledDate(current) {
      return current && current < moment().endOf('day').add(11, 'days');
    },
    getYanZhongChengDu() {
      request
        .post('/dc/insp/pblms/getByPblms', {
          inspPblmsName: this.t1,
          checkPoint: this.t2,
          pblmDesc: this.t3,
          type: this.objType,
        })
        .then((res) => {
          if (res.success) {
            this.inspPblmCate = res.data.inspPblmCate;
          }
        });
    },
    async onSubmit() {
      const form = this.$refs.formRef;
      if (!form || !form.validate) {
        this.$refs.formRef = form;
        this.doSubmit();
        return;
      }
      try {
        await form.validate();
        this.doSubmit();
      } catch (e) {
        // validation failed
      }
    },
    doSubmit() {
      this.problemForm.villType = '0';
      this.problemForm.recPers = this.recPers;
      this.problemForm.objId = this.objId;
      this.problemForm.regid = this.currentObjectRgstrId;
      this.problemForm.nm = this.objName;
      this.problemForm.objType = this.objType;
      this.problemForm.pblmStat = '0';
      this.problemForm.cwsCode = this.rowData.type;
      this.problemForm['inspPblmName'] = this.t1;
      this.problemForm['inspPblmDesc'] = this.problemForm.inspPblmDesc;
      this.problemForm['inspPlbmType'] = this.t2;
      this.problemForm['inspPblmCode'] = this.t3;
      this.problemForm['pblmsTypeId'] = this.newPblmValue.model[2];
      this.problemForm.inspPblmCate = this.inspPblmCate;
      if (!this.problemForm.inspPblmName) {
        this.$message.info('问题类别不能为空');
        return;
      }
      if (!this.problemForm.inspPlbmType) {
        this.$message.info('检查项目不能为空');
        return;
      }
      if (!this.problemForm.inspPblmCode) {
        this.$message.info('问题描述不能为空');
        return;
      }
      request.post('/dc/insp/pblm', this.problemForm).then((res) => {
        if (res.success) {
          const pblmId = res.data.pblmId;
          console.log('[commonwenti] doSubmit success, pblmId:', pblmId, 'uploadFile:', this.$refs.uploadFile);
          this.pblmId = pblmId;
          this.gwComFiles = [];
          this.pblmStat = res.data.pblmStat;
          if (this.$refs.uploadFile.submitUpload(pblmId)) {
            this.removeNewSupervision();
          }
          this.$emit('closeDialog');
        }
      });
    },
    // 文件上传
    submitUpload() {
      this.$refs.uploadFile.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) {
        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.currentCheckPointList = item.checkPoints;
    },
    currentCheckPointListChange: function (item) {
      this.currentProblemDescList = item.pblmDescs;
    },
    modelData(val) {
      this.syncNewModel(val);
    },
    selectPblm() {
      this.pblmEascaderVisible = true;
    },
    submitEascader() {
      if (!this.newPblmValue.model.length) {
        this.$message.warning('问题不能为空');
        return;
      }
      this.pblmEascaderVisible = false;
    },
  },
};
</script>

<style lang='less' scoped>

</style>
