<template>
  <div class="container normal-panel normal-margin">
    <el-button type="primary" size="small" icon="el-icon-plus" @click="onShow(dialogFormVisible = true)">新建联系人</el-button>
    <el-dialog title="新建联系人" :visible.sync="dialogFormVisible" width="50%">
      <el-row :gutter="20">
        <el-form :model="form" ref="form" label-position="left" label-width="80px" 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="家庭住址">
              <el-input v-model="form.homeaddress" autocomplete="on" placeholder="输入联系人家庭住址"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="微信">
              <el-input v-model="form.wechat" autocomplete="on" placeholder="输入联系人微信"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="单位">
              <el-input v-model="form.company" 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-select v-model="form.tag" style="width:100%" multiple>
                <el-option v-for="item in tags" :key="item.index" :value="item" :label="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="群组">
              <el-select v-model="form.sys_phonebookgroupid" style="width:100%">
                <el-option v-for="item in group" :key="item.index" :value="item.sys_phonebookgroupid" :label="item.groupname"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remarks" autocomplete="on" 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="addGroup()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  props:['group'],
  data () {
    return {
      dialogFormVisible:false,
      form:{
        "sys_phonebookid":0,     //sys_phonebookid<=0时 为新增
        "sys_phonebookgroupid":"",
        "name": "",
        "phonenumber": "",
        "company": "",
        "depname": "",
        "position": "",
        "email": "",
        "sex": "男",
        "birthday": '',
        "homeaddress": "",
        "remarks": "",
        "tag": []
      },
      tags:[
        '关键决策人','支持者','反对者'
      ]
    }
  },
  computed:{
    ...mapGetters({
      sys_options:'sys_options'
    })
  },
  methods:{
    onShow () {
    },
    addGroup () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false;
        const res = await this.$api.requested({
          "id": "20220831164503",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{

          this.$emit('onAddSuccess')
          this.refresh()
          this.dialogFormVisible = false

        })
      });
    },
    refresh () {
      this.form = {
        "sys_phonebookid":0,     //sys_phonebookid<=0时 为新增
        "sys_phonebookgroupid":"",
        "name": "",
        "phonenumber": "",
        "company": "",
        "depname": "",
        "position": "",
        "email": "",
        "sex": "男",
        "birthday": (new Date()).toLocaleDateString(),
        "homeaddress": "",
        "remarks": "",
        "tag": []
      }
    }
  },
  mounted () {
    this.$store.dispatch('optiontypeselect','sex')
  }
}

</script>
<style>
</style>