<template>
  <div>
    <el-button size="mini" type="text" @click="onshow" :disabled="!tool.checkAuth($route.name,'contacts')">编 辑</el-button>
    <el-drawer
        title="编辑联系人"
        :visible.sync="dialogFormVisible"
        size="600px"
        direction="rtl"
        :show-close="false"
        append-to-body
        @close="onClose">
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules"  ref="form"  size="mini" label-position="right" label-width="75px">
            <el-col :span="24">
              <el-form-item  label="姓名:" prop="name">
                <el-input   autosize v-model="form.name" placeholder="请输入姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="手机号:" prop="phonenumber">
                <el-input   autosize v-model="form.phonenumber" placeholder="请输入手机号码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="邮箱:" :rules="[
                { pattern:/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '请输入有效的邮箱',trigger: 'blur' }
              ]" prop="email">
                <el-input   autosize v-model="form.email" placeholder="请输入邮箱"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="单位:" prop="enterprisename">
                <el-input  autosize v-model="dataOld.enterprisename" placeholder="请输入部门" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="部门:" prop="depname">
                <el-input  autosize v-model="form.depname" placeholder="请输入部门"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="职位:" prop="position">
                <el-input  autosize v-model="form.position" placeholder="请输入职位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="性别:" prop="sex">
                <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="24">
              <el-form-item  label="生日:" prop="birthday">
                <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="24">
              <el-form-item  label="家庭住址:" prop="address">
                <el-input type="textarea" rows="5" v-model="form.address" placeholder="请输入家庭住址"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="备注:" prop="remarks">
                <el-input type="textarea"  rows="5" v-model="form.remarks" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="onClose" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "edit",
  props:["data","dataOld"],
  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,
      form:{},
      rules:{
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur,change' },
        ],
        phonenumber: [
          { required: true, validator: validateMobilePhone, trigger: 'blur' },
        ]
      }
    }
  },
  methods:{
    onshow(){
      if (this.dataOld.enterprisename === '' || this.dataOld.abbreviation === '' || this.dataOld.contact === ''){
        this.$message.error('请先将企业基础信息补充完整，再进行此操作');
      }else {
        this.dialogFormVisible = true
        this.form = Object.assign({},this.form,this.data)
      }
    },
    onSubmit(){
      this.form.sys_enterpriseid = this.dataOld.sys_enterpriseid
      this.form.enterprisename = this.dataOld.enterprisename
      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("editSuccess")
          this.$refs.form.resetFields()
          this.dialogFormVisible = false
        })
      })
    },
    onClose(){
      this.dialogFormVisible = false
      this.$refs.form.resetFields()
    }
  },
  mounted() {
  }
}
</script>

<style scoped>

</style>