<!--
  @description: 机构选择
  @Modifier: haoFx
  @routerPath:
  @date: 2020/3/26-13:32
-->
<template>
  <el-dialog
    v-dialogDrag
    :before-close="cancelledChoice"
    :visible.sync="visibleDepartDialog"
    :close-on-click-modal="false"
    title="审核人员选择"
    width="840px"
    custom-class="el-dialog__body-p8 el-dialog_table"
    append-to-body
  >
    <section class="clearfix">
      <el-form ref="searchForm" :model="searchForm" inline>
        <el-form-item label="人员名称" prop="realName">
          <el-input
            v-model.trim="searchForm.realName"
            placeholder="人员名称"
            style="width: 220px"
          />
        </el-form-item>
        <el-form-item class="fr">
          <el-button
            type="primary"
            @click="search"
          >
            搜 索
          </el-button>
          <el-button
            plain
            type="info"
            @click="resetForm('searchForm')"
          >
            重 置
          </el-button>
        </el-form-item>
      </el-form>
    </section>
    <section>
      <el-table
        ref="singleTable"
        :data="list"
        class="pointer"
        size="mini"
        highlight-current-row
        @row-click="singleElection"
        @current-change="currentChange"
      >
        <el-table-column label="" width="65" align="center">
          <template slot-scope="scope">
            <el-radio v-model="templateSelection" :label="scope.$index">
&nbsp;
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="80"
        />
        <el-table-column
          header-align="center"
          label="人员名称"
          prop="realName"
        />
        <el-table-column
          header-align="center"
          label="部门名称"
          prop="departName"
        />
        <el-table-column
          header-align="center"
          label="手机号码"
          prop="mobilePhone"
        />
      </el-table>
      <!--分页组件-->
      <pagination :pagination-data="paginationData" @change="initUserDepart" />
    </section>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure">确 定</el-button>
      <el-button @click="cancelledChoice">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { getUserDepartByUserIdsRoleIds } from '@api/work/work'
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'// 分页
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
export default {
  name: 'DepartDialog',
  components: {
    pagination// 分页
  },
  mixins: [ListMixin],
  data() {
    return {
      templateSelection: '',
      isSingleUser: '', // 判断流程否是单节点 00否 01是
      isCheckMutil: '', // 是否指定审核人
      visibleDepartDialog: false, // 机构弹框是否展开
      processInstanceId: this.$route.params.processInstanceId, // 实例化id,
      // 搜索信息
      searchForm: {
        userId: '', // 审核人id（初始化需要）
        auditUserIds: '', // 审核人id
        auditUserName: '', // 审核真实姓名
        auditMobilePhone: '', // 审核手机号
        processDefinitionKey: '', // 工作流key
        nodeId: '', // 工作流节点id
        orgCode: '', // 项目经理代码
        departName: '', // 项目经理名称
        realName: '' // 人员名称
      },
      list: [], // 列表数据
      currentData: {} // 当前选择的信息
    }
  },
  methods: {
    // 工作流流程节点/获取审核用户
    getUserDepart(processNode) {
      if (processNode !== undefined) {
        this.searchForm.processDefinitionKey = processNode.processDefinitionKey // 工作流key
        this.searchForm.nodeId = processNode.nodeId // 工作流key
        this.searchForm.departId = processNode.departId
        this.isCheckMutil = processNode.isCheckMutil
      }
      this.initUserDepart('01')
    },
    initUserDepart(type) {
      // type 01提交 00 搜索/重置
      const values = {
        pageNo: this.paginationData.pageNo,
        pageSize: this.paginationData.pageSize,
        processInstanceId: this.processInstanceId,
        ...this.searchForm
      }
      getUserDepartByUserIdsRoleIds(values).then(response => {
        const { data } = response.data
        this.list = data.records
        this.paginationData.total = data.total
        // 初始化数据
        if (this.list.length > 0) {
          if (type === '01') {
            // 单用户
            if (data.total === 1) {
              this.isSingleUser = '01' // 判断否是单用户 00否 01是
              this.singleUser()
            } else if (data.total > 1 && this.isCheckMutil === '00') {
              this.singleUser()
            } else {
              // 多用户
              this.isSingleUser = '00' // 判断否是单用户 00否 01是
              this.openDialog()
            }
            this.$emit('isSingleUser', this.isSingleUser)
          }
        } else {
          this.$message.warning('此单位暂无审核人，无法下一步操作，如有问题请联系系统管理员')
        }
      }).catch(() => {
      })
    },
    /* =========================单用户===============*/
    singleUser() {
      // 不显示流程信息
      this.visibleDepartDialog = false // 不打开弹框
      // 传给父组件单审核人信息
      this.searchForm.auditUserName = this.list[0].realName // 审核人姓名
      this.searchForm.auditMobilePhone = this.list[0].mobilePhone // 审核人电话
      this.searchForm.auditUserIds = this.list[0].userId // 审核人id
      this.searchForm.userId = this.list[0].userId // 审核人id（初始化）
      this.$emit('processInitData', this.searchForm)
      this.$emit('submit')
    },
    /* =========================多用户===============*/
    // 打开弹框
    openDialog() {
      this.visibleDepartDialog = true // 打开弹框选择
      this.$refs.singleTable.setCurrentRow()
      this.templateSelection = ''
    },
    // 当前选择的信息
    currentChange(val) {
      this.currentData = val
    },
    // 选择信息-确定
    ensure() {
      if (this.currentData && JSON.stringify(this.currentData) !== '{}') {
        // 传给父组件审核人信息
        this.searchForm.auditUserName = this.currentData.realName// 审核人姓名
        this.searchForm.auditMobilePhone = this.currentData.mobilePhone// 审核人电话
        this.searchForm.auditUserIds = this.currentData.userId // 审核人id
        this.searchForm.userId = this.currentData.userId // 审核人id（初始化）
        this.$emit('processInitData', this.searchForm)
        this.$emit('submit')
        this.visibleDepartDialog = false
      } else {
        this.$message({
          message: '请选择审核人',
          type: 'warning'
        })
      }
    },
    // 搜索重置
    resetForm(formName) {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.$refs[formName].resetFields()
      this.initUserDepart('00')
    },
    // 搜索
    search() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.initUserDepart('00')
    },
    // 点击行设置单选按钮选中
    singleElection(row) {
      this.templateSelection = this.list.indexOf(row)
    },
    // 取消选择
    cancelledChoice() {
      this.templateSelection = ''
      this.$refs.singleTable.setCurrentRow()
      this.visibleDepartDialog = false
    }
  }
}
</script>

<style scoped lang="less">
  /deep/.el-radio__label {
    display: none;
  }
</style>
