<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="封号对象" prop="member">
        <el-input
            v-model="queryParams.member"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封号类型" prop="type">
        <el-select v-model="queryParams.type" clearable size="small">
          <el-option
              v-for="dict in forbidTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @keyup.enter.native="handleQuery"
          />
        </el-select>
      </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="handleForbidIp"
            v-hasPermi="['cai:userForbid:forbidIp']"
        >自定义封禁IP</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userForbidList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="类型" align="center" prop="type">
        <template v-slot="scope">
          <cai-dict-tag :options="forbidTypeList" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="封号对象" align="center" prop="member">
        <template v-slot="scope">
          <div class="context-avatar-center" v-if="scope.row.type === 1">
            {{ scope.row.nickname }}【{{ scope.row.member }}】
            <image-avatar :src="scope.row.avatar"/>
          </div>
          <span v-if="scope.row.type !== 1">
            {{ scope.row.member }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="beginTime"/>
      <el-table-column label="结束时间" align="center" prop="endTime"/>
      <el-table-column label="最后封禁理由" align="center" prop="reason"/>
      <el-table-column label="状态" align="center" prop="forbidStatus">
        <template v-slot="scope">
          <cai-dict-tag :options="forbidStatusList" :value="scope.row.forbidStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template v-slot="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-if="scope.row.forbidStatus === 1"
              v-hasPermi="['cai:userForbid:unForbid']"
              @click="handleForbid(scope.row)"
          >解封
          </el-button>
          <el-button
              size="mini"
              type="text"
              @click="handleForbidLogInfo(scope.row)"
          >封禁记录
          </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"
    />

    <user-forbid-log-list v-if="userForbidLogListVisible" ref="userForbidLogList"/>
    <user-forbid-ip-dialog v-if="userForbidIpDialogVisible" ref="userForbidIpDialog" @refreshDataList="getList" />
  </div>
</template>

<script>
import {listUserForbid, unForbid} from '@/api/cai/userForbid'
import {forbidStatusList, forbidTimeList, forbidTypeList} from '@/constant/statusMap'
import UserForbidLogList from "@/views/cai/userForbid/user-forbid-log-list";
import UserForbidIpDialog from "@/views/cai/userForbid/user-forbid-ip-dialog";

export default {
  name: "UserForbid",
  components: {
    UserForbidLogList,UserForbidIpDialog
  },
  data() {
    return {
      forbidTimeList, forbidTypeList, forbidStatusList,
      userForbidLogListVisible: false,
      userForbidIpDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 封号列表表格数据
      userForbidList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userUsercode: undefined,
        userMobile: undefined,
        type: undefined,
        member: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询封号列表列表 */
    getList() {
      this.loading = true;
      listUserForbid(this.queryParams).then(response => {
        this.userForbidList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    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
    },
    handleForbid(row) {
      this.$modal.confirm('是否确认解封对象："' + row.member + '"的数据项？').then(() => {
        this.loading = true;
        return unForbid(row.id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("解封成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    handleForbidLogInfo(row) {
      this.userForbidLogListVisible = true
      this.$nextTick(() => {
        this.$refs.userForbidLogList.init(row.member)
      })
    },
    handleForbidIp(){
      this.userForbidIpDialogVisible = true
      this.$nextTick(() => {
        this.$refs.userForbidIpDialog.init()
      })
    }
  }
};
</script>
<style lang="scss" scoped>
  .context-avatar-center {
    display: flex;
    align-items: center; /* 垂直居中 */
    justify-content: center; /* 水平居中 */
  }
</style>
