<template>
  <div>
    <el-button type="text" size="small" @click="onShow(dialogFormVisible = true)">编 辑</el-button>
    <el-dialog title="编辑联系人" append-to-body :visible.sync="dialogFormVisible" append-to-body width="50%">
      <el-row :gutter="20">
        <el-form :model="form" ref="form" label-position="right" label-width="90px" size="small">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name" :rules="[
              { required: true, message: '联系人姓名不能为空'},
            ]">
              <el-input v-model="form.name" autocomplete="on" placeholder="输入联系人姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="form.sex" style="width:100%">
                <el-option v-for="item in sys_options" :key="item.index" :value="item.value" :label="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber" :rules="[
              { required: true, message: '手机号码不能为空'},
              { pattern:/^1[3-9][0-9]\d{8}$/, message: '请输入正确手机号码',trigger: 'blur' }
            ]">
              <el-input v-model="form.phonenumber" autocomplete="on" placeholder="输入联系人手机号码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生日">
              <el-date-picker
                style="width:100%"
                v-model="form.birthday"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email" :rules="[
              { pattern:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: '请输入正确邮箱',trigger: 'blur' }
            ]">
              <el-input v-model="form.email" autocomplete="on" placeholder="输入联系人邮箱"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省市县" prop="value" :rules="[
          { required: true, message: '选择省市县', trigger: 'change' },
          ]">
              <el-cascader
                style="width:100%"
                v-model="form.value"
                :options="basicData.data().areaData"
                @change="cascaderChange" clearable>
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址">
              <el-input v-model="form.address" type="textarea" :rows="3" autocomplete="on" placeholder="输入地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门">
              <el-input v-model="form.depname" autocomplete="on" placeholder="输入联系人部门"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职位">
              <el-input v-model="form.position" autocomplete="on" placeholder="输入联系人职位"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否负责人">
              <el-checkbox v-model="form.isleader" :true-label="1" :false-label="0"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remarks" autocomplete="on" type="textarea" :rows="3" placeholder="输入联系人备注"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width:120px" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" style="width:120px" type="primary" @click="submitAddress()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  props:['mainData','row'],
  data () {
    return {
      dialogFormVisible:false,
      form:{
        "contactsid":0, 
        "sys_enterpriseid":null,
        "name":"",
        "sex":"男",
        "depname":"",
        "position":"",
        "isleader":1,
        "birthday":"",
        "phonenumber":"",
        "email":"",
        "province":"",
        "city":"",
        "county":"",
        "address":"",
        "remarks":"",
        "workaddress":0,
        "value":[]
      }
    }
  },
  computed:{
    ...mapGetters({
      sys_options:'sys_options'
    })
  },
  methods:{
    onShow () {
      this.$store.dispatch('optiontypeselect','sex')
      
      let arr = ['province','city','county']
      arr.forEach(e=>{
        this.row[e] !== '' ?this.form.value.push(this.row[e]):''
      })
      this.form = Object.assign({},this.form,this.row)
    },
    cascaderChange (val) {
      if (val.length === 1) 
        return this.form = Object.assign({},this.form,{province:val[0],city:'',county:''})
      this.form = Object.assign({},this.form,{province:val[0],city:val[1],county:val[2]})
      
    },
    submitAddress () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false;
        this.form.sys_enterpriseid = this.mainData.sys_enterpriseid
        const res = await this.$api.requested({
          "id": "20221009155703",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{

          this.$emit('onAddSuccess')
           this.$refs['form'].resetFields();
          this.dialogFormVisible = false

        })
      });
    }
  },
  mounted () {
  }
}

</script>
<style>
</style>