<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="查看权限" prop="isshow">
        <el-select v-model="queryParams.isshow" placeholder="请选择查看权限" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="编辑权限" prop="isedit">
        <el-select v-model="queryParams.isedit" placeholder="请选择编辑权限" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="删除权限" prop="isdel">
        <el-select v-model="queryParams.isdel" placeholder="请选择删除权限" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable
          v-model="queryParams.createdTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yscgFileRole:filerole:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yscgFileRole:filerole:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yscgFileRole:filerole:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yscgFileRole:filerole:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileroleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户姓名" align="center" prop="nickName" />
      <el-table-column label="查看权限" align="center" prop="isshow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isshow"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="编辑权限" align="center" prop="isedit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isedit"/>
        </template>
      </el-table-column>
      <el-table-column label="删除权限" align="center" prop="isdel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isdel"/>
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yscgFileRole:filerole:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yscgFileRole:filerole:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 选择用户对话框 -->
    <el-dialog title="选择用户" :visible.sync="open" width="800px" top="5vh" append-to-body>
      <el-form :model="userQueryParams" ref="userQueryForm" size="small" :inline="true">
        <el-form-item label="统一账号" prop="userName">
          <el-input
            v-model="userQueryParams.userName"
            placeholder="请输入统一账号"
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="userQueryParams.phonenumber"
            placeholder="请输入手机号码"
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetUserQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-table 
          v-loading="userLoading"
          @row-click="clickRow" 
          ref="userTable" 
          :data="userList" 
          @selection-change="handleUserSelectionChange" 
          height="260px"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="统一账号" prop="userName" :show-overflow-tooltip="true" />
          <el-table-column label="用户姓名" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true" />
          <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="userTotal>0"
          :total="userTotal"
          :page.sync="userQueryParams.pageNum"
          :limit.sync="userQueryParams.pageSize"
          @pagination="getUserList"
        />
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSelectUser">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFilerole, getFilerole, delFilerole, addFilerole, updateFilerole, batchYscgFileRole } from "@/api/yscgFileRole/filerole"
import { listUser } from "@/api/system/user"

export default {
  name: "Filerole",
  dicts: ['sys_yes_no', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分享管理表格数据
      fileroleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        isshow: null,
        isedit: null,
        isdel: null,
        createdTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 用户列表相关
      userLoading: false,
      // 选中用户ID数组
      selectedUserIds: [],
      // 用户列表数据
      userList: [],
      // 用户列表总数
      userTotal: 0,
      // 用户查询参数
      userQueryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询分享管理列表 */
    getList() {
      this.loading = true
      const fileid = this.$route.query.fileid || this.$route.query.fileId || this.$route.params.fileid || this.$route.params.fileId
      listFilerole({ ...this.queryParams, fileid }).then(response => {
        this.fileroleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickName: null,
        isshow: null,
        isedit: null,
        isdel: null,
        createdTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      // 重置用户查询参数
      this.resetUserQuery()
      this.selectedUserIds = []
      // 获取用户列表
      this.getUserList()
      this.open = true
      this.title = "添加分享管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getFilerole(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改分享管理"
      })
    },
    /** 提交按钮 - 选择用户后确定 */
    handleSelectUser() {
      const userIds = this.selectedUserIds.join(",")
      if (userIds == "") {
        this.$modal.msgError("请选择要分配的用户")
        return
      }
      // 获取fileid，优先从路由查询参数获取，如果没有则从路由参数获取
      const fileid = this.$route.query.fileid || this.$route.query.fileId || this.$route.params.fileid || this.$route.params.fileId
      if (!fileid) {
        this.$modal.msgError("缺少文件ID参数")
        return
      }
      batchYscgFileRole(fileid, userIds).then(response => {
        this.$modal.msgSuccess(response.msg || "新增成功")
        this.open = false
        this.getList()
      }).catch(() => {})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFilerole(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addFilerole(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除分享管理编号为"' + ids + '"的数据项？').then(function() {
        return delFilerole(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yscgFileRole/filerole/export', {
        ...this.queryParams
      }, `filerole_${new Date().getTime()}.xlsx`)
    },
    /** 查询用户列表 */
    getUserList() {
      this.userLoading = true
      listUser(this.userQueryParams).then(res => {
        this.userList = res.rows
        this.userTotal = res.total
        this.userLoading = false
      }).catch(() => {
        this.userLoading = false
      })
    },
    /** 用户搜索按钮操作 */
    handleUserQuery() {
      this.userQueryParams.pageNum = 1
      this.getUserList()
    },
    /** 用户重置按钮操作 */
    resetUserQuery() {
      this.resetForm("userQueryForm")
      this.userQueryParams = {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined
      }
      this.getUserList()
    },
    // 用户多选框选中数据
    handleUserSelectionChange(selection) {
      this.selectedUserIds = selection.map(item => item.userId)
    },
    // 点击行选中
    clickRow(row) {
      this.$refs.userTable.toggleRowSelection(row)
    }
  }
}
</script>
