<template>
<div>
  <el-button type="primary" size="small" icon="el-icon-plus" @click="onShow">新建员工</el-button>
  <el-dialog title="新建员工" :visible.sync="dialogFormVisible" @close="$refs.form.clearValidate()">
    <el-row :gutter="20">
      <el-form :model="form" size="small" :rules="rules" ref="form" label-position="left" label-width="100px">
        <el-col class="mb-30" :span="12">
          <el-form-item label="编号:" prop="hrcode">
            <el-input v-model="form.hrcode" placeholder="输入人员编号" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="性别">
            <el-select v-model="form.sex" style="width:100%" placeholder="请选择">
              <el-option
                v-for="item in sys_options"
                :key="item.index"
                :label="item.value"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="姓名:" prop="name">
            <el-input v-model="form.name" placeholder="输入姓名" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="电子邮箱:" prop="email">
            <el-input v-model="form.email" placeholder="输入电子邮箱" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="手机号码:" prop="phonenumber">
            <el-input v-model="form.phonenumber" placeholder="输入手机号码" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="身份证号:">
            <el-input v-model="form.idcard" placeholder="输入身份证号" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="部门:">
            <el-input v-model="form.depname" readonly placeholder="请选择" suffix-icon="el-icon-arrow-down" @focus="showTree = true" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="办公电话:">
            <el-input v-model="form.officetelephone" placeholder="输入办公电话" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="showTree">
          <el-form-item label="选择部门:">
            <div style="border:1px solid #f1f2f3">
              <el-tree
                :data="deplist"
                node-key="id"
                default-expand-all
                highlight-current
                :expand-on-click-node="false">
                <span class="custom-tree-node" slot-scope="{ node, data }">
                  <span>{{ node.label }}</span>
                  <span>
                    <el-button size="small" type="text" @click="selectDep(data)">选 择</el-button>
                  </span>
                </span>
              </el-tree>
            </div>
          </el-form-item>
        </el-col>
        
        <el-col class="mb-30" :span="12">
          <el-form-item label="职位:">
            <el-input v-model="form.position" placeholder="输入职位" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="12">
          <el-form-item label="汇报对象:">
            <selectMenber @selectMenber="selectMenber"></selectMenber>
          </el-form-item>
        </el-col>
        <el-col class="mb-30" :span="24">
          <el-form-item>
            <el-checkbox v-model="form.isleader" :false-label="0" :true-label="1">是否部门负责人</el-checkbox>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div class="dialog-footer">
      <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
      <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {mapGetters} from 'vuex'
import selectMenber from '../../../../../components/selectMenber/index.vue'
export default {
  props:['depInfo'],
  components:{
    selectMenber
  },
  computed:{
    ...mapGetters({
      deplist:'deplist',
      sys_options:'sys_options'
    })
  },
  data () {
    return {
      dialogFormVisible:false,
      showTree:false,
      rules:{
        hrcode: [
          { required: true, message: '请输入员工编码', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
        ],
        phonenumber: [
          { required: true, message: '请输入员工电话', trigger: 'blur' },
          { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'blur' }
        ],
        email:[
          { pattern:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: '请输入正确邮箱',trigger: 'blur' }
        ],
      },
      form:{
        "hrid":0,
        "hrcode":"",
        "name":"",
        "sex":'男',
        "phonenumber":"",
        "officetelephone":"",
        "idcard":"",
        "email":"",
        "birthday":"",
        "remarks":"",
        "departmentid":"",
        "isleader":"",
        "position":"",
        "reporthrid":"",
        "userid":"0"
      },
      value:[]
    }
  },
  methods:{
    onShow () {
      this.dialogFormVisible = true
      this.form.depname = this.depInfo.data.label
      this.form.departmentid = this.depInfo.data.departmentid
      this.$store.dispatch('optiontypeselect','sex')
    },
    selectDep  (data) {
      let obj = {
        depname:data.label,
        departmentid:data.departmentid
      }
      this.form = Object.assign({},this.form,obj)
      this.showTree = !this.showTree
    },
    onSubmit () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid)  return false;
        const res = await this.$api.requested({
          "classname": "webmanage.hr.hr",
          "method": "insertormodify_hr",
          "content": this.form
        })
        if (res.code === 0)  return this.$notify({
            title: '失败',
            message: res.data,
            type: 'error'
          });

          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success'
          });

          this.dialogFormVisible = false
          this.refreshData()
          this.$emit('onSuccess')
      });
    },
    refreshData () {
      this.form = {
        "hrid":0,
        "hrcode":"",
        "name":"",
        "sex":'男',
        "phonenumber":"",
        "officetelephone":"",
        "idcard":"",
        "email":"",
        "birthday":"",
        "remarks":"",
        "departmentid":"",
        "isleader":"",
        "position":"",
        "reporthrid":"",
        "userid":"0"
      }
    },
    // 监听汇报对象选择
    selectMenber (data) {
      this.form.reporthrid = data.hrid
    }
  }
}

</script>
<style>
</style>
<style scoped>
.mb-30{
  margin-bottom:12px
}
</style>