<template>
    <div class="app-container">
        <el-card class="box-card" shadow="never">
            <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="100px">
                <el-form-item label="充值编号" prop="rechargeCardId">
                    <el-input
                        v-model="queryParams.rechargeCardId"
                        placeholder="请输入充值编号"
                        clearable
                        prefix-icon="el-icon-document"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="充值卡号" prop="cardNo">
                    <el-input
                        v-model="queryParams.cardNo"
                        placeholder="请输入充值卡号"
                        clearable
                        prefix-icon="el-icon-document"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="订单编号" prop="orderNo">
                    <el-input
                        v-model="queryParams.orderNo"
                        placeholder="请输入订单编号"
                        clearable
                        prefix-icon="el-icon-tickets"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="客户名称" prop="appUserName">
                    <el-input
                        v-model="queryParams.appUserName"
                        placeholder="请输入客户名称"
                        clearable
                        prefix-icon="el-icon-user"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="下单时间" prop="queryCreateTime">
                    <el-date-picker
                        v-model="queryParams.queryCreateTime"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="yyyy-MM-dd"
                        style="width: 240px"
                    />
                </el-form-item>
                <el-form-item label="消费门店" prop="orgIndex" v-if="userInfoVO.userType == '00'">
                    <el-select v-model="queryParams.orgIndex" placeholder="请选择门店" clearable style="width: 240px">
                        <el-option v-for="store in storeList" :key="store.id + store.sourceType" :label="store.name" :value="store.id + ',' + store.sourceType" />
                    </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-card>

        <el-card class="box-card table-card" shadow="never">
            <div slot="header" class="clearfix">
                <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>
            </div>

            <Page uri="/mapi/recharge/card/batchRecord/listCardUseRecordByBatchNo" :request-params="queryParams" ref="pagination">
                <el-table-column label="充值卡编号" align="center" prop="rechargeCardId" min-width="190" :show-overflow-tooltip="true" />
                <el-table-column label="充值卡号" align="center" prop="cardNo" min-width="190" :show-overflow-tooltip="true" />
                <el-table-column label="客户名称" align="center" prop="appUserName" min-width="120" />
                <el-table-column label="消费订单号" align="center" prop="orderNo" min-width="190" :show-overflow-tooltip="true" />
                <el-table-column label="订单类型" align="center" prop="title" min-width="120" />
                <el-table-column label="下单时间" align="center" prop="createTime" width="160">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="消费门店" align="center" min-width="120">
                    <template slot-scope="scope">
                        <span v-for="store in storeList" :key="store.id">
                            {{ store.sourceType == scope.row.sourceType && store.id == scope.row.orgId ? store.name : '' }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="消费金额" align="center" min-width="300">
                    <template slot-scope="scope">
                        <div class="amount-info">
                            <el-tag size="mini" type="danger">
                                总金额：¥{{ formatAmount(scope.row.payTotalAmount, scope.row.isRefund) }}
                            </el-tag>
                            <el-tag size="mini" type="success" v-if="scope.row.rechargePayAmount != 0">
                                充值余额：¥{{ formatAmount(scope.row.rechargePayAmount, scope.row.isRefund) }}
                            </el-tag>
                            <el-tag size="mini" type="warning" v-if="scope.row.givePayAmount != 0">
                                赠送余额：¥{{ formatAmount(scope.row.givePayAmount, scope.row.isRefund) }}
                            </el-tag>
                            <el-tag size="mini" type="info" v-if="scope.row.welfarePayAmount != 0">
                                福利金额：¥{{ formatAmount(scope.row.welfarePayAmount, scope.row.isRefund) }}
                            </el-tag>
                        </div>
                    </template>
                </el-table-column>
            </Page>
        </el-card>
    </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(() => {})
        },
        formatAmount(amount, isRefund) {
            if (!amount) return '0'
            const value = amount.toString().replace('-', '')
            return isRefund === 'Y' && amount.toString() !== '0' ? '退款：' + value : value
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    padding: 20px;
    background-color: #f5f7fa;

    .box-card {
        margin-bottom: 20px;

        &.table-card {
            margin-bottom: 0;
        }
    }

    :deep(.el-card__header) {
        padding: 15px 20px;
        border-bottom: 1px solid #ebeef5;
    }

    :deep(.el-card__body) {
        padding: 20px;
    }

    .amount-info {
        .el-tag {
            margin: 0 4px;
            
            &:first-child {
                margin-left: 0;
            }
            
            &:last-child {
                margin-right: 0;
            }
        }
    }
}
</style>
