<template>
<div>
  <el-button type="primary" size="small" icon="el-icon-plus" @click="onShow">新建员工</el-button>
  <el-drawer title="新建员工" direction="rtl" append-to-body :visible.sync="dialogFormVisible" size="800px" @close="$refs.form.clearValidate()" :show-close="false">
    <div class="drawer__panel">
      <el-row :gutter="20">
        <el-form :model="form" size="small" :rules="rules" ref="form" label-position="right" 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="部门:" prop="depname">
              <el-popover
                placement="bottom"
                width="400"
                trigger="click"
                v-model="visible">
                <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,visible=false)">选 择</el-button>
                    </span>
                  </span>
                </el-tree>
                <el-input slot="reference" v-model="form.depname" readonly placeholder="请选择" autocomplete="off"></el-input>
              </el-popover>
            </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 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="5" style="height:51px">
            <el-form-item>
              <el-checkbox v-model="form.isleader" :false-label="0" :true-label="1">是否部门负责人</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col class="mb-30" :span="5">
            <el-form-item>
              <el-checkbox v-model="form.isworker" :false-label="0" :true-label="1">是否服务人员</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <el-radio-group v-model="form.status">
                <el-radio :label="0">在职</el-radio>
                <el-radio :label="1">离职</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <div class="fixed__btn__panel">
      <el-button size="small" @click="dialogFormVisible = false,$refs.form.resetFields()" class="normal-btn-width">取 消</el-button>
      <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
    </div>
  </el-drawer>
<!--  <el-dialog
      title="授权角色"
      :visible.sync="drawer"
      width="564px"
      append-to-body
      :show-close="false"
      @close="onClose">
    <div>
      <el-row :gutter="50">
        <el-form :model="roleForm" :rules="roleRules" ref="roleForm"  size="mini" label-position="right">
          <el-col :span="24">
            <el-form-item label="授权角色" label-width="100px" prop="roleids">
              <el-select v-model="roleForm.roleids" style="width:100%" placeholder="请选择" multiple>
                <el-option
                    v-for="item in options"
                    :key="item.index"
                    :label="item.rolename"
                    :value="item.roleid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <div class="dialog-footer">
&lt;!&ndash;      <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>&ndash;&gt;
      <el-button size="small" type="warning" @click="createUser" 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'
    })
  },
  watch: {
    dialogFormVisible(val) {
      !val && this.$refs.form.resetFields()
    }
  },
  data () {
    return {
      dialogFormVisible:false,
      visible:false,
      rules:{
        hrcode: [
          { required: true, message: '请输入员工编码', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
        ],
        depname: [
          { required: true, message: '请选择部门', trigger: 'change' },
        ],
        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":"",
        "isworker":"",
        "userid":"0",
        'status':0
      },
      value:[],
      drawer:false,
      roleRules:{
        roleids: [
          { required: true, message: '请选择授权角色', trigger: 'change' }
        ],
      },
      roleForm:{
        "hrid":'',
        "userid": 0,
        "name": "",
        "phonenumber": "",
        "status": 1,
        "remarks":"",
        "roleids":[]
      },
      options:[]
    }
  },
  methods:{
    onShow () {
      this.dialogFormVisible = true
      this.form.status = 0
      // 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.drawer = true*/
          this.roleForm.hrid = res.data.hrid
          /*this.rolelist()*/
        this.dialogFormVisible = false
        this.refreshData()
        this.$emit('onSuccess')
        this.$store.dispatch('changeDetailDrawer',true)
        this.$router.push({path:'/depManage/staffDetail',query:{id:res.data.hrid,rowindex:res.data.rowindex}})

      });
    },
    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
    },
    async rolelist () {
      let param = {
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "typename": "role",
          "parameter": {
          }
        }
      }
      const res = await this.$api.requested(param)
      this.options = res.data
    },

    createUser () {
      this.$refs['roleForm'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "classname": "webmanage.hr.hr",
          "method": "createUser",
          "content": {
            "hrid":this.roleForm.hrid,
            "roleids":this.roleForm.roleids
          }
        })
        this.tool.showMessage(res,()=>{
          this.drawer = false
          this.dialogFormVisible = false
          this.refreshData()
          this.$emit('onSuccess')
        })

      })
    },
    onClose () {
      this.$refs['roleForm'].resetFields()
    }
  }
}

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