<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <span>授权机构名称:</span>
      <el-input
        v-model="listQuery.authParty"
        placeholder="授权机构名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="getList"
      />
      <span>审核状态:</span>
      <el-select
        v-model="listQuery.auditStatus"
        placeholder="审核状态"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="getList"
      >
        <el-option label="全部" value="" />
        <el-option label="待审核" :value="2" />
        <el-option label="审核通过" :value="1" />
        <el-option label="审核未通过" :value="0" />
      </el-select>
      <span>商品信息审核状态:</span>
      <el-select
        v-model="listQuery.lowerAuditStatus"
        placeholder="供应商类型"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="getList"
      >
        <el-option label="全部" value="" />
        <el-option label="已完成审核" :value="1" />
        <el-option label="未完成审核" :value="0" />
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
    </div>
    <!-- 搜索区域END -->
    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      highlight-current-row
      cell-class-name="table-cell"
    >
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column prop="authParty" label="授权机构" align="center" />
      <el-table-column label="审核状态" width="220px" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.auditStatus === 2" size="small" type="warning">待审核</el-tag>
          <el-tag v-if="row.auditStatus === 1" size="small" type="success">审核通过</el-tag>
          <!-- 未通过原因展示 -->
          <template v-if="row.auditStatus === 0">
            <!-- <span class="status danger">审核未通过&nbsp;</span> -->
            <el-popover
              placement="top-start"
              title="审核说明"
              width="400"
              trigger="hover"
              :content="row.invalidReason"
            >
              <el-tag slot="reference" size="small" type="danger" class="reason">
                <span>审核未通过</span>
                <span class="el-icon-question status danger " />
              </el-tag>
            </el-popover>
            <!-- 未通过原因展示END -->
          </template>
        </template>
      </el-table-column>
      <el-table-column label="商品信息审核状态" width="220px" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.lowerAuditStatus === 0" size="small" type="danger">未完成审核</el-tag>
          <el-tag v-if="row.lowerAuditStatus === 1" size="small" type="success">已完成审核</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="审核时间" width="250px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.auditStatus!==2">{{ row.auditTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>

      <el-table-column label="审核人" align="center" width="200px">
        <template slot-scope="{row}">
          <span v-if="row.auditStatus!==2">{{ row.auditBy }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="240px" align="center">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.auditStatus === 2"
            type="warning"
            size="mini"
            style="margin-right:5px"
            icon="el-icon-s-check"
            @click="handleShowDialog(row)"
          >审核</el-button>
          <el-badge :hidden="row.lowerAuditStatus === 1" :value="row.waitAuditNum" :max="99">
            <el-button icon="el-icon-s-shop" type="primary" size="mini" @click="$_navigationTo(`shop-list?authId=${row.authId}`)">商品信息审核</el-button>
          </el-badge>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域END -->
    <!-- 页码 -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <!-- 审核弹窗 -->
    <el-dialog title="授权机构审核" :visible.sync="dialogVisible" width="450px" @close="dialogClosed">
      <div class="auth-info">
        <div class="auth-name">
          <i>授权机构：</i>
          <b>{{ dialogData.authParty }}</b>
        </div>
        <el-form ref="formRef" :model="dialogData" label-width="85px" :rules="dialogFormRules">
          <el-form-item label="审核状态：">
            <el-radio-group v-model="dialogData.auditStatus">
              <el-radio :label="1">通过</el-radio>
              <el-radio :label="0">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="dialogData.auditStatus === 0" label="原因：" prop="invalidReason">
            <el-input v-model="dialogData.invalidReason" type="textarea" placeholder="请说明原因" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="auditStatusHandle">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { fecthAuthList, auditAuth } from '@/api/auth'
import { formatDate } from '@/utils'
import { mapGetters, mapMutations } from 'vuex'
export default {
  components: { Pagination },
  filters: {
    formatTime(time) {
      if (!time) {
        return ''
      }
      return formatDate(time, 'yyyy-MM-DD HH:mm:ss')
    }
  },
  data() {
    return {
      // 是否显示dialog
      dialogVisible: false,
      listLoading: false,
      // 查询参数
      listQuery: {
        listType: 2,
        authParty: '', // 授权机构
        authUserId: '', // 供应商用户id
        pageNum: 1, // 页码
        pageSize: 10, // 分页
        auditStatus: '', // 审核状态
        lowerAuditStatus: '' // 下属内容审核状态
      },
      total: 0,
      dialogData: {
        auditBy: '', // 审核人
        authId: '', // 授权机构id
        authParty: '', // 授权机构名称
        auditStatus: 1, // 审核状态
        invalidReason: '' // 审核信息
      },
      dialogFormRules: {
        invalidReason: {
          required: true,
          message: '不通过原因不能为空',
          tigger: 'blur'
        }
      },
      // 机构信息审核列表
      list: []
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.listQuery.authUserId = this.$route.query.authUserId
    this.getList()
  },
  methods: {
    ...mapMutations({ setMessageState: 'webSocket/SET_MESSAGE_STATE' }),
    // 获取授权列表
    getList() {
      this.listLoading = true
      fecthAuthList(this.listQuery).then(response => {
        if (response.code !== 0) return
        const { list, total } = response.data
        this.list = list
        this.total = total
      }).finally(() => {
        this.listLoading = false
      })
    },
    // 审核
    auditStatusHandle() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          // 指定审核人
          this.dialogData.auditBy = this.authUserId
          // 提交审核信息
          auditAuth(this.dialogData).then(res => {
            if (res.code !== 0) return
            this.$message.success(res.data)
            this.setMessageState({ id: this.dialogData.authId, type: 1 })
          }).finally(() => {
            this.dialogVisible = false
            this.getList()
          })
        }
      })
    },
    // 审核对话框
    handleShowDialog(item) {
      console.log(item)
      this.dialogData.authParty = item.authParty
      this.dialogData.authId = item.authId
      this.dialogVisible = true
    },
    // 审核对话框关闭
    dialogClosed() {
      this.dialogData.authParty = ''
      this.dialogData.reviewStatus = 1
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  span {
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
    font-size: 14px;
  }
  .el-button {
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
  }
  .el-input,
  .el-select {
    margin-right: 10px;
    margin-left: 10px;
  }
}

.auth-info {
  .auth-name {
    font-size: 16px;
    padding-bottom: 25px;
    border-bottom: 1px solid #ddd;
    margin-bottom: 25px;
  }
}
</style>
