<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>{{name}}</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="InfoID">
                                <el-select v-model="form.InfoID" placeholder="请选择所属客户">
                                    <el-option 
                                        v-for='item in CustomerList' 
                                        :key="item.CustomerId"
                                        :label="item.CustomerName"
                                        :value="item.CustomerId">
                                    </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="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-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">
                        <div class="MyEditForm-item label-pad-left">
                            <el-form-item label="下次联系时间">
                                <el-date-picker
                                    :clearable="false"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    v-model="form.NextContactTime"
                                    type="date"
                                    placeholder="选择日期">
                                </el-date-picker>
                            </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", "name"],
  data() {
    let checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      dialogTableVisible: true,
      rules: {
        IsMain: [
          { required: true, message: "请选择是否决策人", trigger: "change" },
        ],
        InfoID: [{ 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, validator: checkPhone, trigger: "blur" },
        ],
      },
      zwList: [],
      CustomerList: [],
      form: {
        InfoID: "",
        ContactName: "",
        ContactPhone: "",
        ContactDuty: "",
        Sex: 0,
        IsMain: 0,
        Remark: "",
        ID: 0,
        NextContactTime: "",
      },
    };
  },
  watch: {},
  mounted() {
    this.getZw();
    let $this = this;
    setTimeout(() => {
      $this.dialogTableVisible = true;
    }, 50);
    this.getCustomerList();
    if (this.ID !== 0) {
      this.apipost(
        "/api/Customer/GetAllContactModel",
        { Id: this.ID },
        (res) => {
          if (res.data.resultCode == 1) {
            let data = res.data.data;
            this.form = data;
            this.loading = true;
          }
        }
      );
    }
  },
  methods: {
    getCustomerList() {
      this.apipost("/api/Customer/GetCustomerListByCreateInfo", {}, (res) => {
        if (res.data.resultCode == 1) {
          this.CustomerList = res.data.data;
        }
      });
    },
    getZw() {
      this.apipost2(
        "app_today_visit_GetCustomerDutyEnum",
        {},
        (res) => {
          if (res.data.resultCode == 1) {
            this.zwList = res.data.data;
          }
        },
        (err) => {}
      );
    },
    closedDialog() {
      this.MsgBus.$emit("closeaddContacts");
    },
    addData() {
      this.form.ID = 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("getList");
          }
        }
      );
    },
    submitForm(addMsg) {
      //提交创建、修改表单
      this.$refs[addMsg].validate((valid) => {
        if (valid) {
          this.addData();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      // 重置表单
      this.form = {
        InfoID: "",
        ContactName: "",
        ContactPhone: "",
        ContactDuty: "",
        Sex: 0,
        IsMain: 0,
        Remark: "",
        ID: 0,
        NextContactTime: "",
      };
      this.$refs[formName].resetFields();
    },
  },
};
</script>
