<template>
  <div id="block-list">
        <div class="layout--tips mBottom20">
          <i class="el-icon-info"></i>
          查看被系统判定为异常风险的手机号，这部分手机将无法完成正常的注册认证流程；若为系统误判，可将手机号加入白名单后继续后续注册认证流程
        </div>
      <div class="content">
        <header>
          <el-input
            v-model="search.phone"
            placeholder="输入手机号搜索"
            @keyup.native.enter="handleSearch"
            prefix-icon="el-icon-search"
            style="width: 249px;"
            maxlength="32"
            clearable>
          </el-input>
          <!-- <el-button type="primary" @click="dialogVisible = true">移除白名单</el-button> -->
        </header>

        
        <el-table
          v-loading="load"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%">

          <el-table-column
            label="更新时间"
            show-overflow-tooltip>
            <template slot-scope="{row}">
              {{ row.updateTime | formatDate('ymd') }} <br /> {{ row.updateTime | formatDate('hms') }}
            </template>
          </el-table-column>

          <el-table-column
            label="手机号"
            show-overflow-tooltip>
            <template slot-scope="{row}">
              {{ row.phone || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            label="风险详情"
            show-overflow-tooltip>
            <template slot-scope="{row}">
              {{ row.riskDetail || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            show-overflow-tooltip>
            <template slot-scope="{row}">
              <el-button type="text" @click="joinWhiteList(row.phone, 1)">加入白名单</el-button>
            </template>
          </el-table-column>

        </el-table>
        
        <dm-pagination
          v-if="tableData.length > 0"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[20, 40, 60, 80]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next"
          :total="page.totalCount">
        </dm-pagination>
      </div>

      <el-dialog
        title="移除白名单"
        :visible.sync="dialogVisible"
        width="600px"
        @close="onCloseDialog">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item
            label="手机号"
            prop="phone"
            :rules="[
              { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
              { pattern: /^[0-9]{6,13}$/, message: '请输入6-13位数字', trigger: ['blur', 'change'] }
            ]">
            <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </el-dialog>

    <!-- <div class="foot-add">
      <vue-gic-footer></vue-gic-footer>
    </div> -->
  </div>
</template>
<script>
import NavPath from "@/common/navbar/navbar.vue";
import url from "../../components/axios/url";
import { doFetch, doFetchqs, doFetchGet } from "../../components/axios/api";

export default {
  name: 'blockList',
  components: { NavPath },

  created() {
    this.$store.commit("mutations-slide", false);
    this.getList();
  },

  data() {
    return {
      navpath: [{ name: "首页", path: "" }, { name: "黑名单列表", path: "" }],
      load: false,
      search: {
        phone: ''
      },
      tableData: [],
      page: {
        currentPage: 1,
        pageSize: 20,
        totalCount: 999
      },

      dialogVisible: false,
      form: {
        phone: ''
      }
    }
  },

  filters: {
    formatDate(val, format) {
      if(!val) return '--';
      val = new Date(val);
      let y = val.getFullYear();
      let m = (val.getMonth() + 1).toString().padStart(2, '0');
      let d = val.getDate().toString().padStart(2, '0');
      let h = val.getHours().toString().padStart(2, '0');
      let mm = val.getMinutes().toString().padStart(2, '0');
      let s = val.getSeconds().toString().padStart(2, '0');
      if(format == 'ymd') {
        return `${y}-${m}-${d}`;
      } else if(format == 'hms') {
        return `${h}:${mm}:${s}`;
      }
      return `${y}-${m}-${d}\xa0\xa0\xa0${h}:${mm}:${s}`;
    }
  },

  methods: {
    getList() {
      this.load = true;

      const { phone } = this.search;
      let params = {
        pageSize: this.page.pageSize,
        currentPage: this.page.currentPage,
        phone
      }

      doFetchGet(url.getBlockList, params).then(res => {
        const { errorCode, result, message } = res.data || {};
        if (errorCode !== 0) return this.$message.error(message);

        const { result: table, totalCount } = result || {};
        this.tableData = table || [];
        this.page.totalCount = totalCount || 0;
      }).finally(_ => this.load = false);
    },
    handleSizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.getList();
    },
    handleSearch() {
      this.handleCurrentChange(1);
    },
    joinWhiteList(phone, type) {
      this.$confirm('加入白名单后，该手机号可正常完成注册', '加入白名单', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        doFetchGet(url.addToWhiteList, { phone, type }).then(res => {
          const { errorCode, message } = res.data || {};
          if (errorCode !== 0) return this.$message.error(message);
          this.getList();
        })
      })
    },
    onCloseDialog() {
      this.$refs.form.resetFields();
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (!valid) return;
        const { phone } = this.form;
        doFetchGet(url.addToWhiteList, { phone, type: 2 }).then(res => {
          const { errorCode, message } = res.data || {};
          if (errorCode !== 0) return this.$message.error(message);
          this.dialogVisible = false;
          this.getList();
        })
      })
    }
  }
}
</script>
<style lang="scss">
#block-list {
  padding: 20px;
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .content {
    background-color: #fff;
    min-height: calc(100vh - 376px);
    .el-table {
      margin: 20px 0;
    }
    .el-pagination {
      text-align: right;
    }
  }
}
.layout--tips {
  i {
    margin-right: 4px;
    color: #2F54EB;
  }
  font-weight: normal;
  padding: 8px 16px;
  line-height: 1.5;
  font-size: 13px;
  border-radius: 4px;
  color: #606266;
  background-color: #f4f4f5;
}
</style>