<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-card class="box-card custom-card">
                    <div slot="header" class="clearfix">
                        <span>选择客户</span>
                        <el-button type="primary" style="float: right" plain icon="el-icon-plus" @click="handleAdd" v-if="userInfoVO.userType == '02'">新建客户 </el-button>
                    </div>
                    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px" @submit.native.prevent>
                        <el-form-item label="手机号" prop="phoneNumber">
                            <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable maxlength="11" @change="searchUser" @keyup.enter.native="searchUser" />
                        </el-form-item>
                        <el-form-item>
                            <!--                            <el-button type="primary" icon="el-icon-full-screen" @click="qrcodeSearchUser">扫码</el-button>-->
                            <el-button type="primary" icon="el-icon-search" @click="searchUser">查询</el-button>
                        </el-form-item>
                    </el-form>

                    <el-descriptions title="用户信息" v-if="appUserInfo" :column="2">
                        <el-descriptions-item label="用户名" span="2">{{ appUserInfo.realName }}</el-descriptions-item>
                        <el-descriptions-item label="手机号" span="2">{{ appUserInfo.phoneNumber }} </el-descriptions-item>
                        <el-descriptions-item label="现金余额">
                            {{ (appUserInfo.rechargeBalance * 100) / 100 }}
                        </el-descriptions-item>
                        <el-descriptions-item label="赠送余额">
                            {{ (appUserInfo.giveBalance * 100) / 100 }}
                        </el-descriptions-item>
                        <el-descriptions-item label="福利金">
                            {{ (appUserInfo.welfareBalance * 100) / 100 }}
                        </el-descriptions-item>

                        <el-descriptions-item label="积分">
                            {{ appUserInfo.pointBalance }}
                        </el-descriptions-item>
                        <el-descriptions-item label="等级">{{ appUserInfo.level }}</el-descriptions-item>
                    </el-descriptions>

                    <Page uri="/mapi/recharge/card/list" :request-params="cardParams" ref="pagination" v-if="appUserInfo" :page-size="5">
                        <el-table-column label="卡号" align="center" prop="cardNo" width="100" />
                        <el-table-column label="储值门店" align="center" prop="rechargeStoreName" width="100">
                            <template slot-scope="scope">
                                {{ scope.row.useBindStoreName == null ? '--' : scope.row.useBindStoreName }}
                            </template>
                        </el-table-column>
                        <el-table-column label="充值金额(元)" align="center" prop="rechargeAmount" width="120" />
                        <el-table-column label="卡内余额（元）" align="center" prop="rechargeAmount" width="120">
                            <template slot-scope="scope">
                                <span style="display: block; text-align: left">现金金额: {{ scope.row.rechargeBalance }}</span>
                                <span style="display: block; text-align: left">赠送金额: {{ scope.row.giveBalance }}</span>
                                <span style="display: block; text-align: left">福利金额: {{ scope.row.welfareBalance }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="充值时间" align="center" prop="rechargeTime" width="100">
                            <template slot-scope="scope">
                                {{ scope.row.rechargeTime == null ? '--' : scope.row.rechargeTime }}
                            </template>
                        </el-table-column>
                        <el-table-column label="状态" align="center" prop="rechargeStatus">
                            <template slot-scope="scope">
                                <dict-tag :options="dict.type.recharge_use_status" :value="scope.row.rechargeStatus" />
                            </template>
                        </el-table-column>
                    </Page>
                </el-card>
            </el-col>

            <!-- 充值类型 -->
            <el-col :span="12">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>充值信息</span>
                    </div>
                    <el-form ref="rechargeFrom" :model="rechargeFrom" :rules="rules" label-width="160px" @submit.native.prevent>
                        <el-col :span="24">
                            <el-form-item label="请选择充值类型" label-width="120px" prop="rechargeType">
                                <el-col :span="18">
                                    <el-radio-group v-model="rechargeFrom.rechargeType" style="width: 500px" @input="clearData">
                                        <el-radio-button label="3">自定义充值</el-radio-button>
                                        <el-radio-button label="0">套餐充值</el-radio-button>
                                        <el-radio-button label="1">福利金额充值</el-radio-button>
                                        <!--                                        <el-radio-button label="2">卡密充值</el-radio-button>-->
                                    </el-radio-group>
                                </el-col>
                            </el-form-item>

                            <el-form-item label="请选择充值套餐" label-width="120px" prop="rechargeMealId" v-if="rechargeFrom.rechargeType == 0">
                                <el-col :span="18">
                                    <el-select v-model="rechargeFrom.rechargeMealId" placeholder="请选择充值套餐" style="width: 300px" clearable @input="getActivityInfo(rechargeFrom.rechargeMealId)">
                                        <el-option v-for="recharge in rechargeList" :key="recharge.id" :label="recharge.title" :value="recharge.id" />
                                    </el-select>
                                </el-col>
                            </el-form-item>
                            <el-form-item label="自定义金额" label-width="120px" prop="customAmount" v-if="rechargeFrom.rechargeType == 3">
                                <el-col :span="18">
                                    <el-input-number v-model="rechargeFrom.customAmount" :controls="false" :min="0" :max="10000" style="width: 300px"></el-input-number>
                                </el-col>
                            </el-form-item>
                            <el-form-item label="福利金额（元）" label-width="120px" prop="welfareAmount" v-if="rechargeFrom.rechargeType == 1">
                                <el-col :span="18">
                                    <el-input-number v-model="rechargeFrom.welfareAmount" :controls="false" :min="0" :max="999999" style="width: 300px"></el-input-number>
                                </el-col>
                            </el-form-item>
                            <el-form-item label="卡号" label-width="120px" v-if="rechargeFrom.rechargeType == 2" required>
                                <el-col :span="18">
                                    <el-input v-model="rechargeFrom.rechargeCardNo" style="width: 300px"></el-input>
                                </el-col>
                            </el-form-item>
                            <el-form-item label="密码" label-width="120px" v-if="rechargeFrom.rechargeType == 2" required>
                                <el-col :span="18">
                                    <el-input v-model="rechargeFrom.rechargeCardPassword" style="width: 300px"></el-input>
                                </el-col>
                            </el-form-item>
                            <el-form-item label="充值备注" label-width="120px" prop="remark">
                                <el-col :span="18">
                                    <el-input type="textarea" placeholder="请输入充值备注" v-model="rechargeFrom.remark" style="width: 300px"></el-input>
                                </el-col>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-card style="width: 430px" v-if="rechargeFrom.rechargeType == 0">
                                <el-descriptions title="充值活动信息" :column="1" :label-style="{ 'text-align': 'center' }" :contentStyle="{ 'text-align': 'center' }">
                                    <el-descriptions-item label="本次充值金额" style="width: 100px" v-if="activityAmount != null && activityAmount != ''">
                                        {{ getRechargeTypeInfo == '' ? '--' : getRechargeTypeInfo.rechargeAmount }}
                                    </el-descriptions-item>
                                    <el-descriptions-item label="原赠送金额" v-if="activityAmount != null && activityAmount != ''">
                                        {{ getRechargeTypeInfo == '' ? '--' : getRechargeTypeInfo.giveAmount }}
                                    </el-descriptions-item>
                                    <el-descriptions-item label="活动赠送金额" v-if="activityAmount != null && activityAmount != ''">
                                        {{ activityAmount }}
                                    </el-descriptions-item>
                                    <el-descriptions-item label="参与活动" v-if="activityAmount == null || activityAmount == ''"> 此套餐暂未参与活动 </el-descriptions-item>
                                </el-descriptions>
                            </el-card>
                        </el-col>
                        <!--            <el-col :span="24">-->
                        <!--              <el-card class="box-card" shadow="hover" style="width: 150px" @click.native="clickRecharge">-->
                        <!--                <div slot="header" class="clearfix">-->
                        <!--                  <span></span>-->
                        <!--                  <span style="float: right">赠送500元</span>-->
                        <!--                </div>-->
                        <!--                <div class="text item">-->
                        <!--                  <span>{{'充值金额 ' + "1" }}</span>-->
                        <!--                  <span>{{'充值金额 ' + "1" }}</span>-->
                        <!--                </div>-->
                        <!--              </el-card>-->
                        <!--            </el-col>-->
                    </el-form>
                    <div>
                        <el-button type="primary" @click="submitCash" style="margin-left: 3%; margin-top: 20px">收银 </el-button>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-dialog title="确认充值" :visible.sync="openWelfareRecharge" width="400px" append-to-body>
            <el-form ref="payFrom" :model="rechargeInfo" label-width="100px" style="margin-top: 20px" @submit.native.prevent>
                <el-form-item label="充值金额：">
                    <span style="font-size: 16px; font-weight: bold">￥{{ rechargeInfo.welfareAmount }}</span>
                </el-form-item>
                <el-form-item label="备注：">
                    <span style="font-size: 16px; font-weight: bold">{{ rechargeInfo.remark }}</span>
                </el-form-item>
            </el-form>
            <el-button type="primary" @click="submitSettlement" style="margin: 20px 80px 0; width: 200px; height: 40px">确认</el-button>
        </el-dialog>
        <!-- 支付方式 -->
        <el-dialog :title="rechargeTitle" :visible.sync="openRecharge" width="800px" :close-on-click-modal="false" class="cashier-dialog" :before-close="checkClosePay" append-to-body>
            <!-- 订单信息 -->
            <div class="order-info">
                <div class="amount-section">
                    <div class="total-amount">
                        <span class="label">应付金额</span>
                        <span class="value">¥{{ getOrderNeedAmount }}</span>
                    </div>
                    <div class="detail-list">
                        <div class="detail-item">
                            <span class="label">充值金额：</span>
                            <span class="value">¥{{ rechargeInfo.rechargeAmount }}</span>
                        </div>
                        <div class="detail-item">
                            <span class="label">赠送金额：</span>
                            <span class="value">¥{{ activityAmount || rechargeInfo.giveAmount }}</span>
                        </div>
                        <div class="detail-item">
                            <span class="label">赠送积分：</span>
                            <span class="value">{{ rechargeInfo.givePointAmount }}</span>
                        </div>
                        <div class="detail-item">
                            <span class="label">福利金额：</span>
                            <span class="value">¥{{ rechargeInfo.welfareAmount }}</span>
                        </div>
                    </div>
                </div>

                <!-- 支付方式选择 -->
                <div class="payment-section">
                    <div class="section-title">支付方式</div>
                    <div class="payment-methods">
                        <div v-for="(method, index) in paymentMethods" :key="index" :class="['payment-method', { 'is-active': payFrom.payType === method.value }]" @click="selectPaymentMethod(method.value)">
                            <i :class="method.icon"></i>
                            <span>{{ method.label }}</span>
                        </div>
                    </div>

                    <!-- 支付金额输入区域 -->
                    <div class="payment-input" v-if="payFrom.payType === '0'">
                        <div class="input-group">
                            <div class="input-item">
                                <span class="label">微信/支付宝</span>
                                <div class="readonly-value">¥{{ getOrderNeedAmount }}</div>
                            </div>
                        </div>
                        <!-- 增加条形码扫描框 -->
                        <div class="scan-input">
                            <el-input v-model="payFrom.wxBarcode" placeholder="请扫描付款码" prefix-icon="el-icon-camera" clearable autofocus> </el-input>
                        </div>
                    </div>

                    <!-- 支付金额输入区域 -->
                    <div class="payment-input" v-if="payFrom.payType === '2'">
                        <div class="input-group">
                            <div class="input-item">
                                <span class="label">现金支付</span>
                                <div class="readonly-value">¥{{ getOrderNeedAmount }}</div>
                            </div>
                        </div>
                    </div>

                    <!-- 组合支付时显示输入框 -->
                    <div v-if="payFrom.payType === '9'">
                        <div class="payment-input">
                            <div class="input-group">
                                <div class="input-item">
                                    <span class="label">现金支付</span>
                                    <el-input v-model="payFrom.cashPayAmount" placeholder="请输入现金金额" @input="changeCashPayAmount">
                                        <template slot="prepend">¥</template>
                                    </el-input>
                                </div>
                                <div class="input-item">
                                    <span class="label">电子支付</span>
                                    <div class="readonly-value">¥{{ payFrom.otherPayAmount || 0 }}</div>
                                </div>
                            </div>

                            <!-- 增加条形码扫描框 -->
                            <div class="scan-input">
                                <el-input v-model="payFrom.wxBarcode" placeholder="请扫描付款码" prefix-icon="el-icon-camera" clearable autofocus> </el-input>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 底部操作区 -->
            <div slot="footer" class="dialog-footer">
                <div class="payment-summary">
                    <span class="label">应收</span>
                    <span class="amount">¥{{ getOrderNeedAmount }}</span>
                </div>
                <el-button type="primary" size="large" :loading="isSubmitting" @click="submitSettlement"> 确认收款 </el-button>
            </div>
        </el-dialog>

        <el-dialog title="选择优惠券" :visible.sync="openCoupon" width="60%">
            <template>
                <el-table :data="couponList" style="width: 100%" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" align="center" fixed="left" />
                    <el-table-column label="优惠券" align="center" prop="couponTypeTitle" />
                    <el-table-column label="优惠内容" align="center">
                        <template slot-scope="scope">
                            <span>抵扣充值金额：{{ scope.row.deductRechargeAmount }}元</span>
                        </template>
                    </el-table-column>
                </el-table>
                <el-row>
                    <div style="margin-top: 30px">
                        <el-button @click="submitAlreadySelectCouponList" size="small" type="primary" style="float: right"> 确定 </el-button>
                    </div>
                </el-row>
            </template>
        </el-dialog>

        <!-- 添加用户对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="30%" append-to-body :before-close="checkCloseUserPage">
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                    <el-col :span="24">
                        <el-form-item label="昵称" prop="nickName">
                            <el-input v-model="form.nickName" placeholder="请输入昵称" maxlength="50" show-word-limit clearable />
                        </el-form-item>
                        <el-form-item label="真实姓名" prop="realName">
                            <el-input v-model="form.realName" placeholder="请输入真实姓名" maxlength="5" show-word-limit clearable />
                        </el-form-item>
                        <el-form-item label="手机号" prop="phoneNumber">
                            <el-input v-model="form.phoneNumber" placeholder="请输入手机号" maxlength="11" show-word-limit clearable />
                        </el-form-item>
                        <el-form-item label="性别" prop="sex">
                            <el-select v-model="form.sex" placeholder="请选择性别" clearable>
                                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="生日" prop="birthday" clearable>
                            <el-date-picker clearable v-model="form.birthday" type="date" value-format="yyyy-MM-dd" placeholder="请选择生日"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="头像地址" prop="avatarUrl">
                            <image-upload v-model="form.avatarUrl" :limit="1" />
                        </el-form-item>
                        <el-form-item label="客户等级" prop="level">
                            <el-input-number v-model="form.level" :min="1" :max="50" label="请输入客户等级"></el-input-number>
                        </el-form-item>
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入备注" maxlength="50" show-word-limit />
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <pay-status-popup ref="payStatusPopup" hidePointer hideUser @paySuccess="checkIsAllPay"></pay-status-popup>
    </div>
</template>

<script>
import { listType } from '@/api/recharge/type'
import { addUser, findUserByPhoneNumber, getMemberCouponInfoByRecharge, updateUser } from '@/api/app/user'
import { addOrder, getOrderIsAllPayByOrderNo, getOrderStatusByOrderNo, morePay } from '@/api/recharge/order'
import { getRechargeTypeRelationActivityInfo } from '@/api/activity/rechargeItem'
import payStatusPopup from '@/components/payStatusPopup'

export default {
    name: 'workRecharge',
    dicts: ['recharge_use_status', 'sys_user_sex'],
    components: { payStatusPopup },
    data() {
        return {
            alreadyPayTotal: 0,
            orderNeedPayAmount: 0,
            couponAmount: 0,
            alreadyPayInfo: {},
            payConfirm: false,
            payFrom: {},
            userInfoVO: null,
            orderNo: null,
            // 优惠卷
            // couponInfo:null,
            couponList: [],
            // 客户信息
            appUserInfo: null,
            rechargeList: null,
            rechargeInfo: {},
            // 充值类型参数
            rechargeFrom: {
                rechargeType: '3',
                // 充值套餐id
                rechargeMealId: null,
                // 自定义金额
                customAmount: null,
                remark: '',
                // 福利金额
                welfareAmount: null,
                // 卡号
                rechargeCardNo: null,
                // 密码
                rechargeCardPassword: null
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 弹出层标题
            title: '',
            rechargeTitle: '',
            // 是否显示弹出层
            open: false,
            openRecharge: false,
            openWelfareRecharge: false,
            openCoupon: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },
            cardParams: {
                rechargeUserId: 0
            },
            couponParams: {
                pageNum: 1,
                pageSize: 999,
                appUserId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                rechargeCardNo: [
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        required: true,
                        message: '请输入正整数类型',
                        trigger: 'blur'
                    }
                ],
                rechargeCardPassword: [
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        required: true,
                        message: '请输入正整数类型',
                        trigger: 'blur'
                    }
                ],
                nickName: [
                    {
                        required: true,
                        message: '昵称不能为空',
                        trigger: 'blur'
                    }
                ],
                realName: [
                    {
                        required: true,
                        message: '真实姓名不能为空',
                        trigger: 'blur'
                    }
                ],
                phoneNumber: [
                    {
                        required: true,
                        message: '手机号不能为空',
                        trigger: 'blur'
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        required: true,
                        message: '请输入数量，且为正整数类型',
                        trigger: 'blur'
                    },
                    { min: 11, max: 11, message: '手机号至少11位', trigger: 'blur' }
                ],
                sex: [
                    {
                        required: true,
                        message: '性别不能为空',
                        trigger: 'blur'
                    }
                ],
                birthday: [
                    {
                        required: true,
                        message: '生日不能为空',
                        trigger: 'blur'
                    }
                ]
            },
            authCode: null,
            activityAmount: null,
            alreadySelectCouponList: [],
            //支付检测
            isPaying: false,
            isSubmitting: false,
            paymentMethods: [
                { label: '现金支付', value: '2', icon: 'el-icon-money' },
                { label: '微信/支付宝', value: '0', icon: 'el-icon-mobile-phone' },
                { label: '组合支付', value: '9', icon: 'el-icon-wallet' }
            ],
            quickAmounts: ['100', '200', '500', '1000']
        }
    },
    created() {
        this.handleUser()
        this.getRechargeList()
    },
    computed: {
        getRechargeTypeInfo() {
            if (this.rechargeFrom.rechargeMealId != null) {
                return this.rechargeList.find((item) => item.id == this.rechargeFrom.rechargeMealId)
            } else {
                return ''
            }
        },
        getCouponAmount() {
            this.couponAmount = 0
            if (this.alreadySelectCouponList.length == 0) {
                return this.couponAmount
            }
            if (this.alreadySelectCouponList.length > 0) {
                this.alreadySelectCouponList.forEach((coupon) => {
                    this.couponAmount = this.couponAmount + parseFloat(coupon.deductRechargeAmount)
                })
            }
            return this.couponAmount
        },
        getOrderNeedAmount() {
            this.orderNeedPayAmount = parseFloat(this.rechargeInfo.rechargeAmount) - parseFloat(this.couponAmount) > 0 ? parseFloat(this.rechargeInfo.rechargeAmount) - parseFloat(this.couponAmount) : 0
            return this.orderNeedPayAmount
        }
    },
    methods: {
        changeCashPayAmount(value) {
            this.payFrom.cashPayAmount = value.replace(/[^\d]/g, '')
            this.payFrom.otherPayAmount = Math.round((Number(this.orderNeedPayAmount) - Number(this.payFrom.cashPayAmount)) * 100) / 100
        },
        clickRecharge(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        getActivityInfo(rechargeTypeId) {
            if (rechargeTypeId != null && rechargeTypeId != '') {
                getRechargeTypeRelationActivityInfo(rechargeTypeId, this.userInfoVO.storeId).then((response) => {
                    this.activityAmount = response.data
                })
            } else {
                this.activityAmount = null
            }
        },
        clearData() {
            this.rechargeFrom.rechargeMealId = null
            this.rechargeFrom.customAmount = null
            this.rechargeFrom.welfareAmount = null
            this.rechargeFrom.rechargeCardNo = null
            this.rechargeFrom.rechargeCardPassword = null
            this.activityAmount = null
        },
        openCouponList() {
            this.getCouponList()
            this.openCoupon = true
        },
        // 获取优惠券集合
        getCouponList() {
            if (this.appUserInfo == null) {
                this.couponList = []
                return
            }
            this.couponParams.appUserId = this.appUserInfo.id
            getMemberCouponInfoByRecharge(this.couponParams).then((response) => {
                this.couponList = response.data.records
            })
        },
        // 获取登录人信息
        handleUser() {
            this.userInfoVO = this.getUserInfo()
        },
        //搜索用户
        searchUser() {
            this.$refs['queryForm'].validate((valid) => {
                if (valid) {
                    findUserByPhoneNumber(this.queryParams).then((res) => {
                        this.appUserInfo = res.data
                        if (this.appUserInfo) {
                            this.cardParams.rechargeUserId = this.appUserInfo.id
                            this.$nextTick(() => {
                                this.$refs.pagination.handleSearch(true)
                            })
                        }
                    })
                }
            })
        },
        //条码搜索用户
        qrcodeSearchUser() {},
        // 获取 充值套餐集合
        getRechargeList() {
            listType().then((response) => {
                this.rechargeList = response.rows
            })
        },
        // 关闭用户新建页面 二次确认
        checkCloseUserPage(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        // 关闭支付页面 二次确认
        checkClosePay(done) {
            this.$confirm('是否确定取消支付?')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        // 创建用户
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateUser(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                        })
                    } else {
                        addUser(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                        })
                    }
                }
            })
        },
        // 收银
        submitCash() {
            if (this.appUserInfo == null) {
                this.$message.error('请选择客户')
                return
            }
            // 重置
            this.resetPayInfo()
            this.rechargeInfo.remark = this.rechargeFrom.remark
            // 自定义充值
            if (this.rechargeFrom.rechargeType == '3') {
                if (this.rechargeFrom.customAmount == '0') {
                    this.$message.error('充值金额不能为0元')
                    return
                }
                this.rechargeInfo.rechargeAmount = this.rechargeFrom.customAmount
                this.rechargeInfo.giveAmount = 0
                this.rechargeInfo.givePointAmount = 0
                this.rechargeInfo.welfareAmount = 0
                this.rechargeInfo.rechargeType = this.rechargeFrom.rechargeType
                this.rechargeInfo.rechargeTypeId = null
            }
            // 套餐充值
            if (!this.rechargeFrom.rechargeType || this.rechargeFrom.rechargeType == '0') {
                if (this.rechargeFrom.rechargeMealId == null) {
                    this.$message.error('充值套餐不能为空')
                    return
                }
                let recharge
                recharge = this.rechargeList.find((item) => item.id == this.rechargeFrom.rechargeMealId)
                this.rechargeInfo.rechargeAmount = recharge.rechargeAmount
                this.rechargeInfo.giveAmount = recharge.giveAmount
                this.rechargeInfo.givePointAmount = recharge.givePointAmount
                this.rechargeInfo.welfareAmount = 0
                this.rechargeInfo.rechargeType = this.rechargeFrom.rechargeType
                this.rechargeInfo.rechargeTypeId = recharge.id
            }
            // 福利金额充值
            if (this.rechargeFrom.rechargeType == '1') {
                if (!this.rechargeFrom.rechargeType || this.rechargeFrom.welfareAmount == '0') {
                    this.$message.error('充值金额不能为0元')
                    return
                }
                if (!this.rechargeFrom.remark) {
                    this.$message.error('请输入备注')
                    return
                }

                this.rechargeInfo.rechargeAmount = 0
                this.rechargeInfo.giveAmount = 0
                this.rechargeInfo.givePointAmount = 0
                this.rechargeInfo.welfareAmount = this.rechargeFrom.welfareAmount
                this.rechargeInfo.rechargeType = this.rechargeFrom.rechargeType
                this.rechargeInfo.rechargeTypeId = null
                this.rechargeInfo.remark = this.rechargeFrom.remark
                this.openWelfareRecharge = true
                this.orderNo = null
                return
            }
            // 卡密充值
            if (this.rechargeFrom.rechargeType == '2') {
                if (this.rechargeFrom.rechargeCardNo == null || this.rechargeFrom.rechargeCardPassword == null) {
                    this.$message.error('卡号/密码，不能为空')
                    return
                }
                const orderRechargePayDTO = {
                    appUserId: this.appUserInfo.id,
                    rechargeType: this.rechargeFrom.rechargeType,
                    cardNo: this.rechargeFrom.rechargeCardNo,
                    password: this.rechargeFrom.rechargeCardPassword,
                    remark: this.rechargeFrom.remark
                }
                addOrder(orderRechargePayDTO).then((response) => {
                    if (response.data.isAllPayEnd == true) {
                        this.$modal.msgSuccess('充值成功')
                        this.searchUser()
                        this.clearData()
                    }
                })
                return
            }

            this.rechargeTitle = '支付方式选择'
            this.openRecharge = true
            this.orderNo = null
        },
        // 结算
        submitSettlement() {
            const orderRechargePayDTO = {
                orderNo: this.orderNo,
                appUserId: this.appUserInfo.id,
                payType: this.payFrom.payType,
                isInstalment: this.payFrom.isInstalment,
                couponIds: [],
                remark: this.rechargeInfo.remark,
                rechargeAmount: this.rechargeInfo.rechargeAmount,
                giveAmount: this.rechargeInfo.giveAmount,
                givePointAmount: this.rechargeInfo.givePointAmount,
                rechargeTypeId: this.rechargeInfo.rechargeTypeId,
                welfareAmount: this.rechargeInfo.welfareAmount,
                rechargeType: this.rechargeInfo.rechargeType,
                payAmount: this.orderNeedPayAmount
            }
            if (this.alreadySelectCouponList.length > 0) {
                this.alreadySelectCouponList.forEach((vo) => {
                    orderRechargePayDTO.couponIds.push(vo.id)
                })
            } else {
                orderRechargePayDTO.couponIds = []
            }
            if (this.payFrom.isInstalment == 'N') {
                orderRechargePayDTO.thisPayAmount = this.orderNeedPayAmount
            }
            if (this.payFrom.isInstalment == 'Y') {
                orderRechargePayDTO.thisPayAmount = this.payFrom.thisPayMoney
            }
            // 现金支付
            if (this.payFrom.payType == 2) {
                orderRechargePayDTO.thisPayAmount = this.orderNeedPayAmount
            }
            // 微信支付
            if (this.payFrom.payType == 0) {
                orderRechargePayDTO.authCode = this.payFrom.wxBarcode
            }
            // 支付宝支付
            if (this.payFrom.payType == 1) {
                orderRechargePayDTO.authCode = this.payFrom.aliPayBarcode
            }

            if (this.payFrom.payType == 9) {
                if (!this.payFrom.cashPayAmount) {
                    this.$message.error('请输入现金支付金额')
                    return
                }
                if (Number(this.payFrom.cashPayAmount) >= Number(this.orderNeedPayAmount)) {
                    this.$message.error('现金支付金额必须小于还需支付金额')
                    return
                }
                orderRechargePayDTO.cashPayAmount = this.payFrom.cashPayAmount || 0
                orderRechargePayDTO.otherPayAmount = this.payFrom.otherPayAmount || 0
                orderRechargePayDTO.authCode = this.payFrom.wxBarcode
            }

            this.$confirm('确定进行支付结算吗？')
                .then(() => {
                    const loading = this.$loading({
                        lock: true,
                        text: 'Loading',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    })
                    addOrder(orderRechargePayDTO).then(
                        (response) => {
                            loading.close()
                            this.orderNo = response.data.orderNo
                            if (response.data.payResult == 100) {
                                this.$refs.payStatusPopup.open(5, response.data)
                                var intervalId = setInterval(() => {
                                    this.intervalCheckOrderStatus(intervalId, this.orderNo)
                                }, 4000)
                            }
                            if (response.data.payResult == 200) {
                                this.$refs.payStatusPopup.open(1, response.data)
                                this.checkIsAllPay(this.orderNo)
                            }
                            if (response.data.payResult == 101 || response.data.payResult == 0) {
                                this.$refs.payStatusPopup.open(4, response.data)
                            }
                        },
                        (err) => {
                            loading.close()
                        }
                    )
                })
                .catch(() => {})
        },
        intervalCheckOrderStatus(intervalId, orderNo) {
            getOrderStatusByOrderNo({ orderNo: orderNo })
                .then((response) => {
                    if (response.data.payResult == '100') {
                        return
                    }
                    if (response.data.payResult == 101 || response.data.payResult == 0) {
                        this.$refs.payStatusPopup.open(4, response.data)
                        clearInterval(intervalId)
                        return
                    }
                    this.$refs.payStatusPopup.open(1, response.data)
                    this.checkIsAllPay(this.orderNo)
                    clearInterval(intervalId)
                })
                .catch((e) => {
                    this.$refs.payStatusPopup.open(4, e.data)
                })
        },
        checkIsAllPay(orderNo) {
            this.openRecharge = false
            this.openWelfareRecharge = false
            this.searchUser()
        },
        // 保存临时的  支付金额 用于取消支付提示信息
        saveAlreadyPayAmount() {
            // 微信
            if (this.payFrom.payType == '0') {
                this.alreadyPayInfo.alreadyPayWX = this.alreadyPayInfo.alreadyPayWX + parseFloat(this.payFrom.thisPayMoney)
            }
            // 支付宝
            if (this.payFrom.payType == '1') {
                this.alreadyPayInfo.alreadyPayAli = this.alreadyPayInfo.alreadyPayAli + parseFloat(this.payFrom.thisPayMoney)
            }
            // 现金
            if (this.payFrom.payType == '2') {
                this.alreadyPayInfo.alreadyPayCash = this.alreadyPayInfo.alreadyPayCash + parseFloat(this.payFrom.thisPayMoney)
            }
        },
        // 支付页面 确认按钮
        submitConfirm() {
            this.payConfirm = true
        },
        // 切换支付方式
        changePayType() {
            this.payConfirm = false
            this.payFrom.thisPayMoney = null
            this.payFrom.aliPayBarcode = null
            this.payFrom.wxBarcode = null
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                phoneNumber: null,
                nickName: null,
                realName: null,
                sex: null,
                birthday: null,
                avatarUrl: null,
                unionId: null,
                createTime: null,
                payPassword: null,
                isUsePayPassword: null,
                status: '0',
                level: null
            }
            this.resetForm('form')
        },
        resetPayInfo() {
            this.alreadyPayTotal = 0
            this.couponAmount = 0
            this.payConfirm = false
            this.rechargeInfo = {}
            this.alreadySelectCouponList = []
            this.payFrom = {
                payType: '2',
                isInstalment: 'N',
                wxBarcode: null,
                aliPayBarcode: null,
                thisPayMoney: 0,
                isUsedCoupon: false
            }
            this.alreadyPayInfo = {
                alreadyPayWX: 0,
                alreadyPayCash: 0,
                alreadyPayAli: 0
            }
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加用户'
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        submitAlreadySelectCouponList() {
            this.alreadySelectCouponList = []
            this.ids.forEach((id) => {
                const vo = this.couponList.find((item) => item.id == id)
                this.alreadySelectCouponList.push(vo)
            })
            this.openCoupon = false
        },
        handleDelete(row) {
            this.alreadySelectCouponList = this.alreadySelectCouponList.filter((item) => item.id != row.id)
            this.$forceUpdate()
        },
        // 选择支付方式
        selectPaymentMethod(type) {
            this.payFrom.payType = type
            this.payFrom.wxBarcode = ''

            // 现金支付时，直接设置支付金额为应付金额
            if (type === '2') {
                this.payFrom.cashPayAmount = this.getOrderNeedAmount
                this.payFrom.otherPayAmount = 0
            } else {
                this.payFrom.cashPayAmount = ''
                this.payFrom.otherPayAmount = ''
            }
        },

        // 选择快捷金额
        selectQuickAmount(amount) {
            // 确保快捷金额不超过应付金额
            if (Number(amount) > Number(this.getOrderNeedAmount)) {
                this.payFrom.cashPayAmount = this.getOrderNeedAmount
            } else {
                this.payFrom.cashPayAmount = amount
            }
            // 计算电子支付金额
            this.payFrom.otherPayAmount = Math.round((Number(this.getOrderNeedAmount) - Number(this.payFrom.cashPayAmount)) * 100) / 100
        }
    }
}
</script>
<style lang="scss" scoped>
.cashier-dialog {
    :deep(.el-dialog__body) {
        padding: 0;
    }

    .order-info {
        background: #f8f9fb;
        padding: 24px;

        .amount-section {
            text-align: center;
            margin-bottom: 24px;
            padding-bottom: 24px;
            border-bottom: 1px solid #ebeef5;

            .total-amount {
                margin-bottom: 16px;

                .label {
                    font-size: 16px;
                    color: #606266;
                    margin-right: 8px;
                }

                .value {
                    font-size: 32px;
                    font-weight: bold;
                    color: #f56c6c;
                }
            }

            .detail-list {
                display: flex;
                justify-content: center;
                flex-wrap: wrap;
                gap: 24px;

                .detail-item {
                    .label {
                        color: #909399;
                    }

                    .value {
                        color: #606266;
                        font-weight: 500;
                    }
                }
            }
        }

        .payment-section {
            .section-title {
                font-size: 16px;
                font-weight: 500;
                margin-bottom: 16px;
            }

            .payment-methods {
                display: grid;
                grid-template-columns: repeat(4, 1fr);
                gap: 16px;
                margin-bottom: 24px;

                .payment-method {
                    height: 80px;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    gap: 8px;
                    border: 1px solid #dcdfe6;
                    border-radius: 4px;
                    cursor: pointer;
                    transition: all 0.3s;

                    i {
                        font-size: 24px;
                        color: #909399;
                    }

                    &:hover {
                        border-color: #409eff;
                    }

                    &.is-active {
                        border-color: #409eff;
                        background: #ecf5ff;

                        i,
                        span {
                            color: #409eff;
                        }
                    }
                }
            }

            .payment-input {
                margin-bottom: 24px;

                .input-group {
                    display: grid;
                    grid-template-columns: repeat(2, 1fr);
                    gap: 16px;

                    .input-item {
                        .label {
                            display: block;
                            margin-bottom: 8px;
                            color: #606266;
                        }

                        .readonly-value {
                            height: 40px;
                            line-height: 40px;
                            padding: 0 16px;
                            background: #f5f7fa;
                            border: 1px solid #dcdfe6;
                            border-radius: 4px;
                            color: #606266;
                            font-weight: bold;
                        }
                    }
                }
            }

            .scan-input {
                margin-top: 16px;
            }
        }
    }

    .quick-amount {
        padding: 24px 0;

        .section-title {
            font-size: 14px;
            color: #606266;
            margin-bottom: 16px;
        }

        .amount-buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 16px;

            .amount-btn {
                height: 40px;

                &.is-active {
                    color: #409eff;
                    border-color: #409eff;
                    background: #ecf5ff;
                }
            }
        }
    }

    .dialog-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 16px 24px;
        background: #f8f9fb;
        border-top: 1px solid #ebeef5;

        .payment-summary {
            .label {
                font-size: 16px;
                color: #606266;
                margin-right: 8px;
            }

            .amount {
                font-size: 24px;
                font-weight: bold;
                color: #f56c6c;
            }
        }

        .el-button {
            width: 160px;
            height: 48px;
        }
    }
}
</style>
