<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="houseInfo.identification">
            <a-radio-group button-style="solid" v-model="form.houseInfo.identification">
              <a-radio-button
                :key="item.code"
                :value="item.code"
                v-for="item in codes.houseIdentification"
              >{{ item.value }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item class="ant-row-flex" label="治理改造措施" prop="houseInfo.government">
            <a-radio-group button-style="solid" v-model="form.houseInfo.government">
              <a-radio-button
                :key="item.code"
                :value="item.code"
                v-for="item in codes.houseGovernment"
              >{{ item.value }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item class="ant-row-flex" label="是否已通过解危鉴定" prop="houseInfo.fireproofGrade">
            <a-switch />
          </a-form-model-item>-->
          <a-form-model-item class="ant-row-flex" label="房屋使用状态" prop="houseInfo.houseUsedStatus">
            <a-radio-group button-style="solid" v-model="form.houseInfo.houseUsedStatus">
              <a-radio-button
                :key="item.code"
                :value="+item.code"
                v-for="item in codes.houseUsedStatus"
              >{{ item.value }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item class="ant-row-flex" label="综合等级" prop="houseInfo.houseGrade">
            <a-radio-group button-style="solid" v-model="form.houseInfo.houseGrade">
              <a-radio-button
                :key="item.code"
                :value="+item.code"
                v-for="item in codes.houseGrade"
              >{{ item.value }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-spin>
  </a-form-model>
</template>
<script>
/* 表单内容默认值 */
const defaultForm = {
  /* ... */
  houseInfo: {
    // identification: null,
    houseUsedStatus: '1',
    houseGrade: '1',
  },
};

export default {
  props: ['param'],

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

      /** 表单数据 */
      form: {
        ...defaultForm,
      },
      /** 验证格式 */
      rules: {
        /* ... */
        // 'houseInfo.identification': [{ required: true, message: '请选择鉴定等级' }],
        'houseInfo.houseUsedStatus': [{ required: true, message: '请选择房屋使用状态' }],
        'houseInfo.houseGrade': [{ required: true, message: '请选择综合等级' }],
      },

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

      /** 其他成员属性 */
      /* ... */
      codes: {
        houseIdentification: [],
        houseGovernment: [],
        houseUsedStatus: [],
        houseGrade: [],
      },
    };
  },

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

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

        if (record) {
          this.$_.giveDeep(form, record);
        }
        this.form = form;
      }
      this.loading = false;
    },

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

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

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

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

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

    /** 当前组件的其他方法 */
    /* ... */
    onLoadCodes() {
      return this.$api
        .sysDictTypeDropDowns({
          code: ['dic_house_identification', 'dic_house_government', 'dic_house_used_status', 'dic_house_grade'],
        })
        .then(
          ({ data: { dic_house_identification, dic_house_government, dic_house_used_status, dic_house_grade } }) => {
            this.codes = {
              houseIdentification: dic_house_identification,
              houseGovernment: dic_house_government,
              houseUsedStatus: dic_house_used_status,
              houseGrade: dic_house_grade,
            };
          }
        );
    },
  },
};
</script>