<template>
  <div class="app-container" v-loading="loading">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="提现单号" prop="billNo">
        <el-input
          v-model="queryParams.billNo"
          placeholder="请输入提现单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择状态" @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="createDateRange">
        <el-date-picker
          v-model="queryParams.createDateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到账时间" prop="payDateRange">
        <el-date-picker
          v-model="queryParams.payDateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现方式" prop="channelId">
        <el-select v-model="queryParams.channelId" clearable placeholder="请选择提现方式" @change="handleQuery">
          <el-option v-for="dict in dict.type.withdraw_channel" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="打款方式" prop="withdrawType">
        <el-select v-model="queryParams.withdrawType" clearable placeholder="请选择打款方式" @change="handleQuery">
          <el-option v-for="dict in dict.type.withdraw_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="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:bill:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" align="center" label="#" />
      <el-table-column label="提现单号" align="center" prop="billNo" width="180">
        <el-link slot-scope="d" @click="handleView(d.row)" type="primary">{{d.row.billNo}}</el-link>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="d">
          <dict-tag :value="d.row.status" :options="dict.type.withdraw_status"/>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center">
        <template slot-scope="d">
          {{d.row.money | money}} 元
        </template>
      </el-table-column>
      <el-table-column label="到账金额" align="center">
        <template slot-scope="d">
          {{d.row.arrivalAmount | money}} 元
        </template>
      </el-table-column>
      <el-table-column label="服务费" align="center">
        <template slot-scope="d">
          {{d.row.serviceCharge | money}} 元
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="到账时间" align="center" prop="payTime" width="180"/>
      <el-table-column label="提现方式" align="center">
        <template slot-scope="d">
          <dict-tag :value="d.row.channelId" :options="dict.type.withdraw_channel"/>
        </template>
      </el-table-column>
      <el-table-column label="打款方式" align="center">
        <template slot-scope="d">
          <dict-tag :value="d.row.withdrawType" :options="dict.type.withdraw_type"/>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="remark"/>
      <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)"
          >详情</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-drawer
      :with-header="false"
      :visible.sync="showDetail"
      direction="rtl"
      size="800px"
      destroy-on-close
    >
      <mch-withdraw-detail :id="row.billId"/>
    </el-drawer>

  </div>
</template>

<script>
import {
  getWithdraw,
  delWithdraw,
  addBill,
  updateBill
} from "@/api/system/withdraw";
import UserLink from '@/components/Business/SmUser/UserLink.vue'
import { mchListWithdraw } from '@/api/mch/withdraw'
import MchWithdrawDetail from '@/views/mch/withdraw/detail.vue'

export default {
  name: "MchWithdraw",
  components: { MchWithdrawDetail, UserLink },
  dicts: ['withdraw_status', 'withdraw_type', 'withdraw_channel'],
  data() {
    return {
      showDetail: false,
      row: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值记录表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        userName: null,
        deviceName: null,
        mchName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查看详情
    handleView(row) {
      this.row = row;
      this.showDetail = true;
    },
    /** 查询充值记录列表 */
    getList() {
      this.loading = true;
      mchListWithdraw(this.queryParams).then(response => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        userId: null,
        type: null,
        deviceId: null,
        landlordId: null,
        money: null,
        arrivalAmount: null,
        serviceCharge: null,
        createTime: null,
        remark: 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.billId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const billId = row.billId || this.ids
      getWithdraw(billId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.billId != null) {
            updateBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids;
      this.$modal.confirm('是否确认删除充值记录编号为"' + billIds + '"的数据项？').then(function() {
        return delWithdraw(billIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bill/export', {
        ...this.queryParams
      }, `bill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
