<template>
  <div>
    <ht-table
      @load="orgPostData"
      :data="orgPostList"
      :pageResult="orgPostPageResult"
      :selection="true"
      :show-export="false"
      :show-custom-column="false"
      :default-querys="[{property: 'account', value: data.account}]"
      quick-search-props="orgName,posName"
      ref="orgPostTables"
      @selection-change="handleSelectionChange"
    >
      <template v-slot:toolbar>
        <el-button
          size="small"
          @click="divertClick"
          type="primary"
          icon="icon-zhuanyi"
          :disabled="isDisabled"
          >转移</el-button
        >
        <el-button
          size="small"
          @click="copyClick"
          icon="icon-copy"
          :disabled="isDisabled"
          >复制</el-button
        >
        <ht-delete-button
          :url="delOrgUser"
          :htTable="$refs.orgPostTables"
          @after-delete="afterDelOrgUser"
          pk="orgUserId"
          :disabled="isDisabled"
          >删除</ht-delete-button
        >
      </template>
      <template>
        <ht-table-column type="index" width="50" align="center" label="序号" />
        <ht-table-column prop="demName" label="组织维度" width="160" />
        <ht-table-column prop="orgName" label="组织名称" />
        <ht-table-column prop="posName" label="岗位名称" />
        <ht-table-column prop="isMaster" label="主组织/主岗位" width="140">
          <template v-slot="{row}">
            <el-tag type="danger" v-if="row.isMaster == '0'">否</el-tag>
            <el-tag v-if="row.isMaster == '1'">是</el-tag>
          </template>
        </ht-table-column>
      </template>
    </ht-table>
    <!--用户选择对话框  -->
    <ht-user-dialog
      ref="eipUserDialog"
      @on-confirm="eipUserDialogOk"
      append-to-body
    />
  </div>
</template>

<script>
import utils from '@/hotent-ui-util.js'
import uc from '@/api/uc.js'
const eipUserDialog = () => import('@/components/dialog/EipUserDialog.vue')
export default {
  name: 'userOrgPostList',
  props: ['data'],
  components: {eipUserDialog},
  computed: {
    delOrgUser: function() {
      return window.context.uc + '/api/org/v1/orgUser/delOrgUser'
    }
  },
  data() {
    return {
      orgPostList: [], //用户所属组织岗位
      orgPostPageResult: {
        page: 1,
        pageSize: 20,
        total: 0
      },
      selectOrgs: [], //选择的组织
      selectPosts: [], //选择的岗位
      type: '', //1、转移，2、复制
      orgUserIds: [], //转移操作时要删除的组织岗位Id
      isDisabled: true,
      selectedList: []
    }
  },
  watch: {
    'data.account': {
      handler(newValue, oldValue) {
        if (newValue && oldValue && newValue != oldValue) {
          this.$nextTick(() => {
            this.$refs.orgPostTables.load()
          })
        }
      },
      deep: true,
      immediate: true
    },
    selectedList: {
      handler(val) {
        this.isDisabled = val.length < 1 ? true : false
      },
      deep: true
    }
  },
  methods: {
    handleSelectionChange(selection) {
      this.selectedList = selection
    },
    //转移
    divertClick() {
      this.type = ''
      this.selectOrgs = []
      this.selectPosts = []
      let selectSelection = this.$refs.orgPostTables.selection
      if (selectSelection && selectSelection.length == 0) {
        this.$message.error('请选择要转移的组织岗位')
        return
      }
      for (let k = 0; k < selectSelection.length; k++) {
        this.orgUserIds.push(selectSelection[k].orgUserId)
        if (selectSelection[k].posCode) {
          this.selectPosts.push(selectSelection[k].posCode)
        } else {
          this.selectOrgs.push(selectSelection[k].orgCode)
        }
      }
      this.type = '1'
      this.$refs['eipUserDialog'].showDialog()
    },
    //复制
    copyClick() {
      this.type = ''
      this.selectOrgs = []
      this.selectPosts = []
      let selectSelection = this.$refs.orgPostTables.selection
      if (selectSelection && selectSelection.length == 0) {
        this.$message.error('请选择要复制的组织岗位')
        return
      }
      for (let k = 0; k < selectSelection.length; k++) {
        if (selectSelection[k].posCode) {
          this.selectPosts.push(selectSelection[k].posCode)
        } else {
          this.selectOrgs.push(selectSelection[k].orgCode)
        }
      }
      this.type = '2'
      this.$refs['eipUserDialog'].showDialog()
    },
    //选择用户确认事件
    eipUserDialogOk(data) {
      if (data && data.length > 0) {
        let accounts = []
        for (let k = 0; k < data.length; k++) {
          if (data[k].account != this.data.account) {
            accounts.push(data[k].account)
          }
        }
        let map = {
          accounts: accounts,
          orgCodes: this.selectOrgs,
          postCodes: this.selectPosts,
          type: this.type
        }
        if ((this.type = '1')) {
          //转移
          map.orgUserIds = this.orgUserIds
        }
        uc.operatingUserOrgPost(map).then(res => {
          if (res.state) {
            this.$message.success(res.message)
            this.$nextTick(() => {
              this.$refs.orgPostTables.load()
            })
          }
        })
      }
    },
    //查询所属组织岗位
    orgPostData(param, cb) {
      uc.getUserOrgPage(param)
        .then(response => {
          this.orgPostList = response.rows
          this.orgPostPageResult = {
            page: response.page,
            pageSize: response.pageSize,
            total: response.total
          }
        })
        .finally(() => cb())
    },
    afterDelOrgUser() {
      this.$refs.orgPostTables.load()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-main {
  position: relative;
  .toolbar-panel {
    position: absolute;
    right: 0;
  }
}
</style>
