<template>
  <cms-modal
    v-model="visible"
    @onCancel="onCancel"
    @close="onCancel"
    :closable="false"
    :footer="footer"
    :modal-height="formHeight"
    :modal-width="formWidth"
    :modal-title="formTitle"
    :show-footer="showFooter"
    :modal-config="modalConfig"
    :modal-callback="modalCallback"
  >
    <cms-edit-form
      v-bind="formConfig"
      ref="formRef"
      :label-width="labelWidth"
      :form-model="formModel"
      :form-rules="formRules"
      :form-type="formType"
      v-on="listeners">
      <slot></slot>
    </cms-edit-form>
    <template slot="footer">
      <slot name="buttons">
        <cms-button
          type="primary"
          v-if="showSubmit && !(formType === 'view' || formType === 'approval')"
          @click="onSubmit">
          <span>{{ $t("common.button.submit") }}</span>
        </cms-button>
        <cms-button
          @click="onSave"
          v-if="!(formType === 'view' || formType === 'approval')"
          type="primary">
          <span>{{ $t("common.button.save") }}</span>
        </cms-button>
        <cms-button type="info" @click="onBack">
          <span>{{ $t("common.button.back") }}</span>
        </cms-button>
      </slot>
    </template>
  </cms-modal>
</template>

<script>
import { EditFormMixin, CmsDialogFormMixin } from "./edit-form-mixin";

export default {
  name: "CmsDialogForm",
  mixins: [ EditFormMixin, CmsDialogFormMixin ],
  data() {
    return {
      dialogStyle: {},
    }
  },
  created() {
    console.log('=========this.modalConfig', this.modalConfig);
    if (this.initFun)
      this.initFun();
  },
  methods: {
    /** 提交 */
    onSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          this.$emit("submit", this.formModel);
        } else {
          return false;
        }
      });
    },
    /** 执行保存 */
    onSave() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          this.$emit("save", this.formModel);
        } else {
          return false;
        }
      });
    },
    /** 重置表单 */
    resetFields() {
      this.$refs['formRef'].resetFields();
    },
    validateField(props = [], callback) {
      this.$refs['formRef'].validateField(props, (valid) => {
        if (callback) callback(valid);
        return false
      });
    },
    clearValidate(props = []) {
      this.$refs['editForm'].clearValidate(props);
    },
    /** 执行返回 */
    onBack() {
      this.$emit("back", false, this.formModel);
    },
    onCancel() {
      this.$emit("back", false);
    },
    onFullScreen() {
      // this.$messageTips.success(window.innerHeight);
      this.dialogStyle = {
        top: 0,
        height: window.innerHeight,
        width: window.innerWidth
      }
    }
  }
};
</script>

<style scoped lang="less">
.edit-form-main {
  padding: 20px 0 0 0;
}

.edit-form-page {
  padding: 20px 20px 0 0 !important;
  flex-direction: column;
}
</style>
