<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="充值编号" prop="rechargeCardId">
                <el-input v-model="queryParams.rechargeCardId" placeholder="请输入充值编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="充值卡号" prop="cardNo">
                <el-input v-model="queryParams.cardNo" placeholder="请输入充值卡号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="订单编号" prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="客户名称" prop="appUserName">
                <el-input v-model="queryParams.appUserName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="下单时间" prop="queryCreateTime">
                <el-date-picker clearable v-model="queryParams.queryCreateTime" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="下单时间" end-placeholder="结束日期"> </el-date-picker>
            </el-form-item>
            <el-form-item label="消费门店" label-width="120px" prop="orgIndex" v-if="userInfoVO.userType == '00'">
                <el-select v-model="queryParams.orgIndex" placeholder="请选择门店" clearable>
                    <el-option v-for="store in storeList" :key="store.id + store.sourceType" :label="store.name" :value="store.id + ',' + store.sourceType"> </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @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" @click="handleExport" v-hasPermi="['recharge:order:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/recharge/card/batchRecord/listCardUseRecordByBatchNo" :request-params="queryParams" ref="pagination">
            <el-table-column label="充值卡编号" align="center" prop="rechargeCardId" width="190" />
            <el-table-column label="充值卡号" align="center" prop="cardNo" width="190" />
            <el-table-column label="客户名称" align="center" prop="appUserName" width="120" />
            <el-table-column label="消费订单号" align="center" prop="orderNo" width="190" />
            <el-table-column label="订单类型" align="center" prop="title" width="120" />
            <el-table-column label="下单时间" align="center" prop="createTime" width="150" />
            <el-table-column label="消费门店" align="center" width="100">
                <template slot-scope="scope">
                    <span v-for="store in storeList">
                        {{ store.sourceType == scope.row.sourceType && store.id == scope.row.orgId ? store.name : '' }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="消费总金额（元）" align="center" prop="payTotalAmount" width="130">
                <template slot-scope="scope">
                    {{ scope.row.isRefund == 'Y' && scope.row.payTotalAmount.toString() != '0' ? '退款：' + scope.row.payTotalAmount.toString().replace('-', '') : scope.row.payTotalAmount.toString().replace('-', '') }}
                </template>
            </el-table-column>
            <el-table-column label="充值余额支付金额（元）" align="center" prop="rechargePayAmount" width="170">
                <template slot-scope="scope">
                    {{ scope.row.isRefund == 'Y' && scope.row.rechargePayAmount.toString() != '0' ? '退款：' + scope.row.rechargePayAmount.toString().replace('-', '') : scope.row.rechargePayAmount.toString().replace('-', '') }}
                </template>
            </el-table-column>
            <el-table-column label="赠送余额支付金额（元）" align="center" prop="givePayAmount" width="170">
                <template slot-scope="scope">
                    {{ scope.row.isRefund == 'Y' && scope.row.givePayAmount.toString() != '0' ? '退款：' + scope.row.givePayAmount.toString().replace('-', '') : scope.row.givePayAmount.toString().replace('-', '') }}
                </template>
            </el-table-column>
            <el-table-column label="福利金支付金额（元）" align="center" prop="welfarePayAmount" width="180">
                <template slot-scope="scope">
                    {{ scope.row.isRefund == 'Y' && scope.row.welfarePayAmount.toString() != '0' ? '退款：' + scope.row.welfarePayAmount.toString().replace('-', '') : scope.row.welfarePayAmount.toString().replace('-', '') }}
                </template>
            </el-table-column>
        </Page>
    </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, getPayDetailById } from '@/api/recharge/order'
import { allOrg } from '@/api/system/store'
import { refundOrderGoods } from '@/api/order/goods'
import { refundRecharge } from '@/api/recharge/refund'
import { listCardUseRecordByBatchNo } from '@/api/recharge/batchRecord'

export default {
    name: 'useRecord',
    dicts: ['recharge_pay_type', 'recharge_order_status', 'sys_yes_no'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 充值订单表格数据
            recordList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                rechargeCardId: null,
                rechargeStatus: null,
                beginCreateTime: null,
                endCreateTime: null,
                queryCreateTime: null,
                orgIndex: null,
                cardNo: null,
                appUserName: null,
                orderNo: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            userInfoVO: null,
            storeList: [],
            factoryList: [],
            batchNo: null,
            orgList: []
        }
    },
    created() {
        this.batchNo = this.$route.params.batchNo
        allOrg().then((res) => {
            this.orgList = res.data
            this.orgList.forEach((org) => {
                if (org.sourceType == '01') {
                    this.factoryList.push(org)
                } else {
                    this.storeList.push(org)
                }
            })
        })
        this.handleUser()
        this.getList()
    },
    methods: {
        handleUser() {
            this.userInfoVO = this.getUserInfo()
        },
        /** 查询充值订单列表 */
        getList() {
            this.queryParams.batchNo = this.batchNo
            if (this.queryParams.orgIndex != null) {
                this.queryParams.orgId = this.queryParams.orgIndex.split(',')[0]
                this.queryParams.sourceType = this.queryParams.orgIndex.split(',')[1]
            } else {
                delete this.queryParams.orgId
                delete this.queryParams.sourceType
            }
            const param = { ...this.queryParams }
            if (this.queryParams.queryCreateTime != null) {
                param.beginCreateTime = this.queryParams.queryCreateTime[0]
                param.endCreateTime = this.queryParams.queryCreateTime[1]
                param.queryCreateTime = null
            }
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {}
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            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
        },
        /** 支付明细详情 */
        handleDetail(row) {
            this.reset()
            const id = row.id
            getPayDetailById(id).then((response) => {
                this.payDetailList = response.data
                if (this.payDetailList.length == 1 && this.payDetailList[0] == null) {
                    this.payDetailList = null
                }
                this.open = true
                this.title = '支付明细详情'
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除充值订单编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delOrder(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.downloadJson(
                'mapi/recharge/card/batchRecord/exportCardUseRecordByBatchNo',
                {
                    ...this.queryParams
                },
                `use_record_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
