<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.withdraw_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户类型" prop="accountType">
        <el-select v-model="queryParams.accountType" placeholder="请选择账户类型" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.account_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号" prop="accountNo">
        <el-input
          v-model="queryParams.accountNo"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号姓名" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账号姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="accountMobile">
        <el-input
          v-model="queryParams.accountMobile"
          placeholder="请输入账户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="accountIdCard">
        <el-input
          v-model="queryParams.accountIdCard"
          placeholder="请输入账户身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行名称" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入银行名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡面名称" prop="bankCardName">
        <el-input
          v-model="queryParams.bankCardName"
          placeholder="请输入银行卡面名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="verifyUserName">
        <el-input
          v-model="queryParams.verifyUserName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核意见" prop="verifyRemark">
        <el-input
          v-model="queryParams.verifyRemark"
          placeholder="请输入审核意见"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="请选择服务费类型" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.withdraw_service_type"
            :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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-has-permi="['bst:withdraw:add']"
        >申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-has-permi="['bst:withdraw:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'no'">
              {{d.row.no | dv}}
              <dict-tag :options="dict.type.account_type" :value="d.row.accountType" size="mini" style="margin-left: 4px"/>
              <dict-tag :options="dict.type.withdraw_status" :value="d.row.status" size="mini" style="margin-left: 4px"/>
            </template>
            <template v-else-if="column.key === 'accountNo'">
              <image-preview v-if="d.row.accountType === AccountType.QR" :src="d.row.accountNo" :width="50" :height="50" />
              <span v-else>{{d.row.accountNo}}</span>
            </template>
            <template v-else-if="column.key === 'serviceCharge'">
              {{d.row.serviceCharge | fix2 | dv}} 元<br/>
              <dict-tag :options="dict.type.withdraw_service_type" :value="d.row.serviceType" size="mini" style="margin-left: 4px"/>
              <template v-if="d.row.servicePoint != null">（{{d.row.servicePoint | fix2 | dv}} %）</template>
            </template>
            <template v-else-if="['amount', 'arrivalAmount'].includes(column.key)">
              {{d.row[column.key] | fix2 | dv}} 元
            </template>
            <template v-else-if="column.key === 'userName'">
              <user-link :id="d.row.userId" :text="d.row.userName" />
            </template> 
            <template v-else-if="column.key === 'verifyUserName'">
              <user-link :id="d.row.verifyUserId" :text="d.row.verifyUserName" />
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
      <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-view"
            @click="handleView(scope.row)"
            v-has-permi="['bst:withdraw:query']"
          >详情 / 审核</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"
    />

    <withdraw-verify-dialog
      :visible.sync="verifyDialogVisible"
      :id="selectedId"
      @success="getList"
    />

    <withdraw-add-dialog
      :visible.sync="addDialogVisible"
      @success="getList"
    />
  </div>
</template>

<script>
import { listWithdraw} from "@/api/bst/withdraw";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import WithdrawVerifyDialog from '@/views/bst/withdraw/components/WithdrawVerifyDialog.vue';
import WithdrawAddDialog from '@/views/bst/withdraw/components/WithdrawAddDialog.vue';
import { AccountType } from '@/utils/enums';
import UserLink from '@/components/Business/User/UserLink.vue'

  // 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "Withdraw",
  mixins: [$showColumns],
  dicts: ['account_type', 'withdraw_service_type', 'withdraw_status'],
  components: {FormCol, WithdrawVerifyDialog, WithdrawAddDialog, UserLink},
  props: {
    query: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      AccountType,
      span: 24,
      selectedId: null,
      // 字段列表
      columns: [
        {key: 'id', visible: false, label: 'ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: "80"},
        {key: 'no', visible: true, label: "提现单号", minWidth: "180", sortable: true, overflow: false, align: 'left', width: null},
        {key: 'userName', visible: true, label: '用户', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'amount', visible: true, label: '申请金额', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'arrivalAmount', visible: true, label: '到账金额', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'serviceCharge', visible: true, label: '服务费', minWidth: null, sortable: true, overflow: false, align: 'center', width: "150"},
        {key: 'accountNo', visible: true, label: '账号', minWidth: null, sortable: true, overflow: false, align: 'center', width: "200"},
        {key: 'accountName', visible: false, label: '姓名', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'accountMobile', visible: false, label: '手机号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'accountIdCard', visible: false, label: '身份证号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'bankName', visible: false, label: '银行', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'bankCardName', visible: false, label: '卡名', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'bankType', visible: false, label: '卡类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'verifyUserName', visible: true, label: '审核人', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'verifyTime', visible: true, label: '审核时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'verifyRemark', visible: true, label: '审核意见', minWidth: null, sortable: true, overflow: true, align: 'center', width: null},
        {key: 'createTime', visible: true, label: '申请时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提现表格数据
      withdrawList: [],
      // 是否显示弹出层
      verifyDialogVisible: false,
      addDialogVisible: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        id: null,
        userId: null,
        accountId: null,
        accountType: null,
        accountNo: null,
        accountName: null,
        accountMobile: null,
        accountIdCard: null,
        bankName: null,
        bankCardName: null,
        bankType: null,
        verifyUserId: null,
        verifyRemark: null,
        serviceType: null,
      }
    };
  },
  created() {
    Object.assign(this.queryParams, this.query, this.$route.query);
    this.getList();
  },
  methods: {
    handleView(row) {
      this.selectedId = row.id;
      this.verifyDialogVisible = true;
    },
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询提现列表 */
    getList() {
      this.loading = true;
      listWithdraw(this.queryParams).then(response => {
        this.withdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.addDialogVisible = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/withdraw/export', {
        ...this.queryParams
      }, `withdraw_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
