<style>

</style>
<template>
  <div class="flexOne">
    <div class="query-box">
      <ul>
        <li>
          <!-- 岗位名称 -->
          <span><em>{{$t('adm.adm_gwname')}}</em><el-input class='w210' v-model='msg.PostName'
                                       @keyup.enter.native="getList"></el-input></span>
        </li>
        <li>
          <input type="button" class="hollowFixedBtn" :value="$t('pub.searchBtn')" @click="resetPageIndex(),getList()"/>
          <input type="button" @click="outerVisible = true,dialogTitle='新增岗位',layerShowIndex=1,getCompany()"
                 class="normalBtn" :value="$t('pub.addBtn')"/>
        </li>
      </ul>
    </div>
    <table class="singeRowTable" border="0" cellspacing="0" cellpadding="0" v-loading='loading'>
      <tr>
        <th>{{$t('adm.adm_gwname')}}</th>
        <th>{{$t('adm.adm_gxtime')}}</th>
        <th width="250">{{$t('system.table_operation')}}</th>
      </tr>
      <tr v-for="(item,index) in DataList">
        <td>{{item.PostName}}</td>
        <td>{{item.UpdateTime}}</td>
        <td>
          <el-button-group>
            <el-tooltip class="item" effect="dark" :content="$t('pub.updateMsg')" placement="top-start">
              <el-button
                @click="outerVisible = true,dialogTitle='修改岗位信息',getCompany(),getDerpartMent(),updateData(index,item.PostId)"
                type="primary" icon="el-icon-edit"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="ERP角色管理" placement="top-start">
              <el-button @click="roleAllocation(item.PostId,1)" type="primary" icon="iconfont icon-user11"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="APP角色管理" placement="top-start">
              <el-button @click="roleAllocation(item.PostId,2)" type="primary" icon="iconfont icon-user11"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" :content="$t('system.table_delete')" placement="top-start">
              <el-button @click="deleteData(item.PostId)" type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </el-button-group>
        </td>
      </tr>
    </table>
    <el-pagination background @current-change="handleCurrentChange" :current-page.sync="currentPage"
                   layout="total,prev, pager, next, jumper" :page-size=msg.pageSize :total=total>
    </el-pagination>
    <el-dialog custom-class='w550' :title="dialogTitle" :visible.sync="outerVisible" center
               :before-close="closeChangeMachie">
      <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="120px">
        <div v-if="this.layerShowIndex==1">
          <el-form-item :label="$t('adm.adm_gwname')" prop="PostName">
            <el-input class='w300' type="text" v-model="addMsg.PostName" :placeholder="$t('pub.pleaseImport')"></el-input>
          </el-form-item>
          <el-form-item :label="$t('adm.adm_ynyyfenqu')" prop="IsUseSubarea">
            <el-select v-model="addMsg.IsUseSubarea" :placeholder="$t('pub.pleaseSel')">
              <el-option :label="$t('pub.yes')" value="1"></el-option>
              <el-option :label="$t('pub.no')" value="2"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div v-if="this.layerShowIndex==2">
          <el-transfer filterable v-model="postRoleListId" :data="groupRoleList" :props="{key: 'Id',label: 'Name'}" :titles="['可选角色', '已选角色']"></el-transfer>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <button class="hollowFixedBtn" @click="outerVisible = false,resetForm('addMsg')">{{$t('pub.cancelBtn')}}</button> &nbsp;
        <button v-if="this.layerShowIndex==1" class="normalBtn" type="primary" @click="submitForm('addMsg')">{{$t('pub.saveBtn')}}</button>
        <button v-if="this.layerShowIndex==2" class="normalBtn" type="primary" @click="updateRole()">{{$t('pub.saveBtn')}}</button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    data() { //穿梭
      return {
        loading: true,
        total: 0,
        pageSize: '',
        currentPage: 1,
        dialogTitle: '',
        insideDialogTxt: '',
        outerVisible: false,
        layerShowIndex: -1,
        DataList: [],
        groupRoleList: [],
        updatePostId: -1,
        postRoleListId: [],
        form: {},
        //请求数据
        msg: {
          pageIndex: 1,
          pageSize: 15,
          RB_Group_Id: '0',
          RB_Branch_Id: '-1',
          RB_Department_Id: '0',
          PostName: ''
        },
        companyMsg: {
          Status: 0,
        },
        rules: {
          PostName: [{required: true,message: this.$t('adm.adm_qsrgwname'),trigger: 'blur'}],
          IsUseSubarea: [{required: true,message: this.$t('adm.adm_qxzsfyyfenqu'),trigger: 'blur'}]
        },
        addMsg: {
          PostId: '-1',
          PostName: '',
          RB_Group_Id: '',
          RB_Branch_Id: '-1',
          RB_Department_Id: '-1',
          IsUseSubarea: '1'
        },
        getCompanyMsg: {
          RB_Group_Id: '0',
          Status: '0',
        },
        getDepartmentMsg: {
          RB_Group_Id: '0',
          RB_Branch_Id: '-1',
          Status: '0',
        },
        roleMsg: {
          PostId: 1
        },
        //返回数据
        layerCompanyList: [],
        companyList: [],
        layerDepartMentList: [],
        departMentList: [],
        platform: 1
      }
    },
    mounted() {
      let userInfo = this.getLocalStorage();
      this.msg.RB_Group_Id = this.getDepartmentMsg.RB_Group_Id = this.getCompanyMsg.RB_Group_Id = this.addMsg.RB_Group_Id = userInfo.RB_Group_id; //集团
      this.getList();
      this.getCompany();
      this.getDerpartMent();
    },
    methods: {
      getCompany() {
        this.apipost('admin_get_BranchGetList', this.getCompanyMsg, res => {
          if (res.data.resultCode == 1) {
            //console.log(res.data.data)
            if (this.outerVisible) {
              this.layerCompanyList = res.data.data;
            } else {
              this.companyList = res.data.data;
            }
            //console.log(this.layerCompanyList)
          } else {
          }
        }, err => {
        })
      },
      getDerpartMent() { //获取部门
        this.apipost('admin_get_DepartmentGetList', this.getDepartmentMsg, res => {
          if (res.data.resultCode == 1) {
            if (this.outerVisible) {
              this.layerDepartMentList = res.data.data;
            } else {
              this.departMentList = res.data.data;
            }
            //console.log(this.departMentList)
          } else {
          }
        }, err => {
        })
      },
      getList() { //获取数据
        this.loading = true,
          this.apipost('admin_get_PostGetPageList', this.msg, res => {
            if (res.data.resultCode == 1) {
              this.DataList = res.data.data.pageData
              this.total = res.data.data.count
              this.loading = false
              //	console.log(this.DataList)
            } else {
            }
          }, err => {
          })
      },
      addData() { //新增数据
        this.apipost('admin_post_PostSet', this.addMsg, res => {
          if (res.data.resultCode == 1) {
            this.$message.success(res.data.message)
            this.outerVisible = false;
            this.getList();
            this.resetForm('addMsg');
          } else {
            this.$message.error(res.data.message)
          }

        }, err => {
          console.log(err)
        })

      },
      updateData(index, id) {
        this.addMsg.PostId = id
        this.apipost('admin_get_PostGet', {PostId: id}, res => {
          if (res.data.resultCode == 1) {
            this.addMsg = res.data.data
            this.addMsg.IsUseSubarea = this.addMsg.IsUseSubarea.toString();
          } else {
          }
        }, err => {
        })
        this.layerShowIndex = 1;

      },
      roleAllocation(id, platform) { //角色管理
        this.platform = platform;
        this.groupRoleList = [];
        this.postRoleListId = [];
        this.dialogTitle = '角色分配';
        this.updatePostId = id;
        this.layerShowIndex = 2;
        this.outerVisible = true;
          this.apipost('admin_get_PostGetPostRole', {PostId: id, Platform: platform}, res => {
          if (res.data.resultCode == 1) {
            this.groupRoleList = res.data.data.groupRoleList;
            res.data.data.postRoleList.forEach(i => {
              this.postRoleListId.push(i.Id)
            });
          } else {
          }
        }, err => {
        })
      },
      updateRole() { //修改岗位角色
        let roleIdsString = this.postRoleListId.join(',');
        this.apipost('admin_post_PostUpdatePostRole', {
          PostId: this.updatePostId,
          RoleIds: roleIdsString,
          Platform:this.platform
        }, res => {
          this.insideDialogTxt = res.data.message
          if (res.data.resultCode == 1) {
            this.$message.success(res.data.message)
            this.outerVisible = false
          } else {
            this.$message.error(res.data.message)
          }
        }, err => {
        })
      },
      deleteData(id) {
        this.$confirm('是否删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.apipost('admin_post_PostRemove', {
            PostId: id
          }, res => {
            if (res.data.resultCode == 1) {
              this.getList()
            } else {
            }
          }, err => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      linkageDepartment() { //联动部门
        this.msg.RB_Department_Id = '0';
        this.getDepartmentMsg.RB_Branch_Id = this.msg.RB_Branch_Id;
        this.getDerpartMent();
      },
      layerLinkageDepartment() { //弹窗联动部门
        this.addMsg.RB_Department_Id = '';
        this.getDepartmentMsg.RB_Branch_Id = this.addMsg.RB_Branch_Id;
        this.getDerpartMent();
      },
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      //			clearMessage(){
      //				this.addMsg.RB_Branch_Id='';
      //				this.addMsg.RB_Department_Id='';
      //				this.addMsg.PostName='';
      //			},
      resetPageIndex() {
        this.msg.pageIndex = 1;
        this.currentPage = 1
      },
      submitForm(addMsg) { //提交创建、修改表单
        this.$refs[addMsg].validate((valid) => {
          if (valid) {
            this.addData()
          } else {
            return false;
          }
        });
      },
      closeChangeMachie(done) { //弹出框关闭初始化弹框内表单
        done();
        this.resetForm('addMsg');
      },
      resetForm(formName) { //弹出框取消 初始化谈框内表单
        this.$refs[formName].resetFields()
        this.addMsg.PostId = '-1'
      }
    }
  }
</script>
