<template>
  <div class="app-container">
    <h4 class="form-header h4">工单流程节点信息</h4>
    <el-form ref="wfProcessActivityList" :model="wfProcessActivityList[0]"
             label-width="120px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="流程id" prop="">
            <el-input v-model="this.wfProcessId" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="流程名称" prop="">
            <el-input v-model="this.wfProcessName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="流程节点id" prop="">
            <el-input v-model="this.processActivityIdCheck" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="流程节点名称" prop="">
            <el-input v-model="this.processActivityNameCheck" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>




    <el-row :gutter="10" class="mb8">


    <el-col :span="11" >
      <h4 class="form-header h4">可审核用户</h4>
      <el-form :model="queryUserAuthParams"
               ref="queryUserAuthParams" size="small" :inline="true" v-show="showSearch"
               onsubmit="return false;"
               label-width="68px">
<!--        <el-form-item label="用户id" prop="authTargetId">-->
<!--          <el-input-->
<!--            v-model="queryUserAuthParams.authTargetId"-->
<!--            placeholder="请输入用户ID"-->
<!--            style="width:140px;"-->
<!--            clearable-->
<!--            @keyup.enter.native="handleUsetAuth"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item label="用户名称" prop="nickName">
          <el-input
            v-model="queryUserAuthParams.nickName"
            placeholder="请输入用户名称"
            style="width:140px;"
            clearable
            @keyup.enter.native="handleUserAuth"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserAuth">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetAuthQuery">重置</el-button>
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="deleteAuthToUser"
          >删除
<!--            v-hasPermi="['wms:wmswarehouse:add']"-->
          </el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="wfProcessActivityAuthList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="工单流程步骤权限ID" align="center" prop="processActivityAuthId" />-->
<!--      <el-table-column label="工单流程节点步骤ID，关联wf_process_activity的process_activity_id" align="center" prop="processActivityId" />-->
<!--      <el-table-column label="关联目标对象类型(1用户)" align="center" prop="authTargetType" />-->
      <el-table-column label="用户ID" align="center" prop="authTargetId" />
      <el-table-column label="用户ID" align="center" prop="nickName" />
    </el-table>
      <pagination
        v-show="totalAuth>0"
        :total="totalAuth"
        :page.sync="queryUserAuthParams.pageNum"
        :limit.sync="queryUserAuthParams.pageSize"
        @pagination="handleUserAuth"
      />
    </el-col>

    <el-col :span="11">
      <h4 class="form-header h4">所有用户</h4>
      <el-form :model="queryUserParams" ref="queryUserParams" size="small" :inline="true"
               v-show="showSearch"
               onsubmit="return false;"
               label-width="68px">
<!--        <el-form-item label="用户id" prop="userId">-->
<!--          <el-input-->
<!--            v-model="queryUserParams.userId"-->
<!--            placeholder="请输入用户ID"-->
<!--            style="width:140px;"-->
<!--            clearable-->
<!--            @keyup.enter.native="hadnleUser"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item label="用户名称" prop="nickName">
          <el-input
            v-model="queryUserParams.nickName"
            placeholder="请输入用户名称"
            style="width:140px;"
            clearable
            @keyup.enter.native="hadnleUser"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="hadnleUser">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetUserQuery">重置</el-button>
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAuthToUser">添加</el-button>
<!--          :disabled = "allocateMaterialBtnDisable"-->
<!--          v-hasPermi="['wms:wmswarehouse:add']"-->
          <!--          <el-button-->
          <!--            type="danger"-->
          <!--            plain-->
          <!--            icon="el-icon-delete"-->
          <!--            size="mini"-->
          <!--            @click="handleUnallocateMaterials"-->
          <!--            v-hasPermi="['wms:wmswarehouse:add']"-->
          <!--          >删除-->
          <!--          </el-button>-->
        </el-form-item>
      </el-form>

      <el-table v-loading="loadingUser" :data="userList" @selection-change="handleSelectionChangeUser">
        <el-table-column type="selection" width="55" align="center" />
<!--        <el-table-column label="工单流程步骤权限ID" align="center" prop="processActivityAuthId" />-->
<!--        <el-table-column label="工单流程节点步骤ID，关联wf_process_activity的process_activity_id" align="center" prop="processActivityId" />-->
<!--        <el-table-column label="关联目标对象类型(1用户)" align="center" prop="authTargetType" />-->
        <el-table-column label="用户ID" align="center" prop="userId" />
        <el-table-column label="用户名称" align="center" prop="nickName" />
    </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryUserParams.pageNum"
        :limit.sync="queryUserParams.pageSize"
        @pagination="hadnleUser"
      />
    </el-col>
    </el-row>



  </div>
</template>

<script>
import { selectUserAuth,listWfProcessActivityAuth, getWfProcessActivityAuth, delWfProcessActivityAuth, addWfProcessActivityAuth, updateWfProcessActivityAuth } from "@/api/system/common/wfProcessActivityAuth";
import { listWfprocessactivity } from '@/api/system/common/wfprocessactivity'
import {getWfprocessactivity} from '@/api/system/common/wfprocessactivity'
import {listUser,selectnoperList} from '@/api/system/user'
import { getWfprocess} from '@/api/system/common/wfprocess'

export default {
  name: "WfProcessActivityAuth",
  data() {
    return {
      wfProcessName:"",
      wfProcessId:"",
      processActivityNameCheck:"",
      loadingUser:true,
      //所有用户
      userListAll:[],
      //有权限用户集合
      userList:[],
      wfProcessActivityList:[],
      processActivityIdCheck:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalAuth:0,
      // 工单流程节点步骤权限表格数据
      wfProcessActivityAuthList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processActivityId: null,
        authTargetType: null,
        //authTargetId: null,
      },
      queryUserAuthParams:{
        pageNum: 1,
        pageSize: 10,
        processActivityId:null,
        //authTargetId:null,
        userName:null,
        params:{
          authTargetId:"",
          nickName: "",
        },
      },
      queryUserParams:{
        pageNum: 1,
        pageSize: 10,
        userId:null,
        nickname:null,
        processActivityId:null,
      },
      //
      // 表单参数
      form: {},
      queryUserAuthParamsForm:{
        pageNum: 1,
        pageSize: 10,
        processActivityId:null,
        userName:null,
        params:{
          authTargetId:"",
          nickName: "",
        }
      },

      // 表单校验
      rules: {
        // userId:[
        //   {pattern:/^[1-9]\d*$/,message: "用户id必须为数字",trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    const processActivityId = this.$route.params && this.$route.params.processActivityId;
    console.log(processActivityId)
    this.processActivityIdCheck = processActivityId;
    this.queryParams.processActivityId = processActivityId;
    this.getList();
    this.hadnleUser();
    this.getWfProcessAcitivityAuth();
    this.selectWfProcessActivityName();
    this.handleUserAll();

  },
  methods: {
    /** 删除用户权限*/
    deleteAuthToUser(row){
    // console.log(this.ids)
      const processActivityAuthIds = row.processActivityAuthId || this.ids;
      const authTargetId = this.nickNames;

      this.$modal.confirm('是否确认删除用户ID为"' + authTargetId + '"的数据项？').then(function() {
        return delWfProcessActivityAuth(processActivityAuthIds);
      }).then(() => {
        this.getList();
        this.hadnleUser();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 添加用户*/
    handleAuthToUser(row){
      this.queryUserAuthParamsForm.processActivityId = this.processActivityIdCheck;
      const authTargetId = row.authTargetId || this.ids;
      this.queryUserAuthParamsForm.params.authTargetId = authTargetId;
      addWfProcessActivityAuth(this.queryUserAuthParamsForm).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
        this.hadnleUser();
      })
      // alert(authTargetId);

    },
    /**重置用户搜索*/
    resetUserQuery(){
      this.resetForm("queryUserParams");
      this.hadnleUser();
    },
    resetAuthQuery(){
      this.queryUserAuthParams.params.nickName =null;
      this.resetForm("queryUserAuthParams");
      this.getList();
    },
    /** 查询拥有权限的用户*/
    handleUserAuth(){
      // this.queryUserAuthParams.authTargetType = "1";
      this.loading=true;
      this.queryUserAuthParams.processActivityId = this.processActivityIdCheck;
      selectUserAuth(this.queryUserAuthParams).then(response=>{
        this.wfProcessActivityAuthList = response.rows;
        this.totalAuth = response.total;
        // console.log(this.totalAuth)
        this.loading = false;
      })
    },
    /** 查询所有用户*/
    handleUserAll(){
      listUser().then(response=>{
        this.userListAll = response.rows;
        console.log(this.userListAll)
      })
    },
    /** 查询没有权限的用户*/
    hadnleUser(){
      this.loadingUser = true;
      this.queryUserParams.processActivityId = this.processActivityIdCheck;
      selectnoperList(this.queryUserParams).then(response=>{
        this.userList = response.rows;
        this.total = response.total;
        this.loadingUser = false;
      })
    },
    /** 查询工单流程节点步骤列表 */
    getWfProcessAcitivityAuth() {
      listWfprocessactivity(this.queryParams).then(response => {
        this.wfProcessActivityList = response.rows;
      });
    },
    /** 查询工单流程节点名称*/
    selectWfProcessActivityName(){
      getWfprocessactivity(this.processActivityIdCheck).then(response=>{
        // console.log(response.data.processActivityName)
        this.processActivityNameCheck = response.data.processActivityName
        this.wfProcessId = response.data.wfProcessId
        this.selectWfProcessName();
      })
    },
    /** 查询工单流程名称*/
    selectWfProcessName(){
      getWfprocess(this.wfProcessId).then(response=>{
        this.wfProcessName = response.data.wfProcessName;
        // this.wfProcessId = response.data.wfProcessId
        // this.processActivityNameCheck = response.data.processActivityName
      })
    },

    getList() {
      this.loading = true;
      this.queryUserAuthParams.processActivityId = this.processActivityIdCheck;
      selectUserAuth(this.queryUserAuthParams).then(response=>{
        this.wfProcessActivityAuthList = response.rows;
        this.totalAuth  = response.total;
        this.loading = false;
      })
      // listWfProcessActivityAuth(this.queryParams).then(response => {
      //   this.wfProcessActivityAuthList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        processActivityAuthId: null,
        processActivityId: null,
        authTargetType: null,
        authTargetId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // console.log(selection)
      this.ids = selection.map(item => item.processActivityAuthId)
      this.nickNames = selection.map(item => item.authTargetId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionChangeUser(selection) {
      // console.log(selection)
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单流程节点步骤权限";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const processActivityAuthId = row.processActivityAuthId || this.ids
      getWfProcessActivityAuth(processActivityAuthId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单流程节点步骤权限";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.processActivityAuthId != null) {
            updateWfProcessActivityAuth(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWfProcessActivityAuth(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const processActivityAuthIds = row.processActivityAuthId || this.ids;
      this.$modal.confirm('是否确认删除工单流程节点步骤权限编号为"' + processActivityAuthIds + '"的数据项？').then(function() {
        return delWfProcessActivityAuth(processActivityAuthIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wfProcessActivityAuth/export', {
        ...this.queryParams
      }, `wfProcessActivityAuth_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
