<template>
  <div id="">
    <a-layout>
      <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="formObj" :label-col="{ span: 4 }" :wrapper-col="{ span: 14 }">
          <p style="margin-top:10px;margin-bottom: 10px;overflow: hidden;">
            <a-icon type="caret-right" />
            <span style="width:120px;padding-left: 10px">基础信息</span>
            <span class="hrs"></span>
          </p>
          <a-form-item label="取水户名称">
            <a-input v-model:value="formObj.name" autocomplete="off" style="width:90%;"></a-input>
          </a-form-item>
          <a-form-item label="行政区划">
            <div v-if="showLocSelect">
              <a-select v-model:value="provName" @change="provChange" placeholder="选择省" style="margin-right: 10px;">
                <a-select-option v-for="item in provList" :key="item.adCode" :value="item.adCode">{{ item.adName }}
                </a-select-option>
              </a-select>
              <a-select v-model:value="cityName" @change="cityChange" placeholder="选择市" style="margin-right: 10px;">
                <a-select-option v-for="item in cityList" :key="item.adCode" :value="item.adCode">{{ item.adName }}
                </a-select-option>
              </a-select>
              <a-select v-model:value="countryName" @change="countryChange" placeholder="选择县">
                <a-select-option v-for="item in countryList" :key="item.adCode" :value="item.adCode">{{ item.adName }}
                </a-select-option>
              </a-select>
              &#x3000;
              <span
                v-if="showLocSelect"
                @click="showLocSelect = false"
                style="color: #009fff;font-size:12px;cursor:pointer">取消</span>
            </div>
            <div v-else>
              <a-input v-model:value="formObj.adName" autocomplete="off" style="width:90%;" />
              &#x3000;
              <span @click="showLocSelect = true" style="color: #009fff;font-size:12px;cursor:pointer">更改</span>
            </div>
          </a-form-item>
          <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
            <a-button type="primary" @click="addHandler" v-if="openType">保 存</a-button>
          </a-form-item>
        </a-form>
      </a-layout-content>
    </a-layout>
  </div>
</template>

<script>
import {
  getBaseInfoByObjId_szyjs,
  updateBaseInfo_szyjs,
} from '../../../api/duChaTianBao';
import request from '../../../utils/gw_ajax_request';

export default {
  components: {},
  props: {
    rowData: {
      type: Object,
      default: {},
    },
    openType: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      formObj: {
        name: '',
        loc: '',
      },
      formLabelWidth: '160px',
      showLocSelect: false,
      provName: '',
      cityName: '',
      countryName: '',
      provList: [],
      cityList: [],
      countryList: [],
      curSelectItem: 0,
      locat: {
        location: '',
        admDiv: '',
      },
    };
  },
  computed: {
    recPersId() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '';
    },
  },
  watch: {
    // 监控data中的数据变化
    showLocSelect(val) {
      if (!val) {
        this.formObj.location = this.locat.location;
        this.formObj.admDiv = this.locat.admDiv;
        this.provName = '';
        this.cityName = '';
        this.countryName = '';
      }
    },
  },
  created() {
    this.getData();
    this.getAreaList();
  },
  methods: {
    getData() {
      getBaseInfoByObjId_szyjs(this.rowData.objId).then((res) => {
        if (res.data) {
          this.formObj = res.data;
        } else {
          this.formObj = {};
        }
      });
    },
    provChange(val) {
      this.curSelectItem = 1;
      this.cityName = '';
      this.countryName = '';
      this.formObj.admDiv = val;
      this.provList.forEach((ele) => {
        if (ele.adCode == this.provName) {
          this.provNames = ele.adName;
        }
      });
      this.formObj.loc = this.provNames;
      this.getAreaList(val);
    },
    cityChange(val) {
      this.curSelectItem = 2;
      if (val && val != '') {
        this.formObj.admDiv = val;
        this.countryName = '';
        this.cityList.forEach((ele) => {
          if (ele.adCode == this.cityName) {
            this.cityNames = ele.adName;
          }
        });
        this.formObj.loc = this.provNames + this.cityNames;
        this.getAreaList(val);
      }
    },
    countryChange(val) {
      if (val && val != '') {
        this.formObj.admDiv = val;
        this.countryList.forEach((ele) => {
          if (ele.adCode == this.countryName) {
            this.countryNames = ele.adName;
          }
        });
        this.formObj.loc = this.provNames + this.cityNames + this.countryNames;
      }
    },
    getAreaList(val) {
      const areaParams = {
        adName: '',
        id: this.dczId,
        pageNum: 1,
        pageSize: 10000,
      };
      let adCode = '';
      if (!val) {
        adCode = '000000000000';
      } else {
        adCode = val;
      }
      request
        .get('/dc/ad/base/getAdData', { params: { adCode: adCode } })
        .then((res) => {
          if (res.success) {
            if (this.curSelectItem == 1) {
              this.cityList = res.data;
            } else if (this.curSelectItem == 2) {
              this.countryList = res.data;
            } else {
              this.provList = res.data;
            }
          }
        });
    },
    addHandler() {
      this.formObj.id = this.rowData.rgstrId;
      this.formObj.rgstrId = this.rowData.rgstrId;
      try {
        this.$confirm({
          title: '提示',
          content: '确认保存基础信息？',
          okType: 'warning',
          onOk: () => {
            updateBaseInfo_szyjs(this.formObj).then((res) => {
              if (res.success) {
                this.$emit('addShuiKuSuccess');
                this.showLocSelect = false;
                this.getData();
                this.$message.success('修改成功');
              }
            });
          },
        });
      } catch (e) {
        this.$message.error('修改失败！');
      }
    },
  },
};
</script>

<style lang="less" scoped>
#dcdxHeader {
  padding: 15px 20px;
}

#dxdcFooter {
  text-align: center;
}

#dxdcAside {
  border: 1px solid #d5d5ff;
}

:deep(form .ant-select) {
  width: 20%;
}
</style>
