<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="820px" append-to-body @close="handleClose">
    <el-form ref="formRef" :model="localForm" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属中介">
            <el-input :value="ownerName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="亲属关系" prop="personSubType">
            <el-select v-model="localForm.personSubType" placeholder="请选择亲属关系" clearable style="width: 100%" :disabled="readonly">
              <el-option
                v-for="item in filteredRelationOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="localForm.name" placeholder="请输入姓名" maxlength="100" clearable :disabled="readonly" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="证件号" prop="personId">
            <el-input v-model="localForm.personId" placeholder="请输入证件号码" maxlength="50" clearable :disabled="readonly" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="人员类型">
            <el-select v-model="localForm.personType" placeholder="请选择人员类型" clearable style="width: 100%" :disabled="readonly">
              <el-option
                v-for="item in indivTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="证件类型">
            <el-select v-model="localForm.idType" placeholder="请选择证件类型" clearable style="width: 100%" :disabled="readonly">
              <el-option
                v-for="item in certTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-select v-model="localForm.gender" placeholder="请选择性别" clearable style="width: 100%" :disabled="readonly">
              <el-option
                v-for="item in genderOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码">
            <el-input v-model="localForm.mobile" placeholder="请输入手机号码" maxlength="20" clearable :disabled="readonly" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="联系地址">
        <el-input v-model="localForm.contactAddress" placeholder="请输入联系地址" maxlength="200" clearable :disabled="readonly" />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所在公司">
            <el-input v-model="localForm.company" placeholder="请输入所在公司" maxlength="200" clearable :disabled="readonly" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="职位">
            <el-input v-model="localForm.position" placeholder="请输入职位" maxlength="100" clearable :disabled="readonly" />
          </el-form-item>
        </el-col>
      </el-row>
      <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: "RelativeEditDialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ""
    },
    form: {
      type: Object,
      default: () => ({})
    },
    ownerName: {
      type: String,
      default: ""
    },
    readonly: {
      type: Boolean,
      default: false
    },
    relationTypeOptions: {
      type: Array,
      default: () => []
    },
    indivTypeOptions: {
      type: Array,
      default: () => []
    },
    genderOptions: {
      type: Array,
      default: () => []
    },
    certTypeOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogVisible: false,
      localForm: {},
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        personId: [{ required: true, message: "证件号不能为空", trigger: "blur" }],
        personSubType: [{ required: true, message: "亲属关系不能为空", trigger: "change" }]
      }
    };
  },
  computed: {
    filteredRelationOptions() {
      return this.relationTypeOptions.filter(item => item.value !== "本人");
    }
  },
  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>
