<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="提现者姓名" prop="nameValue">
        <el-input
          v-model="queryParams.nameValue"
          placeholder="请输入提现者姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable size="small">
          <el-option label="待审核" value="0" />
          <el-option label="审核通过" value="1" />
          <el-option label="审核不通过" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="钱包类型" prop="moneyType">
        <el-select v-model="queryParams.moneyType" placeholder="请选择钱包类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.money_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户标识" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="flex_one tr">
        <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="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleBatchAudit(1)"
          v-hasPermi="['model:AppletAmountLog:audit']"
        >批量通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          @click="handleBatchAudit(2)"
          v-hasPermi="['model:AppletAmountLog:audit']"
        >批量拒绝</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['model:AppletAmountLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AppletAmountLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识" align="center" prop="id" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="提现者姓名" align="center" prop="nameValue" />
      <el-table-column label="用户标识" align="center" prop="userId" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="提现金额" align="center" prop="amount" width="120">
        <template slot-scope="scope">
          <span style="color: #f56c6c; font-weight: bold;">￥{{ scope.row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="钱包类型" align="center" prop="moneyType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.money_type" :value="scope.row.moneyType"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditStatus == 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.auditStatus == 1" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.auditStatus == 2" type="danger">审核不通过</el-tag>
          <el-tag v-else type="info">未知状态</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="state" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0" type="warning">未到账</el-tag>
          <el-tag v-else-if="scope.row.state == 1" type="success">已到账</el-tag>
          <el-tag v-else-if="scope.row.state == 2" type="danger">失败</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="提现参数" align="center" prop="packageInfo" width="200" show-overflow-tooltip /> -->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['model:AppletAmountLog:view']"
          >查看</el-button>
          <el-button
            v-if="scope.row.auditStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAudit(scope.row, 1)"
            v-hasPermi="['model:AppletAmountLog:audit']"
            style="color: #67C23A;"
          >通过</el-button>
          <el-button
            v-if="scope.row.auditStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleAudit(scope.row, 2)"
            v-hasPermi="['model:AppletAmountLog:audit']"
            style="color: #F56C6C;"
          >拒绝</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="60%" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" class="dialog-form-two">
        <el-row>
          <el-col :span="12">
            <el-form-item label="唯一标识">
              <el-input v-model="form.id" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              <el-input v-model="form.createTime" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="提现者姓名">
              <el-input v-model="form.nameValue" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户标识">
              <el-input v-model="form.userId" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题">
              <el-input v-model="form.title" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提现金额">
              <el-input v-model="form.amount" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="钱包类型">
              <dict-tag :options="dict.type.money_type" :value="form.moneyType"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核状态">
              <el-tag v-if="form.auditStatus == 0" type="warning">待审核</el-tag>
              <el-tag v-else-if="form.auditStatus == 1" type="success">审核通过</el-tag>
              <el-tag v-else-if="form.auditStatus == 2" type="danger">审核不通过</el-tag>
              <el-tag v-else type="info">未知状态</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="处理状态">
              <el-tag v-if="form.state === 0" type="warning">处理中</el-tag>
              <el-tag v-else-if="form.state === 1" type="success">成功</el-tag>
              <el-tag v-else-if="form.state === 2" type="danger">失败</el-tag>
              <el-tag v-else type="info">未知</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型">
              <el-tag v-if="form.type === 0" type="success">收入</el-tag>
              <el-tag v-else-if="form.type === 1" type="warning">支出</el-tag>
              <el-tag v-else type="info">未知</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.auditRemark">
          <el-col :span="24">
            <el-form-item label="审核备注">
              <el-input v-model="form.auditRemark" type="textarea" rows="3" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.errorInfo">
          <el-col :span="24">
            <el-form-item label="错误信息">
              {{ form.errorInfo }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="auditTitle" :visible.sync="auditOpen" width="40%" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="120px">
        <el-form-item label="审核状态">
          <el-tag v-if="auditForm.auditStatus === 1" type="success">通过</el-tag>
          <el-tag v-else-if="auditForm.auditStatus === 2" type="danger">不通过</el-tag>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="auditForm.auditRemark" type="textarea" rows="4" placeholder="请输入审核备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppletAmountLog, getAppletAmountLog, exportAppletAmountLog, auditAppletAmountLog } from "@/api/model/AppletAmountLog";

export default {
  name: "AppletAmountLogAudit",
  dicts: ['money_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 金额明细表格数据
      AppletAmountLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核弹出层标题
      auditTitle: "",
      // 是否显示审核弹出层
      auditOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nameValue: null,
        auditStatus: null,
        moneyType: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 审核表单参数
      auditForm: {},
      // 审核表单校验
      auditRules: {
        auditRemark: [
          // { required: true, message: "审核备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询提现审核列表 */
    getList() {
      this.loading = true;
      // 只查询提现类型的记录 (type=1表示支出，这里是提现记录)
      const params = { ...this.queryParams, type: 1, audit: true}
      listAppletAmountLog(params).then(response => {
        // 过滤出需要审核的提现记录（有auditStatus字段且nameValue不为空的记录）
        this.AppletAmountLogList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        amount: null,
        type: null,
        userId: null,
        auditStatus: null,
        moneyType: null,
        nameValue: null,
        packageInfo: null,
        createTime: null,
        state: null,
        auditRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      getAppletAmountLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看提现详情";
      });
    },
    /** 审核按钮操作 */
    handleAudit(row, status) {
      this.auditForm = {
        id: row.id,
        auditStatus: status,
        auditRemark: ""
      };
      this.auditOpen = true;
      this.auditTitle = status === 1 ? "审核通过" : "审核拒绝";
    },
    /** 批量审核操作 */
    handleBatchAudit(status) {
      if (this.ids.length === 0) {
        this.$modal.msgError("请选择要审核的记录");
        return;
      }
      this.auditForm = {
        ids: this.ids,
        auditStatus: status,
        auditRemark: ""
      };
      this.auditOpen = true;
      this.auditTitle = status === 1 ? "批量审核通过" : "批量审核拒绝";
    },
    /** 提交审核 */
    submitAudit() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          auditAppletAmountLog(this.auditForm).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 取消审核 */
    cancelAudit() {
      this.auditOpen = false;
      this.auditForm = {};
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = { ...this.queryParams, type: 1 };
      this.$modal.confirm('是否确认导出所有提现审核数据项？').then(() => {
        this.exportLoading = true;
        return exportAppletAmountLog(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.el-tag {
  margin: 0 5px;
}
</style>
