<!-- Dialog.vue -->
<template>
  <el-dialog
    :visible.sync="visible"
    @update:visible="handleUpdateVisible"
    :title="title + '组织机构'"
    :width="width"
    :before-close="beforeClose"
  >
    <MyForm
      :formData="formData"
      :labelPosition="'left'"
      :disabled="isDisabled"
      ref="myFormComponent"
    ></MyForm>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirm">保 存</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import MyForm from "@/components/MyForm";
export default {
  name: "StandardDialog",
  components: {
    MyForm,
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "弹窗标题",
    },
    width: {
      type: String,
      default: "50%",
    },
  },
  data() {
    return {
      visible: this.value,
      // 这是传递给表单组件的数据
      formData: [
        {
          fieldName: "name",
          label: "上级组织",
          type: "input",
          value: "",
          isRequired: true,
        },
        {
          fieldName: "name",
          label: "组织名称",
          type: "input",
          value: "",
          isRequired: true,
        },
        {
          fieldName: "age",
          label: "显示排序",
          type: "input_number",
          value: "",
          isRequired: true,
        },
        {
          fieldName: "age",
          label: "负责人",
          type: "input",
          value: "",
          isRequired: true,
        },
        {
          fieldName: "age",
          label: "联系电话",
          type: "input",
          value: "",
          isRequired: true,
        },
        {
          fieldName: "age",
          label: "邮箱",
          type: "input",
          value: "",
          isRequired: true,
        },
        {
          fieldName: "gender",
          label: "部门状态",
          type: "radio",
          value: '',
          isRequired: false,
          options: [
            { value: 1, label: "正常" },
            { value: 0, label: "停用" },
          ],
        },

        {
          fieldName: "age",
          label: "创建人",
          type: "input",
          value: "",
          isRequired: true,
        },
      ],
      isDisabled: false,
    };
  },
  watch: {
    value(newVal) {
      this.visible = newVal;
    },
    visible(newVal) {
      this.$emit("input", newVal);
    },
  },
  methods: {
    handleUpdateVisible(val) {
      this.visible = val;
    },
    beforeClose(done) {
      this.visible = false;
      this.$emit("before-close", done);
    },
    // 取消按钮处理方法
    handleCancel() {
      this.visible = false;
      this.$emit("cancel"); // 可以在父组件监听 'cancel' 事件
    },

    // 确定按钮处理方法
    handleConfirm() {
      this.$refs.myFormComponent.validateForm();

      this.visible = false;
      this.$emit("confirm"); // 可以在父组件监听 'confirm' 事件
    },
  },
};
</script>

<style scoped lang="scss">
/* 你可以根据需要为弹窗添加样式 */
</style>
