<template>
  <div class="gcsthyxglqk">
    <a-layout style="height:520px;">
      <a-layout-content style="border:1px solid #d5d5ff;margin-left:5px;padding:0px;">
        <p style="font-weight:bolder;background-color:#d5d5ff;padding:3px;">信息填报</p>
        <a-form :model="addObj" :label-col="{span: 8}" :wrapper-col="{span: 16}" :rules="rules" ref="ruleForm">
          <p style="margin-top:10px;margin-bottom: 10px">
            <a-icon type="caret-right" /><span style="width:120px;padding-left: 10px">效益发挥情况</span><span class="hrs"></span>
          </p>
          <a-form-item label="是否发挥效益">
            <a-radio-group v-model:value="addObj.ifEffective">
              <a-radio value="1">是</a-radio>
              <a-radio value="2">否</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="未发挥效益原因" v-if="addObj.ifEffective == 2">
            <a-radio-group v-model:value="addObj.effvCas">
              <a-radio value="1">水源问题</a-radio>
              <a-radio value="2">工程缺陷</a-radio>
              <a-radio value="3">规划需求变化</a-radio>
              <a-radio value="4">其他原因</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="具体原因" v-if="addObj.ifEffective == 2">
            <a-input v-model:value="addObj.effvMemo" placeholder="请输入备注" autocomplete="off" style="width:40%;"></a-input>
          </a-form-item>
          <a-form-item label="是否为饮用水源" >
            <a-radio-group v-model:value="addObj.ifWaterS">
              <a-radio value="1">是</a-radio>
              <a-radio value="2">否</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="饮用水源供水量" v-if="addObj.ifWaterS == '1'" prop="wsfdws">
            <a-input v-model:value="addObj.wsfdws" placeholder="" autocomplete="off" style="width:280px;" addon-after="吨/年" >
            </a-input>
          </a-form-item>
          <a-form-item label="是否有灌溉任务">
            <a-radio-group v-model:value="addObj.diskRunInfo">
              <a-radio value="1">是</a-radio>
              <a-radio value="2">否</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="灌溉面积" v-if="addObj.diskRunInfo == '1'" prop="hasSetMainFl">
            <a-input v-model:value="addObj.hasSetMainFl" placeholder="" autocomplete="off" style="width:280px;" addon-after="亩">
            </a-input>
          </a-form-item>
          <a-form-item label="其他情况">
            <a-textarea :rows="5" v-model:value="addObj.otherNote" style="width: 280px" />
          </a-form-item>
          <a-form-item :wrapper-col="{ span: 16, offset: 8 }">
            <a-button type="primary" @click="makesure" v-if="openType">保 存</a-button>
          </a-form-item>
        </a-form>
      </a-layout-content>
    </a-layout>
  </div>
</template>

<script>
import { getEffectionlist } from '../../../api/duChaTianBao.js';
import request from '../../../utils/gw_ajax_request.js';
export default {
  props: [
    'currentObjectId',
    'currentObjectName',
    'currentObjectRgstrId',
    'pType',
    'openType',
  ],
  data() {
    var checkInput = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('不能为空'));
      }
      if (value.toString().length > 8) {
        return callback(new Error('输入位数过长'));
      }
      setTimeout(() => {
        if (!Number.isInteger(parseFloat(value))) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      }, 500);
    };
    return {
      objtbStatu: {
        benefitStat: '',
        rgstrId: '',
      },
      dialogFormVisible: false,
      formLabelWidth: '180px',
      loading: true,
      addObj: {
        ifEffective: '',
        ifWaterS: '',
        wsfdws: '',
        diskRunInfo: '',
        hasSetMainFl: '',
        rgstrId: '',
        recPersId: '',
        recPers2: '',
        recPersTel: '',
        status: '1',
        otherNote: '', // 其他情况
        effvCas: '', // 未发挥效益原因1 水源问题 2工程缺陷 3规划需求变化 4其他原因
        effvMemo: '',
      },
      operationType: '',
      rsvrRules: null,
      probId: '',
      showAddProblemDialog: false,
      ruleForm: {
        wsfdws: '',
        hasSetMainFl: '',
      },
      rules: {
        wsfdws: [{ validator: checkInput, trigger: 'blur' }],
        hasSetMainFl: [{ validator: checkInput, trigger: 'blur' }],
        effvCas: [
          {
            required: true,
            message: '请选择未发挥效益原因',
            trigger: 'change',
          },
        ],
      },
    };
  },
  computed: {
    recPersId() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '1098101939614724096';
    },
  },
  created() {
    this.findItemByType();
  },
  mounted() {
    this.getformList();
  },
  methods: {
    getformList() {
      getEffectionlist(this.currentObjectRgstrId).then((res) => {
        if (res.success) {
          if (res.data && Object.keys(res.data).length) {
            this.addObj = res.data;
            this.operationType = 'update';
          } else {
            this.addObj = {};
            this.operationType = 'add';
          }
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 保存
    makesure() {
      this.addObj['rgstrId'] = this.currentObjectRgstrId;
      this.addObj['recPersId'] = this.recPersId;
      this.addObj['status'] = '1';
      this.objtbStatu.rgstrId = this.currentObjectRgstrId;
      var params = {
        bisInspSafeExtManage: this.addObj,
      };
      if (this.addObj.ifEffective == '1') {
        this.addObj.effvCas = '';
      }
      if (this.addObj.ifWaterS == '2') {
        this.addObj.wsfdws = '';
      }
      if (this.addObj.diskRunInfo == '2') {
        this.addObj.hasSetMainFl = '';
      }
      if (
        this.addObj.ifEffective ||
        this.addObj.ifWaterS ||
        this.addObj.diskRunInfo ||
        this.addObj.otherNote
      ) {
        this.objtbStatu.benefitStat = '1';
      }
      if (
        this.addObj.ifEffective &&
        this.addObj.ifWaterS &&
        this.addObj.diskRunInfo &&
        this.addObj.otherNote
      ) {
        this.objtbStatu.benefitStat = '2';
      }
      if (
        this.addObj.ifEffective == undefined &&
        this.addObj.ifWaterS == undefined &&
        this.addObj.diskRunInfo == undefined &&
        this.addObj.otherNote == undefined
      ) {
        this.objtbStatu.benefitStat = '0';
      }
      this.$confirm({
        title: '提示',
        content: '确认保存效益发挥情况信息吗？',
        okType: 'warning',
        onOk: () => {
          if (this.operationType == 'add') {
            request.post('/dc/insp/revrBenefit', this.addObj).then((res) => {
              if (res.success) {
                this.$message.success('添加成功');
                /* request.post("/dc/insp/rsvrRgstr/update", _self.objtbStatu).then(res => {
                    res;
                }); */
                this.$emit('addShuiKuSuccess');
                this.getformList();
              }
            });
          } else {
            request.post('/dc/insp/revrBenefit', this.addObj).then((res) => {
              if (res.success) {
                this.$message.success('修改成功');
                /* request.post("/dc/insp/rsvrRgstr/update", _self.objtbStatu).then(res => {
                    res;
                }); */
                this.$emit('addShuiKuSuccess');
                this.getformList();
              }
            });
          }
        },
      });
    },
    formatRadio(val) {
      if (val == '1') {
        return '是';
      } else if (val == '0') {
        return '否';
      } else {
        return val;
      }
    },
    cancelHandler() {
      this.$emit('cancelHandler');
    },
    findItemByType() {
      const obj = {
        tname: 'BIS_INSP_RSVR_PROJECT',
        type: this.pType,
      };
      request
        .post('/bis/insp/item/ques/info/findItemByType', obj)
        .then((res) => {
          this.rsvrRules = res.data;
        });
    },
    addProblem(item) {
      const rest = this.rsvrRules.filter((e) => {
        return e.eName === item;
      });
      this.probId = rest[0].id;
      this.showAddProblemDialog = true;
    },
    closePlDialog(val) {
      this.showAddProblemDialog = false;
    },
  },
  watch: {
    currentObjectRgstrId(n, o) {
      this.getformList();
    },
  },
};
</script>

<style lang='less' scoped>

</style>
