<style>
.addContacts .add-tit {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 20px;
}
.addContacts .add-tit p {
  display: flex;
  align-items: center;
  font-weight: bold;
  color: rgba(17, 17, 17, 1);
  font-size: 14px;
}
.addContacts .add-tit p span {
  display: inline-block;
  width: 6px;
  height: 6px;
  background: #409efe;
  border-radius: 50%;
  margin-right: 10px;
}
.addContacts .el-dialog__header {
  padding: 15px 20px;
  background: rgba(255, 255, 255, 1);
}
.addContacts .dialog-footer {
  text-align: center;
  background-color: rgba(248, 250, 251, 1);
  padding-bottom: 20px;
}
.addContacts .el-dialog__footer {
  padding: 0;
}
</style>
<template>
    <el-dialog @closed="closedDialog" :modal="false" :visible.sync="dialogTableVisible" :close-on-click-modal="false" class="addContacts">
        <div class="add-tit" slot="title">
            <p><span></span>新建联系人</p>
            <span icon="el-icon-close"></span>
        </div>
        <div class="form-box" >
            <el-form :model="form" ref="form" class="MyEditForm" :rules="rules">
                <el-row :gutter="30">
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="联系人" prop="ContactName">
                                <el-input placeholder="请输入联系人姓名" v-model="form.ContactName"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="电话" prop="ContactPhone">
                                <el-input placeholder="请输入联系电话" v-model="form.ContactPhone"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="职务" prop="ContactDuty">
                                <el-select v-model="form.ContactDuty" placeholder="请选择联系人职务">
                                    <el-option 
                                        v-for='item in zwList' 
                                        :label='item.Name' 
                                        :value='item.ID' 
                                        :key='item.ID'>
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="性别" prop="Sex">
                                <el-select v-model="form.Sex" placeholder="请选择联系人性别">
                                    <el-option label='保密' :value='0' :key='0'> </el-option>
                                    <el-option label='男' :value='1' :key='1'> </el-option>
                                    <el-option label='女' :value='2' :key='2'> </el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="是否决策人" prop="IsMain">
                                <el-select v-model="form.IsMain" placeholder="请选择是否决策人">
                                    <el-option label='否' :value='0' :key='0'> </el-option>
                                    <el-option label='是' :value='1' :key='1'> </el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8" v-if="!form.ID">
                        <div class="MyEditForm-item">
                            <el-form-item label="密码" prop="Password">
                              <el-input type="text" show-Password placeholder="请输入密码" v-model="form.Password"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <div class="MyEditForm-item label-pad-left">
                            <el-form-item label="备注">
                                <el-input type="textarea" placeholder="请输入备注" v-model="form.Remark"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" class="add-box-btn" @click="submitForm('form')">确 定</el-button>
            <el-button class="add-box-btn add-box-cancel" @click="dialogTableVisible = false, resetForm('form')">取 消</el-button>
        </div>
    </el-dialog>
</template>
<script>
export default {
  props: {
    ID: {
      type: Number,
      default: 0,
    },
    contactID: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      dialogTableVisible: true,
      rules: {
        IsMain: [
          { required: true, message: "请选择是否决策人", trigger: "change" },
        ],
        Sex: [
          { required: true, message: "请选择联系人性别", trigger: "change" },
        ],
        ContactDuty: [
          { required: true, message: "请选择联系人职务", trigger: "change" },
        ],
        ContactName: [
          { required: true, message: "请输入联系人姓名", trigger: "blur" },
        ],
        ContactPhone: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        Password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          // {
          //   pattern: /^(?=.*[a-zA-Z])(?=.*\d).{6,20}$/,
          //   message: '密码由6-20位字母、数字组成'
          // }
        ],
      },
      zwList: [],
      form: {
        CustomerId: 0,
        ContactName: "",
        ContactPhone: "",
        ContactDuty: "",
        Sex: 0,
        IsMain: 0,
        Remark: "",
        ID: 0,
        InfoID: 0,
        Password: '',
      },
    };
  },
  watch: {},
  mounted() {
    this.getZw();
    let $this = this;
    setTimeout(() => {
      $this.dialogTableVisible = true;
    }, 50);
    if(this.contactID) this.getInfo()
  },
  methods: {
    getZw() {
      this.apipost2(
        "app_today_visit_GetCustomerDutyEnum",
        {},
        (res) => {
          if (res.data.resultCode == 1) {
            this.zwList = res.data.data;
          }
        },
        (err) => {}
      );
    },
    getInfo() {
      this.form.ID = this.contactID
      this.apipost(
        "/api/Customer/GetAllContactModel",
        {
          Id: this.contactID
        },
        (res) => {
          if (res.data.resultCode == 1) {
            let data = res.data.data
            this.form = {
              CustomerId: data.CustomerId,
              ContactName: data.CustomerName,
              ContactPhone: data.ContactPhone,
              ContactDuty: data.ContactDuty,
              Sex: data.Sex,
              IsMain: data.IsMain,
              Remark: data.Remark,
              ID: data.ID,
              InfoID: data.InfoID,
              Password: '',
            }
          }
        }
      );
    },
    closedDialog() {
      this.MsgBus.$emit("closeaddContacts");
    },
    addData() {
      // this.form.CustomerId = this.ID
      this.form.InfoID = this.ID;
      this.apipost(
        "/api/Customer/CreateMyCustomerContact",
        this.form,
        (res) => {
          if (res.data.resultCode == 1) {
            this.$message.success("操作成功");
            this.resetForm("form");
            this.dialogTableVisible = false;
            this.$emit("addContactsOk");
          }
        }
      );
    },
    submitForm(addMsg) {
      //提交创建、修改表单
      this.$refs[addMsg].validate((valid) => {
        if (valid) {
          this.addData();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      // 重置表单
      this.form = {
        CustomerId: 0,
        ContactName: "",
        ContactPhone: "",
        ContactDuty: "",
        Sex: "",
        IsMain: "",
        Remark: "",
        ID: 0,
        Password: '',
      };
      this.$refs[formName].resetFields();
    },
  },
};
</script>
