<template>
  <el-tabs class="my-tabs" v-model="activeName" type="card" @tab-click="handleClick">
    <el-tab-pane label="岗位列表" name="roleList">

      <div class="container">
        <div class="tab-header">
          <el-form ref="form" :inline="true" :model="search" label-width="80px">
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="岗位名称">
                  <el-input v-model="search.name" clearable></el-input>
                </el-form-item>
                <!-- <el-form-item label="所属部门">
                  <el-select v-model="search.orgSid" placeholder="请选择" clearable @clear="handleClear">
                    <el-option hidden :key="search.orgSid" :label="orgName" :value="search.orgSid"></el-option>
                    <el-tree v-loading="loading" :data="treedata" ref="Tree" show-checkbox accordion node-key="sid"
                             :default-checked-keys="checkedId"
                             :default-expand-all='true' :check-strictly='true' :props="defaultProps"
                             @check-change="checkchange"
                    >
                    </el-tree>
                  </el-select>
                </el-form-item> -->
                <el-button @click="getList(1)">查询</el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <!-- table -->
        <el-table :data="roleTable" border style="width: 100%;">
          <el-table-column label="序号" width="80px" type="index" align="center">
          </el-table-column>
          <el-table-column label="操作" width="220px" align="center">
            <template slot-scope="scope">
              <el-button @click="editRow(scope.row)" type="primary" size="mini"> 修改</el-button>
              <el-button @click="delRow(scope.row)" type="danger" size="mini"> 删除</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="岗位名称" align="center"></el-table-column>
          <el-table-column prop="parentName" label="上级岗位名称" align="center"></el-table-column>
          <el-table-column prop="postCode" label="岗位编码" align="center"></el-table-column>
          <el-table-column prop="postLevel" label="职级" align="center"></el-table-column>
          <!-- <el-table-column prop="orgName" label="所属部门" align="center"></el-table-column> -->
        </el-table>
        <pagination :total="page.total" :page.sync="page.current" :limit.sync="page.size" @pagination="pagination"/>

        <!-- 编辑岗位信息 -->
        <el-dialog :title="dialogTitle + '岗位信息'" :visible.sync="editDialog" width="40%">
          <table class="e-table" cellspacing="0">
            <tr>
              <td>上级岗位</td>
              <td>  <!-- @change="changeParentSid" -->
                <el-select v-model="roleForm.parentSid" filterable clearable placeholder="请选择"
                           style="width:300px">
                  <el-option v-for="item in parentSidList" :key="item.sid" :label="item.name"
                             :value="item.sid">
                  </el-option>
                </el-select>
              </td>
            </tr>
            <tr>
              <td>岗位名称</td>
              <td>
                <el-input v-model="roleForm.name" style="width:300px"></el-input>
              </td>
            </tr>
            <tr>
              <td>岗位职级</td>
              <td>
                <el-select v-model="roleForm.postLevel"  @change="changeUPpostLevel" filterable placeholder="请选择" style="width:300px">
                    <el-option v-for="item in postLevelList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey">
                    </el-option>
                </el-select>
              </td>
            </tr>
            <tr>
            <td>岗位编码</td>
              <td>
                <el-input v-model="roleForm.postCode" style="width:300px"></el-input>
              </td>
            </tr>
            <!--<tr>
              <td>备注</td>
              <td>
                <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="roleForm.remarks"></el-input>
              </td>
            </tr>-->
            <!-- <tr>
              <td>选择部门(单选)</td>
              <td>
                <el-tree
                  :data="treedata1"
                  show-checkbox
                  node-key="sid"
                  ref="Tree1"
                  :default-expand-all='true'
                  :check-strictly='true'
                  :default-checked-keys="checkedId1"
                  :props="defaultProps"
                  @check-change="checkchange1"
                >
                </el-tree>
              </td>
            </tr> -->
          </table>
          <div style="margin-top: 20px; text-align: center;">
            <el-button type="primary" @click="save()">保 存</el-button>
            <el-button @click="editDialog = false">关 闭</el-button>
          </div>
        </el-dialog>
      </div>
    </el-tab-pane>
    <!-- 新增岗位信息 -->
    <el-tab-pane label="新增岗位" name="addrole">
      <el-card class="box-card">
        <table class="e-table" cellspacing="0">
          <tr>
            <td>上级岗位</td>
            <td>  <!-- @change="changeParentSid" -->
              <el-select v-model="roleForm.parentSid" filterable clearable placeholder="请选择"
                         style="width:300px">
                <el-option v-for="item in parentSidList" :key="item.sid" :label="item.name"
                           :value="item.sid">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td>岗位名称</td>
            <td>
              <el-input v-model="roleForm.name" style="width:300px"></el-input>
            </td>
          </tr>
          <tr>
            <td>岗位职级</td>
            <td>
              <el-select v-model="roleForm.postLevel" @change="changePostLevel" filterable placeholder="请选择"
                         style="width:300px">
                <el-option v-for="item in postLevelList" :key="item.dictKey" :label="item.dictValue"
                           :value="item.dictKey">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td>岗位编码</td>
            <td>
              <el-input v-model="roleForm.postCode" style="width:300px"></el-input>
            </td>
          </tr>
          <!--<tr>
            <td>备注</td>
            <td> <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="roleForm.remarks"></el-input></td>
          </tr>-->
          <!-- <tr>
            <td>选择部门(单选)</td>
            <td>
              <el-tree
                :data="treedata2"
                show-checkbox
                node-key="sid"
                ref="Tree2"
                :default-expand-all='true'
                :check-strictly='true'
                :default-checked-keys="checkedId2"
                :props="defaultProps"
                @check-change="checkchange2"
              >
              </el-tree>
            </td>
          </tr> -->
        </table>
        <div style="margin-top: 20px; text-align: center;">
          <el-button type="primary" @click="save()">保 存</el-button>
        </div>
      </el-card>
    </el-tab-pane>
  </el-tabs>
</template>

<script>
import {delOrgroles, orgList, pageList, pullDown, putOrgroles, savePost,selectList} from '@/api/system/postManage/index.js'

export default {
  data() {
    return {
      dialogTitle: '',
      activeName: 'roleList',
      roleForm: {
        remarks: "",
        roleName: "",
        postCode: '',
        orgSid: '',
        postLevel: '',
        postLevelKey: '',
        parentSid:''
      },
      formBackup: {},
      search: {
        name: '',
        orgSid: ''
      },
      orgName: '',
      page: {
        total: 0, // 默认数据总数
          current: 1, // 默认开始页面
          size: 10, // 每页的数据条数
        },
        roleTable: [],
        editDialog: false,
        // 树形
        loading: false,
        defaultProps: {
          children: 'children',
          label: 'name'
        },
//      orgdata:[],           // 部门
      checkedId: [],
      checkedId1: [],
      checkedId2: [],
      treedata: [],
      treedata1: [],
      treedata2: [],
      postLevelList: [],
      parentSidList: [],
//      chace: [],
//      roleDialog: false,
//      Thisrow: {},
//      orgListAll:[]
    };
    },
    mounted() {
      this.formBackup = Object.assign({}, this.roleForm),
      this.getList()
      this.getOrgList()
      this.postLevel()
      this.getParent()
    },
    methods: {
      pagination(val) { // 分页函数
        this.page.current = val.pageNum
        this.page.size = val.pageSize
        this.getList()
      },
      // 分页列表
      getList(flag) {
        if (flag == '1') {
          this.page.current = 1
        }
        let params = this.page
        params.params = this.search
        pageList(params).then(res => {
          this.page.total = res.data.total
          this.roleTable = res.data.records
        })
      },
      postLevel(){
        pullDown({ psid: 0, type: 'postLevel' }).then((res)=>{
          if(res.code==='200'){
            this.postLevelList=res.data
          }
        })
      },
      getParent(){
        selectList().then((res)=>{
          if(res.code==='200'){
            this.parentSidList=res.data
          }
        })
      },
      changePostLevel(value){
        let bb = null
        this.postLevelList.forEach((e) => {
          if (e.dictKey == value) {
            bb = {
              name: e.dictValue,
              value: e.dictKey
            }
          }
        })
        this.roleForm.postLevel=bb.name
        this.roleForm.postLevelKey=bb.value
      },
      changeUPpostLevel(value){
        let bb = null
        this.postLevelList.forEach((e) => {
          if (e.dictKey == value) {
            bb = {
              name: e.dictValue,
              value: e.dictKey
            }
          }
        })
        this.roleForm.postLevel=bb.name
        this.roleForm.postLevelKey=bb.value
      },
      handleClick(tab, event) {
        if (tab.name == 'addrole') {
          this.dialogTitle = '新增'
          this.roleForm = Object.assign({}, this.formBackup)
        } else {
          this.getList()
        }
      },
      editRow(row) {
        this.dialogTitle = '编辑'
        this.editDialog = true
        console.log(row)
        this.roleForm = Object.assign({}, row)
        this.checkedId1 = [this.roleForm.orgSid]
      },
      delRow(row) {
        this.$confirm('确定要删除该岗位吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            sids: row.sid
          }
          delOrgroles(params).then(res => {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          })
        })
      },
      // 保存角色
      save() {
        if (this.roleForm.sid) {
          // let orgSid = this.$refs.Tree1.getCheckedKeys()
          // this.roleForm.orgSid = orgSid.toString()
          putOrgroles(this.roleForm).then(res => {
            if (res.code == '200') {
              this.getList()
              this.editDialog = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
            }
          })
        } else {
          // let orgSid = this.$refs.Tree2.getCheckedKeys()
          // this.roleForm.orgSid = orgSid.toString()
          savePost(this.roleForm).then(res => {
            if (res.code == '200') {
              this.getList()
              this.activeName = "roleList"
              this.$message({
                message: res.msg,
                type: 'success'
              })
              let arr = []
              // this.$refs.Tree2.setCheckedKeys(arr);
            }
          })
        }
        this.reset()
      },
      reset() {
        this.roleForm = {
          remarks: "",
          roleName: "",
          orgSid: ''
        };
        this.search = {
          name: '',
          orgSid: ''
        }
      },

      // 获取部门树形
      getOrgList(row) {
        this.roleDialog = true
        this.checkedId = []
//      this.roleForm.roleName = row.roleName
        this.Thisrow = row
        this.loading = true
        let params = {
//        roleSid: row.sid,
//        userType: '2'
        }

        orgList(params).then(res => {
          this.treedata = res.data
          this.treedata1 = res.data
          this.treedata2 = res.data
//        this.getTreeParentNode(res.data, this.checkedId)
//        this.getTreeParentid(res.data)
          this.loading = false
        })

      },
//    // 递归查询所有上级数据
//    getTreeParentNode(menus, ids) {
//      for (var i = 0; i < menus.length; i++) {
//        if (menus[i].children && menus[i].children.length != 0) {
//          this.getTreeParentNode(menus[i].children, ids)
//        } else if (menus[i].isCheck) {
//          ids.push(menus[i].sid)
//        }
//      }
//    },
//    // 所有一级菜单ID
//    getTreeParentid(menus) {
//      for (var i = 0; i < menus.length; i++) {
//        this.chace.push(menus[i].sid)
//      }
//    },
//    getCheckedKeys() {
//      let _this = this
//      let roleMenus = []
//      this.$refs.Tree.getCheckedKeys().forEach(val => {
//        roleMenus.push({
//          menuSid: val,
//          orgSid: _this.$store.getters.userInfo.orgSid,
//          roleSid: _this.Thisrow.sid
//        })
//      })
//      let parentTrue = this.$refs.Tree.getHalfCheckedKeys()
//      for (let i = 0; i < parentTrue.length; i++) {
//        for (let j = 0; j < this.chace.length; j++) {
//          if (parentTrue[i] == this.chace[j]) {
//            parentTrue.splice(i, 1);
//          }
//        }
//      }
//      parentTrue.forEach(val => {
//        roleMenus.push({
//          menuSid: val,
//          orgSid: _this.$store.getters.userInfo.orgSid,
//          roleSid: _this.Thisrow.sid
//        })
//      })
//      saveorgrolemenus({
//        roleSid: _this.Thisrow.sid,
//        roleMenus: roleMenus
//      }).then(res => {
//        this.roleDialog = false
//        this.$message({
//          message: res.msg,
//          type: 'success'
//        })
//      })
//    },
      checkchange(data, checked, indeterminate) {
        if (checked) {

          this.search.orgSid = data.sid
          this.orgName = data.name

          let arr = []
          arr = [data.sid];
          this.$refs.Tree.setCheckedKeys(arr);
          return;
        }
      },
      checkchange1(data, checked, indeterminate) {
        if (checked) {
          this.roleForm.orgSid = data.sid
//    		this.orgName = data.name
          let arr = []
          arr = [data.sid];
          this.$refs.Tree1.setCheckedKeys(arr);
          return;
        }
      },
      checkchange2(data, checked, indeterminate) {
        if (checked) {
          this.roleForm.orgSid = data.sid
//    		this.orgName = data.name
          let arr = []
          arr = [data.sid];
          this.$refs.Tree2.setCheckedKeys(arr);
          return;
        }
      },
      handleClear() {
        // 将选择器的值置空
        this.orgName = ''
        this.search.orgSid = ''
        let arr = []
        this.$refs.Tree.setCheckedKeys(arr);
      }
    }
  };
</script>

<style scoped>
  .el-select > .el-input {
    display: block;
    width: 300px;
  }

  /deep/ .tab-header .el-select {
    width: 180px;
  }

  .my-tabs {
    margin-top: 10px;
  }
</style>
