<!--
 * @Description: 委托-选择委托人弹窗
 * @Author:zyy
 * @Date:2021-6-03 16:52
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    title="成员"
    width="800px"
  >
    <section class="p0-16">
      <!--查询条件-->
      <div class="clearfix">
        <el-form ref="searchForm" :model="searchForm" inline @submit.native.prevent>
          <el-form-item
            label="用户名"
            prop="userName"
          >
            <el-input
              v-model.trim="searchForm.userName"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item
            label="真实姓名"
            prop="userName"
          >
            <el-input
              v-model.trim="searchForm.realName"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item class="fr">
            <el-button
              type="primary"
              @click="onSearch('searchForm')"
            >
              搜索
            </el-button>
            <el-button
              plain
              type="info"
              @click="resetForm('searchForm')"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-table
          :data="list"
          :row-class-name="tableRowClassName"
          :row-style="selectedHighlight"
          width="100%"
          highlight-current-row
          @row-click="rowClick"
        >
          <el-table-column
            type="index"
            label="序号"
            width="50"
          />
          <el-table-column
            header-align="center"
            align="left"
            prop="userName"
            label="用户名"
          />
          <el-table-column
            header-align="center"
            align="left"
            prop="realName"
            label="真实姓名"
          />
          <el-table-column
            header-align="center"
            align="left"
            prop="departName"
            label="部门"
          />
        </el-table>
        <pagination :pagination-data="paginationData" @change="loadData" />
      </div>
    </section>
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="loading" @click="dialogData.dialog = false">确定</el-button>
      <el-button @click="dialogData.dialog = false">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
/* 接口*/
// import { allocationItems } from '@/api/procurement/purchasplan' // 分配
/* 公用方法*/
import { filterObj } from '@/assets/js/utils'// 过滤空的key
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'// 分页
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
import { mapGetters } from 'vuex'
import { singleStandardFetchList } from '../../../../../api/common/user.js'

export default {
  name: 'UserDialog',
  components: {
    pagination// 分页
  },
  mixins: [ListMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: () => {
      }
    },
    excludeList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      searchForm: {
        userName: '',
        realName: '', // 委托人名称
        departId: this.$store.getters.info.companyId // 机构id    必传
        // roleCode: '11' // 角色编码   必传   此处传  11
      },
      getIndex: '',
      title: '委托人',
      list: [], // 列表数据
      currentData: [], // 当前选择的信息
      loading: false
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  watch: {

  },
  created() {
    // console.log(this.$store.getters.info)
    this.searchForm.departId = this.dialogData.departId
    this.loadData()
  },
  methods: {
    // 点击一行获取下标
    rowClick(row) {
      this.getIndex = row.index
    },

    rowdbClick(row) {
      this.getIndex = row.index
      this.ensure()
    },
    // 获取表格内每一行下标
    selectedHighlight({ row, rowIndex }) {
      row.index = rowIndex
    },
    // 给选中的一行添加样式
    tableRowClassName({ row, rowIndex }) {
      if (this.getIndex === rowIndex) {
        return 'procurement_pith_on'
      }
    },
    // 选择人员-确定
    ensure() {
      if (typeof this.getIndex === 'number') {
        this.loading = true
        let data = {}
        data = this.list[this.getIndex]
        this.loading = false
        this.dialogData.dialog = false
        this.$emit('userSelected', data)
        /*        allocationItems(data).then(response => {
          this.loading = false
          this.dialogData.dialog = false
          // 分配成功
          this.$message({
            type: 'success',
            message: '分配成功'
          })
          this.$emit('reloadData')
        }).catch(() => {
          this.loading = false
        }) */
      } else {
        this.$message({
          message: `请选择${this.title}`,
          type: 'warning'
        })
      }
    },
    // 加载数据
    loadData() {
      const params = this.getParams()// 查询条件
      singleStandardFetchList(params).then(response => {
        this.list = response.data.records
        this.toExclude()
        this.paginationData.total = response.data.total
      }).catch(() => {

      })
    },
    // 查询条件
    getParams() {
      const param = Object.assign({}, this.searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)// 过滤空的key
    },
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()
    },
    // 重置
    resetForm() {
      this.searchForm.realName = ''
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()
    },
    // 排除已经选择的
    toExclude() {
      window.console.log(this.excludeList)
      this.list = this.list.filter((item) => {
        if ((this.excludeList.find(cell => cell.userId === item.userId))) {
          return false
        }
        return true
      })
    }
  }
}
</script>

<style scoped lang="less">

</style>
