<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="审核时间" prop="authTime">
        <el-date-picker
          clearable
          v-model="queryParams.authTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="选择搜索条件">
        <el-select v-model="queryParams.searchField" placeholder="请选择搜索条件" clearable>
          <el-option
            v-for="dict in dict.type.buyer_auth_search_fields"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键词搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择审核人" clearable>
          <el-option
            v-for="user in userList"
            :key="user.userId"
            :label="user.nickName"
            :value="user.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="authStatus">
        <el-select v-model="queryParams.authStatus" placeholder="请选择认证类型" clearable>
          <el-option
            v-for="dict in dict.type.buyer_company_auth_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['buyer:info:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['buyer:info:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号" align="center" prop="userName" />
      <el-table-column label="账号状态" align="center" prop="accountStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.buyer_company_account_status" :value="scope.row.accountStatus" />
        </template>
      </el-table-column>
      <el-table-column label="认证企业名称" align="center" prop="name" />
      <el-table-column label="信用代码" align="center" prop="code" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="联系人电话" align="center" prop="phone" />
      <el-table-column label="营业执照" align="center" prop="fileNames">
        <template slot-scope="scope">
          <el-image :src="baseUrl + scope.row.fileNames" :preview-src-list="[baseUrl+scope.row.fileNames]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="reviewer" />
      <el-table-column label="审核时间" align="center" prop="authTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.authTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="authStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.buyer_company_auth_status" :value="scope.row.authStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,'0')"
            v-hasPermi="['buyer:info:edit']"
            v-if="scope.row.authStatus === '0'"
          >
            去审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,'2')"
            v-hasPermi="['buyer:info:edit']"
            v-if="scope.row.authStatus === '2'"
          >
            查看原因
          </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"
    />

    <!-- 添加或修改会员企业信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-descriptions title="认证信息" :column="1" :labelStyle="{color:'#aaa'}" v-if="currentStatus === '0'">
        <el-descriptions-item label="营业执照">
          <el-image
            style="width: 100px; height: 100px"
            :src="baseUrl+form.fileNames"
            fit="contain"
            :preview-src-list="[baseUrl+form.fileNames]"
          ></el-image>
        </el-descriptions-item>
        <el-descriptions-item label="企业名称">{{ form.name }}</el-descriptions-item>
        <el-descriptions-item label="信用代码">{{ form.code }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ form.contacts }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ form.phone }}</el-descriptions-item>
      </el-descriptions>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-else-if="currentStatus === '1'">
        <el-form-item label="拒绝理由" prop="remarks">
          <el-input v-model="form.remarks" type="text" placeholder="请输入拒绝理由" />
        </el-form-item>
      </el-form>
      <el-descriptions :column="1" :labelStyle="{color:'#aaa'}" v-if="currentStatus === '2'">
        <el-descriptions-item label="拒绝理由">{{ form.remarks }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(currentStatus === '0' ? '1' : '2')" v-if="currentStatus !=='2'">{{ currentStatus === '0' ? '通过' : '提交' }}</el-button>
        <el-button type="danger" @click="submitForm('3')" v-if="currentStatus === '0'">拒绝</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from '@/api/buyer/company'
import { listUser } from '@/api/system/user'

export default {
  name: 'Info',
  dicts: ['buyer_company_account_status', 'buyer_company_auth_status', 'buyer_company_type', 'buyer_auth_search_fields'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员企业信息表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        authStatus: null,
        searchField: null,
        keyword: null,
        authTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remarks: [
          { required: true, message: '拒绝理由不能为空', trigger: 'blur' }
        ]
      },
      userList: [],
      currentStatus: ''
    }
  },
  created() {
    this.getList()
    listUser({ pageNum: 1, pageSize: 99999 }).then(response => {
      this.userList = response.rows
    })
  },
  methods: {
    /** 查询会员企业信息列表 */
    getList() {
      this.loading = true
      listInfo(this.addSearchField(this.queryParams, this.queryParams.searchField, this.queryParams.keyword)).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      if (this.currentStatus === '1') {
        this.currentStatus = '0'
        return
      }
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        remarks: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
        pageNum: 1,
          pageSize: 10,
          userId: null,
          authStatus: null,
          searchField: null,
          keyword: null,
          authTime: null
      }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row, status) {
      this.currentStatus = status
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '认证审核'
      })
    },
    /** 提交按钮 */
    submitForm(status) {
      if (status === '3') {
        // 展示拒绝理由form
        this.currentStatus = '1'
        return
      }
      this.form.authStatus = status
      updateInfo(this.form).then(response => {
        this.$modal.msgSuccess(`${status === '1' ? '已通过' : '已拒绝'}`)
        this.open = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除会员企业信息选中的数据项？').then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buyer/company/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
