<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
        <Form
          v-show="searchOpen"
          inline
          onsubmit="return false"
          :label-width="70"
        >
          <label class="label-sign" />
          <Form-item class="search-item" label="禁封IP:">
            <el-input v-model="formObj.ip" placeholder="请输入IP" @keyup.enter.native="_formSearch" />
          </Form-item>
          <Form-item class="search-btn">
            <el-button type="primary" @click="_formSearch">
              搜索
            </el-button>
          </Form-item>
        </Form>
      </el-col>
      <!--操作-->
      <el-col :span="24">
        <btn-list
          :msg="btn"
          class="contHide"
          :open="searchOpen"
          :show-search-btn="true"
          @on-result-change="_btnClick"
        />
      </el-col>
      <!-- 表格 -->
      <el-col :span="24">
        <element-table
          ref="pageTable"
          :table-height="tableHeight"
          :get-page="getPage"
          @on-result-change="_tableResultChange"
        >
          <el-table-column
            v-for="item in pageColumns"
            :key="item.key"
            sortable
            show-overflow-tooltip
            :prop="item.key"
            :label="item.title"
            :min-width="item.width"
            :fixed="item.fixed?item.fixed:undefined"
          >
            <template slot-scope="scope">
              <div v-if="item.datetime">
                {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM'):'' }}
              </div>
              <div v-else-if="item.key==='status'">
                <div v-if="scope.row[item.key]===0" class="red-color">
                  封号
                </div>
                <div v-else class="green-color">
                  解封
                </div>
              </div>
              <div v-else>
                {{ scope.row[item.key] }}
              </div>
            </template>
          </el-table-column>
          <template slot="col">
            <el-table-column
              label="操作"
              align="center"
              :width="100"
              fixed="right"
            >
              <template slot-scope="scope">
                <icon-list
                  :msg="scope.row['status']===0?iconMsg:iconMsgDis"
                  :row-data="scope.row"
                  @on-result-change="_iconClick"
                />
              </template>
            </el-table-column>
          </template>
        </element-table>
      </el-col>
    </el-row>

    <!-- 添加、编辑 -->
    <blackListEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { blackList } from '../../../api'
import blackListEdit from './blackListEdit.vue'

export default {
  components: {
    blackListEdit
  },
  data() {
    return {
      searchOpen: false,
      btn: [{ type: 'success', id: '', name: '添加' }],
      iconMsg: [
        { type: 'pt-locked', id: '', name: '封号', disabled: true },
        { type: 'pt-unlocked', id: '', name: '解封' }
      ],
      iconMsgDis: [
        { type: 'pt-locked', id: '', name: '封号' },
        { type: 'pt-unlocked', id: '', name: '解封', disabled: true }
      ],
      selectIds: [],
      formObj: { ip: '' },
      getPage: {},
      pageColumns: [
        { title: '被封 IP 地址', key: 'ip', width: 90, detail: true },
        { title: '被封用户 ID', key: 'userId', width: 90 },
        { title: '被封次数', key: 'totalCount', width: 90 },
        { title: '状态', key: 'status', width: 100 },
        { title: '解封人姓名', key: 'releaseName', width: 100 },
        {
          title: '最近封号时间',
          key: 'recentStopTime',
          width: 150,
          datetime: true
        },
        {
          title: '最近解封时间',
          key: 'recentOpenTime',
          width: 150,
          datetime: true
        }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._formSearch()
  },
  methods: {
    _iconClick(res, data) {
      switch (res) {
        case '封号':
          this._denyById(data.id)
          break
        case '解封':
          this._openById(data.id)
          break
      }
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await blackList.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _editModal: async function(id) {
      const result = await blackList.getById(id)
      this.$refs.editModal._open(result)
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
      }
    },
    _denyById(id) {
      // 封号
      this.$confirm('确定封杀该账号?', '提示', { type: 'warning' }).then(() => {
        this._deny([id])
      })
    },
    _deny: async function(ids) {
      const result = await blackList.deny(ids)
      if (result) {
        this._page()
        this.$message.success('封号成功！')
      }
    },
    _openById(id) {
      // 解封
      this.$confirm('确定解封该账号?', '提示', { type: 'warning' }).then(() => {
        this._open([id])
      })
    },
    _open: async function(ids) {
      const result = await blackList.open(ids)
      if (result) {
        this._page()
        this.$message.success('解封成功！')
      }
    }
  }
}
</script>
