<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.drawingMaterial">
            <a-checkbox-group v-model="form.houseInfo.drawingMaterial">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.houseStorageOfDrawings"
              >{{ item.value }}</a-checkbox>
              <a-input
                class="w-300"
                placeholder="请输入其他图纸资料存档处"
                v-if="form.houseInfo.drawingMaterial && form.houseInfo.drawingMaterial.indexOf(codes.houseStorageOfDrawings[codes.houseStorageOfDrawings.length - 1].code) >= 0"
                v-model="form.houseInfo.drawingMaterialText"
              />
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-spin>
  </a-form-model>
</template>
<script>
/* 表单内容默认值 */
const defaultForm = {
  /* ... */
  houseInfo: {
    drawingMaterial: null,
    drawingMaterialText: null,
  },
};

export default {
  props: ['param'],

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

      /** 表单数据 */
      form: {
        ...defaultForm,
      },
      /** 验证格式 */
      rules: {
        /* ... */
        'houseInfo.drawingMaterial': [{ required: true, message: '请选择图纸资料存档处' }],
      },

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

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

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

  methods: {
    /**
     * 必要的方法
     * 在打开编辑页时允许填充数据
     */
    async onFillData() {
      this.loading = true;
      {
        await this.onLoadCodes();
        /** 将默认数据覆盖到form */
        const record = this.param && this.param.record;
        record.houseInfo.drawingMaterial = record.houseInfo.drawingMaterial
          ? record.houseInfo.drawingMaterial.split(',')
          : ['1'];

        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);

            /** 验证通过后可以对数据进行转换得到想要提交的格式 */
            record.houseInfo.drawingMaterial = this.form.houseInfo.drawingMaterial.join(',');
            /* ... */

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

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

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

    /** 当前组件的其他方法 */
    /* ... */
    onLoadCodes() {
      return this.$api
        .sysDictTypeDropDowns({
          code: ['dic_house_storage_of_drawings'],
        })
        .then(({ data: { dic_house_storage_of_drawings } }) => {
          this.codes = {
            houseStorageOfDrawings: dic_house_storage_of_drawings,
          };
        });
    },
  },
};
</script>