<template>
  <el-dialog
    :visible.sync="visible"
    :title="title"
    :modal-append-to-body="false"
    :width="`${percentWidth}%`"
    @close="handleCancel"
  >
    <PublicForm
      ref="publicForm"
      :form-arr="formArr"
      :form-data="formData"
      @outOperation="outOperation"
    />
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        type="primary"
        @click="dialogOK"
      >保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import PublicForm from '@/components/form/index.vue';
export default {
  components: {
    PublicForm,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '',
    },
    formArr: {
      type: Array,
      required: true,
    },
    percentWidth: {
      type: Number,
      default: 30,
    },
    formData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      // 默认值
    };
  },
  methods: {
    handleCancel() {
      console.log('我是formArr', this.formArr);
      this.$emit('handleCancel');
    },
    outOperation(val, index, prop, num) {
      console.log('我是outOperation', val, index, prop, num);
    },
    dialogOK() {
      // 判断表单验证是否通过
      if (this.$refs.publicForm.submitForm()) {
        console.log('success');
        console.log(this.formData);
        if (this.title === '新增') {
          this.$emit('dialogOK', this.formData, 1);
        } else if (this.title === '编辑') {
          this.$emit('dialogOK', this.formData, 2);
        }
      } else {
        console.log('error');
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .titiebox .titieboxlabel {
  white-space: nowrap;
}
</style>
