<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="680px" append-to-body @close="handleClose">
    <el-form ref="formRef" :model="localForm" :rules="rules" label-width="140px">
      <el-form-item label="所属中介">
        <el-input :value="ownerName" disabled />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="socialCreditCode">
        <el-input
          v-model="localForm.socialCreditCode"
          placeholder="请输入统一社会信用代码"
          maxlength="18"
          clearable
          :disabled="readonly"
        />
      </el-form-item>
      <el-form-item label="关联职务">
        <el-input
          v-model="localForm.relationPersonPost"
          placeholder="请输入关联职务"
          maxlength="100"
          clearable
          :disabled="readonly"
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="localForm.remark"
          type="textarea"
          :rows="3"
          maxlength="500"
          show-word-limit
          :disabled="readonly"
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button v-if="!readonly" type="primary" @click="handleSubmit">确 定</el-button>
      <el-button @click="handleClose">{{ readonly ? '关 闭' : '取 消' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "EnterpriseRelationEditDialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ""
    },
    form: {
      type: Object,
      default: () => ({})
    },
    ownerName: {
      type: String,
      default: ""
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      localForm: {},
      rules: {
        socialCreditCode: [{ required: true, message: "统一社会信用代码不能为空", trigger: "blur" }]
      }
    };
  },
  watch: {
    visible: {
      immediate: true,
      handler(val) {
        this.dialogVisible = val;
      }
    },
    dialogVisible(val) {
      this.$emit("update:visible", val);
    },
    form: {
      immediate: true,
      deep: true,
      handler(val) {
        this.localForm = { ...val };
      }
    }
  },
  methods: {
    handleSubmit() {
      if (this.readonly) {
        return;
      }
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$emit("submit", { ...this.localForm });
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$emit("close");
    }
  }
};
</script>
