<template>
    <div>
        <crumbs title="超管信息">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="注册时间：">
                    <el-date-picker v-model="filter.time"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    :default-time="['00:00:01', '23:59:59']"
                                    size="mini"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    type="daterange"></el-date-picker>
                </el-form-item>
                <el-form-item label="机构名称：">
                    <el-input v-model="filter.orgName" clearable size="mini" placeholder="机构名称"></el-input>
                </el-form-item>
                <el-form-item label="手机号：">
                    <el-input v-model="filter.userName" clearable size="mini" placeholder="手机号"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <router-link to="/admin/LT-DDXT-TJCG" style="margin:0 10px">
                        <el-button size="mini" type="warning">添加超管</el-button>
                    </router-link>
                    <el-button size="mini" type="primary" @click="exportData">导出</el-button>
                </el-form-item>
            </el-form>
        </crumbs>
        <div class="contents">
            <el-table :data="list"
                      border
                      style="width: 100%">
                <el-table-column
                        type="index"
                        :index="indexMethod"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="orgGmtCreate"
                        label="注册时间"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="orderNum"
                        label="认证申请单号"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="linkman"
                        label="超管名称">
                </el-table-column>
                <el-table-column
                        prop="orgName"
                        label="所在机构"
                        width="120"
                >
                </el-table-column>
                <el-table-column
                        prop="position"
                        label="岗位名称">
                </el-table-column>
                <el-table-column
                        prop="userName"
                        label="手机号"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="email"
                        label="邮箱">
                </el-table-column>
                <el-table-column
                        prop="authStatusStr"
                        label="状态">
                </el-table-column>
                <el-table-column
                        label="审核状态"
                        width="110">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="showPassDialog(scope.row,1)">通过</el-button>
                        <span style="margin:0 3px;">|</span>
                        <el-popover placement="bottom-start"
                                width="380"
                                v-model="scope.row.visible"
                        >
                            <el-form>
                                <span class="closeDate" @click="scope.row.visible= !scope.row.visible">×</span>
                                <h4>原因说明</h4>
                                <el-form-item>
                                    <el-radio-group v-model="type" @change="rejectReason=''" v-if="reasons&&reasons.length">
                                        <p v-for="(it,index) in reasons" style="margin: 10px 0;">
                                            <el-radio :label="it.key">{{it.name}}</el-radio>
                                        </p>
                                    </el-radio-group>
                                </el-form-item>
                                <el-form-item>
                                    <el-input v-model="rejectReason" @focus="type=''" type="textarea" placeholder="其他原因说明"></el-input>
                                </el-form-item>
                                <el-form-item>
                                    <el-button type="primary" @click="handleApply(scope.row,0)">完成</el-button>
                                </el-form-item>
                            </el-form>
                            <el-button slot="reference" size="mini" >不通过</el-button>
                        </el-popover>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination v-if="total>pageSize"
                           :current-page.sync="currentPage"
                           @current-change="currentChange"
                           background
                           :page-size="pageSize"
                           layout="total,prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
  import api from '@api/admin.js';
  import utils from '@api/utils.js';

  export default {
    name: 'super-admin',
    data: function () {
      return {
        visible: false,
        reasons: [],
        rejectReason: '',
        type:'',
        currentId: '',
        status:'',
        list: [],
        cacheData: {},
        currentPage: 1,
        pageSize: 10,
        total: 0,
        filter: {
          time: '',
          orgName: '',
          userName: ''
        }
      }
    },
    created() {
      this.getDataList();
      const enums = JSON.parse(localStorage.getItem('enumsData'));      //审核不通过原因枚举
      this.reasons = enums&&enums.accdenEnum;
    },
    methods: {
      showPassDialog(row,status){        //打开通过弹窗
        this.$alert('操作不可逆！确定要通过审核吗？', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            if(action == 'confirm'){
              this.currentId = row.userId;
              this.status = status;
              const param = {
                userId: row.userId,
                orderNum: row.orderNum
              };
              api.auditUserInfoAllPass(param).then((res)=>{
                if(res.data.code=='0'){
                  this.currentPage = 1;
                  this.$message({
                    message:'操作成功',
                    type:'success'
                  });
                  this.getDataList(); //操作成功刷新列表
                }else{
                  this.$message({
                    message:res.data.msg,
                    type:'warning'
                  });
                }
              });
            }
          }
        });
      },
      handleApply(row){
        const param = {
          rejectComment: this.rejectReason,
          rejectType: this.type,
          userId: row.userId,
          orderNum:row.orderNum
        };
        if(this.type===''&&this.rejectReason===''){
          this.$message({
            showClose: true,
            message:'请选择/填写不通过原因',
            type:'warning'
          });
          return;
        }
        if(this.rejectReason.length>100){
          this.$message({
            showClose: true,
            message:'不通过原因最多可输入100字',
            type:'warning'
          });
          return;
        }
        api.auditUserInfoAll(param).then((res)=>{
          if(res.data.code=='0'){
            this.currentPage = 1;
            this.$message({
              showClose: true,
              message:'操作成功',
              type:'success'
            });

          }else{
            this.$message({
              showClose: true,
              message:res.data.msg,
              type:'warning'
            });
          }
          this.getDataList(); //操作后刷新列表
        });
      },
      indexMethod(index) {
        return ((this.currentPage - 1) * this.pageSize) + index + 1;
      },
      filterDatas(){
        this.currentPage = 1;
        this.getDataList();
      },
      getDataList() {
        const param = this.getFilterItems();
        api.getAuthentication(param).then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            this.list = data.records.map((item) => {
              return {...item, visible:false}
            });       //当前页显示的数据
            this.cacheData[param.current] = data.records;       //缓存数据
            this.total = data.total;
          }
        }).catch((error) => {
          console.log(error);
        });
      },
      currentChange(next) {          //切换当前页
        this.currentPage = next;
        if (this.cacheData[next]) {       //如果已请求过该页数据，则使用缓存不重复请求
          this.list = this.cacheData[next];
        } else {
          this.getDataList();
        }
      },
      getFilterItems() {           //筛选
        const time = this.filter.time;
        const startTime = time&&time[0]||undefined;
        const endTime = time&&time[1]||undefined;
        const param = {
          startTime,
          endTime,
          userName: this.filter.userName||undefined,
          orgName: this.filter.orgName||undefined,
          current: this.currentPage,
          size: this.pageSize
        };
        return param;
      },
      exportData() {             //导出
        const canExport = utils.exportCondition(this.filter.time);
        if(typeof canExport == 'string'){
          this.$message({
            message:canExport,
            type:'warning'
          });
          return;
        }
        const param = this.getFilterItems();
        api.exportKema(param).then((res) => {
          if (res.data.code = '0') {
            utils.downloadExportedData(res.data, '审核超管信息表.xls');
            this.$message({message: "导出成功", type: 'success'});
          } else {
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        }).catch((error) => {
          this.$message({
            message: "服务器正忙...",
            type: 'warning'
          });
        });
      }
    }
  }
</script>

<style lang="less">
    @import "../../less/admin.less";
    .closeDate {
      position: absolute;
      top: 0px;
      right: 7px;
      cursor: pointer;
      font-size: 20px;
    }
</style>