<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="orderNo" v-if="userInfoVO.userType == '00'">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="phoneNumber">
                <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="realName">
                <el-input v-model="queryParams.realName" placeholder="请输入客户姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label-width="120px" prop="orgId" v-if="userInfoVO.userType == '00'">
                <el-select v-model="queryParams.orgId" placeholder="请选择" clearable>
                    <el-option-group label="门店">
                        <template>
                            <el-option v-for="store in storeList" :key="store.id + store.sourceType" :label="store.name" :value="store.id"> </el-option>
                        </template>
                    </el-option-group>
                </el-select>
            </el-form-item>
            <el-form-item 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 prop="queryRefundTime">
                <el-date-picker clearable v-model="queryParams.queryRefundTime" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
            </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']" v-if="userInfoVO.userType == '00'">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/recharge/refund/refundRechargeOrderList" :request-params="queryParams" ref="pagination">
            <el-table-column label="订单编号" align="center" width="190" prop="orderNo" />
            <el-table-column label="客户姓名" align="center" prop="realName" />
            <el-table-column label="客户手机号" align="center" prop="phoneNumber" width="110" />
            <el-table-column label="总金额（元）" align="center" width="100">
                <template slot-scope="scope">
                    {{ scope.row.rechargeAmount + scope.row.giveAmount + scope.row.welfareAmount }}
                </template>
            </el-table-column>
            <el-table-column label="充值金额（元）" align="center" prop="rechargeAmount" width="120" />
            <el-table-column label="赠送金额（元）" align="center" prop="giveAmount" width="120" />
            <el-table-column label="福利金额（元）" align="center" prop="welfareAmount" width="120" />
            <el-table-column label="赠送积分" align="center" prop="givePointAmount" v-if="userInfoVO.userType == '02'" />
            <el-table-column label="退款金额（元）" align="center" prop="refundAmount" width="120" />
            <el-table-column label="退款描述" align="center" prop="refundDescribe" width="200" show-overflow-tooltip />
            <el-table-column label="退款时间" align="center" prop="refundTime" width="180" />
            <el-table-column label="支付方式" align="center" prop="payType">
                <template slot-scope="scope">
                    <div v-if="scope.row.payType != ''">
                        <dict-tag :options="dict.type.recharge_pay_type" :value="scope.row.payType" />
                    </div>
                    <div v-if="scope.row.payType == null">
                        <span>分段支付</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="订单状态" align="center" prop="orderStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.recharge_order_status" :value="scope.row.orderStatus" />
                </template>
            </el-table-column>
            <el-table-column label="退款状态" align="center" prop="refundStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.goods_order_refund_status" :value="scope.row.refundStatus" />
                </template>
            </el-table-column>
            <el-table-column label="交易时间" align="center" prop="payTime" width="180" />
            <el-table-column label="下单时间" align="center" prop="createTime" width="180" />
            <el-table-column label="门店名称" align="center" v-if="userInfoVO.userType == '00'">
                <template slot-scope="scope">
                    <span v-if="scope.row.sourceType === '01'">
                        {{ scope.row.factoryName }}
                    </span>
                    <span v-if="scope.row.sourceType === '02'">
                        {{ scope.row.storeName }}
                    </span>
                    <span v-if="scope.row.sourceType === '00'"> </span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
                <template slot-scope="scope">
                    <el-button type="text" @click="handleDetail(scope.row)">支付明细</el-button>
                </template>
            </el-table-column>
        </Page>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改活动订单对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="40%">
            <template v-if="payDetailList != null">
                <el-timeline v-for="detail in payDetailList" :key="detail.id">
                    <el-timeline-item :timestamp="'支付时间：' + detail.createTime" placement="top">
                        <el-card style="width: 90%">
                            <el-descriptions :column="3">
                                <el-descriptions-item label="支付方式">
                                    <dict-tag :options="dict.type.recharge_pay_type" :value="detail.payType" />
                                </el-descriptions-item>
                                <el-descriptions-item label="支付状态">
                                    <dict-tag :options="dict.type.recharge_order_status" :value="detail.orderStatus" />
                                </el-descriptions-item>
                                <el-descriptions-item label="支付金额">{{ detail.payAmount }} 元</el-descriptions-item>
                            </el-descriptions>
                        </el-card>
                    </el-timeline-item>
                </el-timeline>
            </template>
        </el-dialog>
    </div>
</template>

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

export default {
    name: 'OrderRechargeRecord',
    dicts: ['recharge_pay_type', 'recharge_order_status', 'goods_order_refund_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 充值订单表格数据
            orderList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                orderNo: null,
                sourceType: null,
                orgId: null,
                storeName: null,
                phoneNumber: null,
                realName: null,
                createTimeFrom: null,
                createTimeTo: null,
                queryCreateTime: null,
                beginRefundTime: null,
                endRefundTime: null,
                queryRefundTime: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            queryCreateTime: null,
            userInfoVO: null,
            storeList: [],
            payDetailList: []
        }
    },
    created() {
        allOrg().then((res) => {
            this.orgList = res.data
            this.orgList.forEach((org) => {
                if (org.sourceType == '02') {
                    this.storeList.push(org)
                }
            })
        })
        this.handleUser()
        this.getList()
    },
    methods: {
        getCreateTime() {},
        handleUser() {
            this.userInfoVO = this.getUserInfo()
        },
        /** 查询充值订单列表 */
        getList() {
            const param = { ...this.queryParams }
            if (this.queryParams.queryCreateTime != null) {
                param.createTimeFrom = this.queryParams.queryCreateTime[0]
                param.createTimeTo = this.queryParams.queryCreateTime[1]
                param.queryCreateTime = null
            }
            if (this.queryParams.queryRefundTime != null) {
                param.beginRefundTime = this.queryParams.queryRefundTime[0]
                param.endRefundTime = this.queryParams.queryRefundTime[1]
                param.queryRefundTime = 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
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加充值订单'
        },
        /** 支付明细详情 */
        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() {
            const param = { ...this.queryParams }
            if (this.queryParams.queryCreateTime != null) {
                param.createTimeFrom = this.queryParams.queryCreateTime[0]
                param.createTimeTo = this.queryParams.queryCreateTime[1]
                param.queryCreateTime = null
            }
            if (this.queryParams.queryRefundTime != null) {
                param.beginRefundTime = this.queryParams.queryRefundTime[0]
                param.endRefundTime = this.queryParams.queryRefundTime[1]
                param.queryRefundTime = null
            }
            this.download(
                'mapi/recharge/refund/exportByRefund',
                {
                    param
                },
                `order_rechargeRefund_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
