<!--
  @description:项目组设置列表
  @Modifier: haofengxian
  @routerPath: /bidding/projectDrive/tenderProjectUserSetting
  @date: 2020-6-8-15:13
  isCanOperate: true可以操作 false不可以查看只有查看权限

-->
<template>
  <div class="main">
    <!--当前登陆的角色是否有该项目权限-->
    <div v-if="tenderBase.isCanOperate" class="mb10">
      <el-button type="primary" @click="addMember">
        添加
      </el-button>
    </div>
    <div>
      <el-table
        ref="smallTable"
        v-loading="loading"
        :data="tableData"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60px"
          align="center"
        />
        <el-table-column
          prop="realName"
          label="姓名"
          min-width="160"
        />
        <el-table-column
          header-align="center"
          prop="checkStatus"
          label="查看权限"
          min-width="100"
        >
          <!--projectUserType 项目用户类型 00:创建人 01:特殊分配用户(可对项目进行再次分配) 02:普通分配用户-->
          <template slot-scope="scope">
            <el-checkbox
              v-model="scope.row.checkStatus"
              :disabled="scope.row.projectUserType!=='02' || !tenderBase.isCanOperate"
            />
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          prop="canOperateStatus"
          label="操作权限"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-checkbox
              v-model="scope.row.canOperateStatus"
              :disabled="scope.row.projectUserType!=='02' || !tenderBase.isCanOperate"
            />
          </template>
        </el-table-column>
        <el-table-column
          v-if="tenderBase.isCanOperate"
          header-align="center"
          label="操作"
          width="90"
          align="center"
        >
          <template slot-scope="scope">
            <el-link
              v-if="scope.row.projectUserType==='02'"
              type="primary"
              @click="deleteUser(scope.row)"
            >
              [删除]
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <!--保存按钮-->
      <div v-if="tenderBase.isCanOperate" class="but_footer">
        <div class="but_footer-h">
          <div class="but_footer-but">
            <el-button :loading="confirmLoading" type="primary" @click="saveData">
              保存
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <!--选择操作用户-->
    <setting-select-dialog ref="userSettingSelect" @loadData="loadData" />
  </div>
</template>

<script>
/* 接口*/
import { listTenderProjectUserList, deleteTenderProjectUser, distributionTenderProjectUser } from '@/api/bidding/biddingProject'
import bidProjectUserSettingSelect from './bidProjectUserSettingSelect-dialog'

export default {
  name: 'BidProjectUserSettingList',
  components: {
    'setting-select-dialog': bidProjectUserSettingSelect // 选择操作用户
  },
  data() {
    return {
      tenderBase: this.$store.getters.tenderBase, // 项目信息
      confirmLoading: false, // 控制按钮loading
      tenderId: this.$route.query.id, // 项目Id
      loading: true,
      tableData: []
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    // 添加成员
    addMember() {
      // 触发子组件方法打开弹框加载数据
      const userIdList = []
      this.tableData.map(item => {
        userIdList.push(item.userId)
      })
      this.$refs.userSettingSelect.openDialogVisible(userIdList)
    },
    // 获取数据
    loadData() {
      const params = {
        tenderId: this.tenderId
      }
      listTenderProjectUserList(params).then(response => {
        this.loading = false
        this.tableData = response.data
        const len = this.tableData.length
        for (let i = 0; i < len; i++) {
          // 查看状态 00:不可查看 01:可查看
          if (this.tableData[i].checkStatus === '00') {
            this.tableData[i].checkStatus = false
          } else {
            this.tableData[i].checkStatus = true
          }
          // 是否可以操作 00:否 01:是
          if (this.tableData[i].canOperateStatus === '00') {
            this.tableData[i].canOperateStatus = false
          } else {
            this.tableData[i].canOperateStatus = true
          }
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 保存组装数据
    assemblyData() {
      this.tableData.map((item) => {
        // 查看状态 00:不可查看 01:可查看
        item.checkStatus ? item.checkStatus = '01' : item.checkStatus = '00'
        // 是否可以操作 00:否 01:是
        item.canOperateStatus ? item.canOperateStatus = '01' : item.canOperateStatus = '00'
      })
      const data = {
        tenderId: this.tenderId, // 项目id
        userInfoList: this.tableData
      }
      return data
    },
    // 保存
    saveData() {
      this.confirmLoading = true// btnLoding
      const data = this.assemblyData()// 组装数据
      distributionTenderProjectUser(data).then(res => {
        this.confirmLoading = false// btnLoding
        this.$message({
          message: '保存成功',
          type: 'success'
        })
        this.loadData()
      }).catch(() => {
        this.confirmLoading = false// btnLoding
      })
    },
    // 删除项目
    deleteUser(row) {
      this.$confirm('确认删除本条权限信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        deleteTenderProjectUser(row.id).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          // 获取列表信息
          this.loadData()
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
