<template>
<div>
  <el-button type="default" size="mini" @click="onShow">编 辑</el-button>
  <el-drawer title="编辑员工" direction="rtl" append-to-body :visible.sync="dialogFormVisible" size="800px" @close="$refs.form.clearValidate()">
    <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="12">
            <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="12">
            <el-form-item>
              <el-checkbox v-model="form.isworker" :false-label="0" :true-label="1">是否服务人员</el-checkbox>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <div class="fixed__btn__panel">
      <el-button size="small" @click="dialogFormVisible = false" 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>
import {mapGetters} from 'vuex'
import selectMenber from '@/components/selectMenber/index.vue'
export default {
  components:{
    selectMenber
  },
  computed:{
    ...mapGetters({
      sys_options:'sys_options'
    })
  },
  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":0,
        "phonenumber":"",
        "officetelephone":"",
        "idcard":"",
        "email":"",
        "birthday":"",
        "remarks":"",
        "departmentid":"",
        "isleader":"",
        "position":"",
        "reporthrid":"",
        "userid":"0"
      },
      value:[],
      deplist:[]
    }
  },
  methods:{
    async query_hrMain () {
      const res = await this.$api.requested({
        "classname": "webmanage.hr.hr",
        "method": "query_hrMain",
        "content": {
            "hrid":this.$route.query.id
        }
      })
      this.form = Object.assign({},this.form, res.data)
    },
    async department () {
      const res = await this.$api.requested({
        "classname": "webmanage.department.department",
        "method": "querydepartment",
        "content": {
        }
      })
      // 数据格式转换成elementui-tree所需的格式
      this.deplist = this.createMenu(res.data)
    },
    createMenu (array) {
      var that = this
      let arr = []
      function convertToElementTree(node) {
        // 新节点
        var elNode = {
          label: node["depname"],
          parentid:node['parentid'],
          departmentid:node["departmentid"],
          value:node["departmentid"],
          remarks:node["remarks"],
          isused:node["isused"],
          changedate:node['changedate'],
          changeby:node['changeby'],
          createdate:node['createdate'],
          createby:node['createby'],
          depno:node['depno'],
          children: []
        }
          
        if (node.subdep && node.subdep.length > 0) {
          // 如果存在子节点
          for (var index = 0; index < node.subdep.length; index++) {
          // 遍历子节点, 把每个子节点看做一颗独立的树, 传入递归构造子树, 并把结果放回到新node的children中
          elNode.children.push(convertToElementTree(node.subdep[index]));
          }
        }
        return elNode;
      }
      array.forEach((element) => {
        arr.push(convertToElementTree(element))
      });
      return arr
    },
    onShow () {
      this.query_hrMain()
      this.department()
      this.$store.dispatch('optiontypeselect','sex')
      this.dialogFormVisible = true
    },
    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.$emit('onSuccess')
      });
    },
    // 监听汇报对象选择
    selectMenber (data) {
      this.form.reporthrid = data.hrid
    }
  }
}

</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
  background: none;
}
</style>
<style scoped>
.mb-30{
  margin-bottom:12px
}
</style>