<template>
  <div>
    <el-button size="mini" type="primary" @click="onshow">新增联系人</el-button>
    <el-dialog
        :visible.sync="dialogFormVisible"
        width="900px"
        @close="dialogFormVisible = false"
    >
      <div slot="title" style="font-size: 15px">
        新增联系人信息
      </div>
      <div>
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules"  ref="form"  size="mini" label-position="right" label-width="90px">
            <el-col :span="20">
              <el-form-item  label="姓名:" prop="name">
                <el-input  autosize v-model="form.name" placeholder="请输入姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="手机号:" prop="phonenumber">
                <el-input  autosize v-model="form.phonenumber" placeholder="请输入手机号码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="邮箱:" :rules="[
              { pattern:/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '请输入有效的邮箱',trigger: 'blur' }
            ]">
                <el-input   autosize v-model="form.email" placeholder="请输入邮箱"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="部门:" >
                <el-input  autosize v-model="form.depname" placeholder="请输入部门"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="职位:" >
                <el-input  autosize v-model="form.position" placeholder="请输入职位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="性别:" >
                <el-radio v-model="form.sex" label="男">男</el-radio>
                <el-radio v-model="form.sex" label="女">女</el-radio>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="生日:" >
                <el-date-picker
                    v-model="form.birthday"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择日期"
                    style="width: 100%"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="家庭住址:" >
                <el-input type="textarea" autosize v-model="form.address" placeholder="请输入家庭住址"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="备注:" >
                <el-input type="textarea"  autosize v-model="form.remarks" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"   class="normal-btn-width btn-primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "add",
  props:["data"],
  data(){
    var validateMobilePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('手机号不可为空'));
      } else {
        if (value !== '') {
          var reg=/^1[3456789]\d{9}$/;
          if(!reg.test(value)){
            callback(new Error('请输入有效的手机号码'));
          }
        }
        callback();
      }
    };
    return {
      dialogFormVisible:false,
      visible:false,
      form:{
        contactsid: 0,
        sys_enterpriseid: "",
        name: "",
        sex: "",
        depname: "",
        position: "",
        birthday: "",
        phonenumber: "",
        email: "",
        address: "",
        remarks: "",
        enterprisename:'',
        province: "",
        city: "",
        county: "",
        workaddress: 0,
        isdefault: 1,
        isprimary: 1
      },
      rules:{
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
        ],
        phonenumber: [
          { required: true, validator: validateMobilePhone, trigger: 'blur' },
        ]
      },
    }
  },
  methods:{
    onshow(){
      this.dialogFormVisible = true

      this.form.sys_enterpriseid = this.data.sys_enterpriseid
      this.form.enterprisename = this.data.enterprisename
      console.log("***********//////////////")
      console.log(this.form)
    },
    onSubmit(){
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221018141802",
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit("insertSuccess")
          this.$refs['form'].resetFields()
          this.dialogFormVisible = false
        })
      })
    },
  }
}
</script>

<style scoped>

</style>