<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="propertyRights">
            <a-radio-group button-style="solid" v-model="form.propertyRights">
              <a-radio-button
                :key="item.code"
                :value="item.code"
                v-for="item in codes.housePropertyRights"
              >{{ 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 ant-form-inline" label="房屋包含的住宅总套数">
            <section>
              <a-form-model-item
                :colon="false"
                class="ant-row-flex"
                label="直管公房"
                prop="straightHouseCount"
              >
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number
                      :min="0"
                      :step="1"
                      placeholder="几"
                      v-model="form.straightHouseCount"
                    />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
              <a-form-model-item
                :colon="false"
                class="ant-row-flex"
                label="单位自管公房"
                prop="selfHouseCount"
              >
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number
                      :min="0"
                      :step="1"
                      placeholder="几"
                      v-model="form.selfHouseCount"
                    />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
              <a-form-model-item :colon="false" class="ant-row-flex" label="其他" props="otherCount">
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number :min="0" :step="1" placeholder="几" v-model="form.otherCount" />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
            </section>
            <section>
              <a-form-model-item
                :colon="false"
                class="ant-row-flex"
                label="商品房"
                prop="businessCount"
              >
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number
                      :min="0"
                      :step="1"
                      placeholder="几"
                      v-model="form.businessCount"
                    />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
              <a-form-model-item
                :colon="false"
                class="ant-row-flex"
                label="房改房"
                prop="changeHouseCount"
              >
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number
                      :min="0"
                      :step="1"
                      placeholder="几"
                      v-model="form.changeHouseCount"
                    />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
              <a-form-model-item
                :colon="false"
                class="ant-row-flex"
                label="拆迁安置房"
                prop="resettlementHouseCount"
              >
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number
                      :min="0"
                      :step="1"
                      placeholder="几"
                      v-model="form.resettlementHouseCount"
                    />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
            </section>
            <section>
              <a-form-model-item
                :colon="false"
                class="ant-row-flex"
                label="私房"
                prop="privateHouseCount"
              >
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number
                      :min="0"
                      :step="1"
                      placeholder="几"
                      v-model="form.privateHouseCount"
                    />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
              <a-form-model-item :colon="false" class="ant-row-flex" label="总共" prop="houseCount">
                <a-row type="flex">
                  <a-col flex="1">
                    <a-input-number :min="0" :step="1" placeholder="几" v-model="form.houseCount" />
                  </a-col>
                  <a-col>
                    <span class="yo-addon">套</span>
                  </a-col>
                </a-row>
              </a-form-model-item>
            </section>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="产权单位" prop="propertyUnit">
            <a-input placeholder="请输入产权单位" v-model="form.propertyUnit" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="负责人" prop="propertyUnitUser">
            <a-input placeholder="请输入负责人" v-model="form.propertyUnitUser" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="负责人电话" prop="propertyUnitUserTel">
            <a-input placeholder="请输入负责人电话" v-model="form.propertyUnitUserTel" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="联系人" prop="propertyUnitConent">
            <a-input placeholder="请输入联系人" v-model="form.propertyUnitConent" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="联系人电话" prop="propertyUnitConentTel">
            <a-input placeholder="请输入联系人电话" v-model="form.propertyUnitConentTel" />
          </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: {
        /* ... */
        propertyRights: [{ required: true, message: '请选择产权性质' }],
        straightHouseCount: [{ required: true, message: '请输入直管公房' }],
        selfHouseCount: [{ required: true, message: '请输入单位自管公房' }],
        otherCount: [{ required: true, message: '请输入其他' }],
        businessCount: [{ required: true, message: '请输入商品房' }],
        changeHouseCount: [{ required: true, message: '请输入房改房' }],
        resettlementHouseCount: [{ required: true, message: '请输入拆迁安置房' }],
      },

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

      /** 其他成员属性 */
      /* ... */
      codes: {
        housePropertyRights: [],
      },
    };
  },

  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_property_rights'],
        })
        .then(({ data: { dic_house_property_rights } }) => {
          this.codes = {
            housePropertyRights: dic_house_property_rights,
          };
        });
    },
  },
};
</script>