<template>
  <!-- 
    整页表单的分片表单
    v 1.0
    2021-05-12
    Lufthafen
  -->
  <a-form-model
    :label-col="labelCol"
    :model="form"
    :rules="rules"
    :wrapper-col="wrapperCol"
    ref="form"
  >
    <a-spin :spinning="loading">
      <a-icon slot="indicator" spin type="loading" />
      <!-- 表单控件 -->
      <!-- ... -->
      <a-row :gutter="16" type="flex">
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="房屋场地" prop="houseSite">
            <a-checkbox-group v-model="form.houseSite">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.houseHouseSite"
              >{{ item.value }}</a-checkbox>
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="相邻施工" prop="adjacentConstruction">
            <a-checkbox-group v-model="form.adjacentConstruction">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.houseAdjacentConstruction"
              >{{ item.value }}</a-checkbox>
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="化学侵蚀" prop="chemicalErosion">
            <a-checkbox-group v-model="form.chemicalErosion">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.houseChemicalErosion"
              >{{ item.value }}</a-checkbox>
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="结构拆改" prop="structuralDismantling">
            <a-radio-group button-style="solid" v-model="form.structuralDismantling">
              <a-radio-button
                :key="item.code"
                :value="item.code"
                v-for="item in codes.houseStructuralDismantling"
              >{{ item.value }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="加层改造" prop="addingLayer">
            <a-radio-group button-style="solid" v-model="form.addingLayer">
              <a-radio-button
                :key="item.code"
                :value="item.code"
                v-for="item in codes.houseAddingLayer"
              >{{ item.value }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="修缮加固" prop="repairAndReinforce">
            <a-checkbox-group v-model="form.repairAndReinforce">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.houseRepairAndReinforce"
              >{{ item.value }}</a-checkbox>
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="历史灾害" prop="historicalCalamity">
            <a-checkbox-group v-model="form.historicalCalamity">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.houseHistoricalCalamity"
              >{{ item.value }}</a-checkbox>
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="使用功能变更" prop="functionalChange">
            <a-checkbox-group v-model="form.functionalChange">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.houseFunctionalChange"
              >{{ item.value }}</a-checkbox>
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="其他调查内容">
            <a-textarea auto-size placeholder="请输入其他调查内容" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-spin>
  </a-form-model>
</template>
<script>
/* 表单内容默认值 */
const defaultForm = {
  /* ... */
};

export default {
  props: ['param'],

  data() {
    return {
      labelCol: { flex: '150px' },
      wrapperCol: { flex: '1' },

      /** 表单数据 */
      form: {},
      /** 验证格式 */
      rules: {
        /* ... */
        houseSite: [{ required: true, message: '请选择房屋场地' }],
        adjacentConstruction: [{ required: true, message: '请选择相邻施工' }],
        chemicalErosion: [{ required: true, message: '请选择化学侵蚀' }],
        structuralDismantling: [{ required: true, message: '请选择结构拆改' }],
        addingLayer: [{ required: true, message: '请选择加层改造' }],
        repairAndReinforce: [{ required: true, message: '请选择修缮加固' }],
        historicalCalamity: [{ required: true, message: '请选择历史灾害' }],
        functionalChange: [{ required: true, message: '请选择使用功能变更' }],
      },

      /** 加载异步数据状态 */
      loading: false,

      /** 其他成员属性 */
      /* ... */
      codes: {
        houseHouseSite: [],
        houseAdjacentConstruction: [],
        houseChemicalErosion: [],
        houseStructuralDismantling: [],
        houseAddingLayer: [],
        houseRepairAndReinforce: [],
        houseHistoricalCalamity: [],
        houseFunctionalChange: [],
      },
    };
  },

  watch: {
    'form.adjacentConstruction'(value, oldValue) {
      this.onCheckedNone(value, oldValue, 'adjacentConstruction');
    },
    'form.chemicalErosion'(value, oldValue) {
      this.onCheckedNone(value, oldValue, 'chemicalErosion');
    },
    'form.repairAndReinforce'(value, oldValue) {
      this.onCheckedNone(value, oldValue, 'repairAndReinforce');
    },
    'form.historicalCalamity'(value, oldValue) {
      this.onCheckedNone(value, oldValue, 'historicalCalamity');
    },
    'form.functionalChange'(value, oldValue) {
      this.onCheckedNone(value, oldValue, 'functionalChange');
    },
  },

  async created() {
    await this.onInit();
    this.onFillData();
  },

  methods: {
    /**
     * 必要的方法
     * 在打开编辑页时允许填充数据
     */
    onFillData() {
      /** 将默认数据覆盖到form */
      const record = this.param && this.param.record;

      this.form = this.$_.cloneDeep({
        ...defaultForm,
        ...record,
        /** 在此处添加其他默认数据转换 */
        /* ... */
      });
    },

    /**
     * 必要方法
     * 验证表单并获取表单数据
     */
    onGetData() {
      return new Promise((reslove, reject) => {
        this.$refs.form.validate((valid) => {
          if (valid) {
            const record = this.$_.cloneDeep(this.form);

            /** 验证通过后可以对数据进行转换得到想要提交的格式 */
            /* ... */

            reslove(record);
          } else {
            reject();
          }
        });
      });
    },

    /**
     * 必要的方法
     * 在外部窗口进行保存时调用表单验证
     */
    onValidate(callback) {
      this.$refs.form.validate(callback);
    },

    /**
     * 必要的方法
     * 在外部窗口关闭或重置时对表单验证进行初始化
     */
    onResetFields() {
      setTimeout(() => {
        this.$refs.form.resetFields();

        /** 在这里可以初始化当前组件中其他属性 */
        /* ... */
      }, 300);
    },

    /**
     * 必要方法
     * 加载当前表单中所需要的异步数据
     */
    async onInit() {
      this.loading = true;
      /** 可以在这里await获取一些异步数据 */
      /* ... */
      await this.onLoadCodes();
      this.loading = false;
    },

    /** 当前组件的其他方法 */
    /* ... */
    onLoadCodes() {
      return this.$api
        .sysDictTypeDropDowns({
          code: [
            'dic_house_house_site',
            'dic_house_adjacent_construction',
            'dic_house_chemical_erosion',
            'dic_house_structural_dismantling',
            'dic_house_adding_layer',
            'dic_house_repair_and_reinforce',
            'dic_house_historical_calamity',
            'dic_house_functional_change',
          ],
        })
        .then(
          ({
            data: {
              dic_house_house_site,
              dic_house_adjacent_construction,
              dic_house_chemical_erosion,
              dic_house_structural_dismantling,
              dic_house_adding_layer,
              dic_house_repair_and_reinforce,
              dic_house_historical_calamity,
              dic_house_functional_change,
            },
          }) => {
            this.codes = {
              houseHouseSite: dic_house_house_site,
              houseAdjacentConstruction: dic_house_adjacent_construction,
              houseChemicalErosion: dic_house_chemical_erosion,
              houseStructuralDismantling: dic_house_structural_dismantling,
              houseAddingLayer: dic_house_adding_layer,
              houseRepairAndReinforce: dic_house_repair_and_reinforce,
              houseHistoricalCalamity: dic_house_historical_calamity,
              houseFunctionalChange: dic_house_functional_change,
            };
          }
        );
    },

    onCheckedNone(value, oldValue, key) {
      // 选中"无"时全不选,反之取消选择"无"
      if (value.indexOf('0') >= 0 && oldValue.indexOf('0') === -1) {
        this.form[key] = ['0'];
      } else if (value.indexOf('0') >= 0 && value.length > 1) {
        this.form[key].splice(value.indexOf('0'), 1);
      }
    },
  },
};
</script>