<!-- 总部财务&管理员 -->
<!-- 客服 -->
<template>
    <el-card style="margin-bottom: 0.5vh;">
        <div class="condition">
            <div class="item1">
                <el-text size="large" style="width:4vw;">精网号</el-text>
                <el-input v-model="searchForm.jwcode" placeholder="请输入精网号" style="width:9vw;" clearable />
            </div>
            <div class="item1">
                <el-text size="large" style="width:4vw;">客户姓名</el-text>
                <el-input v-model="searchForm.name" placeholder="请输入客户姓名" style="width:9vw;" clearable />
            </div>
            <div class="item1">
                <el-text size="large" style="width:4vw;">所属地区</el-text>
                <el-input v-model="searchForm.market" placeholder="请输入所属地区" style="width:9vw;" clearable />
            </div>
            <div class="item1">
                <el-text size="large" style="width:4vw;">产品名称</el-text>
                <el-input v-model="searchForm.goodsName" placeholder="请输入产品名称" style="width:9vw;" clearable />
            </div>
            <div class="item1">
                <el-text size="large" style="width:4vw;">订单状态</el-text>
                <el-select v-model="searchForm.status" style="width:9vw;">
                    <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </div>
        </div>
        <div class="condition">
            <div class="item2">
                <el-text size="large" style="width:4vw;">付款币种</el-text>
                <el-input v-model="searchForm.payCurrency" placeholder="请输入付款币种" style="width:9vw;" clearable />
            </div>
            <div class="item2">
                <el-text size="large" style="width:4vw;">支付方式</el-text>
                <el-input v-model="searchForm.payType" placeholder="请输入支付方式" style="width:9vw;" clearable />
            </div>
            <div class="item2" style="width: 28.5vw;">
                <el-text size="large" style="width:4vw;">付款时间</el-text>
                <el-date-picker v-model="dateRange" type="datetimerange" range-separator="至" start-placeholder="起始时间"
                    end-placeholder="结束时间" style="width:22vw;" @change="handleDatePickerChange"
                    :disabled-date="disabledDate" />
            </div>
            <div>
                <el-button type="success">重置</el-button>
                <el-button type="primary">查询</el-button>
                <el-button type="warning">导出excel</el-button>
                <el-button type="primary">查看导出列表</el-button>
            </div>
        </div>
    </el-card>

    <div style="display: flex;">
        <el-button type="primary" @click="showAdd = true">地区客服编辑</el-button>
        <el-button type="primary" @click="showAudit1 = true">审核1</el-button>
        <el-button type="primary" @click="showAudit2 = true">审核2</el-button>
        <el-button type="primary" @click="showSteps = true">看看步骤条</el-button>
        <el-button type="primary" @click="showBack = true">看看撤回</el-button>
        <el-button type="primary" @click="showError = true">退款金额有误</el-button>
    </div>

    <el-card style="margin-top: 0.5vh;">
        <el-table :data="tableData" style="height:64vh;width:82vw">
            <el-table-column type="index" label="序号" width="60" fixed="left" />
            <el-table-column prop="name" label="Homily ID" width="120" fixed="left" />
            <el-table-column prop="jwcode" label="姓名" width="120" fixed="left" show-overflow-tooltip />
            <el-table-column prop="market" label="所属地区" width="120" />
            <el-table-column prop="orderCode" label="活动名称" width="120px" v-if="isKF" show-overflow-tooltip />
            <el-table-column prop="refundType" label="产品名称" width="120" />
            <el-table-column prop="refundType" label="产品数量" width="120" />
            <el-table-column prop="refundType" label="退款方式" width="120" v-if="!isKF" />
            <el-table-column prop="refundType" label="提交人" width="120" v-if="!isKF" />
            <el-table-column prop="refundType" label="退款理由" width="120" v-if="!isKF" />
            <el-table-column prop="refundType" label="付款币种" width="120" v-if="isKF" />
            <el-table-column prop="refundType" label="付款金额" width="120" v-if="isKF" />
            <el-table-column prop="refundType" label="支付方式" width="120" v-if="isKF" />
            <el-table-column prop="refundType" label="付款时间" width="120" v-if="isKF" />
            <el-table-column prop="refundType" label="转账凭证" width="120" v-if="isKF" />
            <el-table-column prop="remark" label="备注" width="150" show-overflow-tooltip />
            <el-table-column prop="adminName" label="订单状态" width="120" />
            <el-table-column prop="operation" label="操作" fixed="right" width="150px" v-if="!isKF">
                <template #default="scope">
                    <div class="operation">
                        <el-popconfirm title="确定要通过此条记录吗？" @confirm="handleApprove(scope.row)">
                            <template #reference>
                                <el-button v-if="hasrefundWaitThough" :disabled="clicked || cancelClicked"
                                    type="primary" text>
                                    通过
                                </el-button>
                            </template>
                            <template #actions="{ confirm, cancel }">
                                <el-button size="small" @click="cancel">取消</el-button>
                                <el-button type="primary" size="small" :disabled="clicked" @click="confirm">
                                    确认
                                </el-button>
                            </template>
                        </el-popconfirm>
                        <el-button v-if="hasrefundWaitReject" :disabled="clicked || cancelClicked" type="primary" text
                            @click="showRejectDialog(scope.row)">
                            驳回
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="pagination.pageNum" v-model:page-size="pagination.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
            @size-change="handlePageSizeChange" @current-change="handleCurrentChange"
            style="margin-top: 1vh;"></el-pagination>
    </el-card>

    <el-dialog v-model="showAudit1" title="审核" overflow draggable width="20vw">
        <div class="audit1">
            <div class="audit-item">
                <el-text style="width:4vw;">精网号</el-text>
                <el-input v-model="addForm.jwcode" placeholder="请输入精网号" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">姓名</el-text>
                <el-input v-model="addForm.name" placeholder="请输入客户姓名" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">所属地区</el-text>
                <el-input v-model="addForm.market" placeholder="请输入所属地区" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">活动名称</el-text>
                <el-input v-model="addForm.orderCode" placeholder="请输入活动名称" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">商品名称</el-text>
                <el-input v-model="addForm.orderCode" placeholder="请输入活动名称" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">付款币种</el-text>
                <el-input v-model="addForm.refundAmount" placeholder="请输入付款金额" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">付款金额</el-text>
                <el-input v-model="addForm.refundAmount" placeholder="请输入付款金额" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">支付方式</el-text>
                <el-input v-model="addForm.refundAmount" placeholder="请输入付款金额" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">付款时间</el-text>
                <el-input v-model="addForm.refundAmount" placeholder="请输入付款金额" style="width:10vw;" clearable />
            </div>
            <div class="audit-item">
                <el-text style="width:4vw;">银行流水订单号</el-text>
                <el-input v-model="addForm.refundAmount" placeholder="请输入付款金额" style="width:10vw;" clearable />
            </div>

        </div>
        <div style="display:flex;justify-content: center;">
            <el-button type="primary" @click="">驳回</el-button>
            <el-button type="primary" @click="">通过</el-button>
        </div>
    </el-dialog>

    <el-dialog v-model="showAudit2" title="审核" class="audit2" overflow draggable>
        <div class="top">
            <el-button @click="" class="smallTitle" size="small">退款申请信息</el-button>
            <div class="top-item">
                <el-text style="width:4vw;" size="small">退款类型</el-text>
                <el-input v-model="auditForm.refundType" placeholder="请输入退款类型" size="small" style="width:10vw;"
                    clearable></el-input>
            </div>
            <div class="top-item">
                <el-text style="width:4vw;" size="small">退款金额</el-text>
                <el-input v-model="auditForm.refundAmount" placeholder="请输入退款金额" size="small" style="width:10vw;"
                    clearable></el-input>
            </div>
            <div class="top-item">
                <el-text style="width:4vw;" size="small">退款备注</el-text>
                <el-input v-model="auditForm.refundReason" placeholder="请输入退款备注" size="small" style="width:10vw;"
                    :rows="3" maxlength="100" show-word-limit type="textarea" />
            </div>
        </div>
        <el-button @click="" class="smallTitle" size="small">原订单信息</el-button>
        <div class="center">
            <div class="center-left">
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">精网号</el-text>
                    <el-input v-model="addForm.jwcode" placeholder="请输入精网号" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">所属地区</el-text>
                    <el-input v-model="addForm.name" placeholder="请输入所属地区" size="small" style="width:10vw;" clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">产品名称</el-text>
                    <el-input v-model="addForm.productName" placeholder="请输入产品名称" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">付款金额</el-text>
                    <el-input v-model="addForm.orderCode" placeholder="请输入付款金额" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">到账金额</el-text>
                    <el-input v-model="addForm.orderCode" placeholder="请输入到账金额" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">支付方式</el-text>
                    <el-input v-model="addForm.refundAmount" placeholder="请输入支付方式" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">付款金额</el-text>
                    <el-input v-model="addForm.refundAmount" placeholder="请输入付款金额" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:3vw;padding-right: 1vw;" size="small">银行流水订单号</el-text>
                    <el-input size="small" v-model="addForm.bankOrderNo" placeholder="请输入银行流水订单号" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">审核人</el-text>
                    <el-input v-model="addForm.auditUser" placeholder="请输入审核人" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="center-item">
                    <el-text style="width:4vw;" size="small">转账凭证</el-text>
                    <el-form-item :rules="{ required: true, message: '请上传图片', trigger: 'change' }">
                        <el-upload ref="uploadRef" :auto-upload="false" :http-request="customUpload"
                            list-type="picture-card" :on-change="handleImageChange" class="image"
                            :on-success="handleUploadSuccess" :on-error="handleUploadError"
                            :before-upload="beforeUpload" :show-file-list="false">
                            <template #default>
                                <img v-if="addForm.imageUrl" :src="addForm.imageUrl"
                                    style="width: 100%; height: 100%; object-fit: cover;">
                                <el-icon v-else>
                                    <Plus />
                                </el-icon>
                            </template>
                        </el-upload>
                    </el-form-item>
                </div>
            </div>
            <div class="center-right">
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">客户姓名</el-text>
                    <el-input v-model="addForm.customerName" placeholder="请输入客户姓名" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">活动名称</el-text>
                    <el-input v-model="addForm.activityName" placeholder="请输入活动名称" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">付款币种</el-text>
                    <el-input v-model="addForm.paymentCurrency" placeholder="请输入付款币种" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">到账币种</el-text>
                    <el-input v-model="addForm.receiveCurrency" placeholder="请输入到账币种" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">手续费</el-text>
                    <el-input v-model="addForm.fee" placeholder="请输入手续费" size="small" style="width:10vw;" clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">付款时间</el-text>
                    <el-input v-model="addForm.paymentTime" placeholder="请输入付款时间" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">提交人</el-text>
                    <el-input v-model="addForm.submitUser" placeholder="请输入提交人" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">到账时间</el-text>
                    <el-input v-model="addForm.receiveTime" placeholder="请输入到账时间" size="small" style="width:10vw;"
                        clearable />
                </div>
                <div class="right-item">
                    <el-text style="width:4vw;" size="small">备注</el-text>
                    <el-input v-model="addForm.remark" placeholder="请输入备注" size="small" style="width:10vw;" :row="3"
                        clearable />
                </div>
            </div>
        </div>
        <el-button @click="" class="smallTitle" size="small">转交信息</el-button>
        <div class="bottom">
            <div class="bottom-item">
                <el-text style="width:4vw;" size="small">执行人</el-text>
                <el-select v-model="addForm.executor" size="small" placeholder="请选择执行人" style="width:10vw;">
                    <el-option v-for="item in executor" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </div>
        </div>
    </el-dialog>

    <el-dialog v-model="showSteps" title="唉！！！" overflow draggable width="1206px" height="506px" :style="{
        backgroundImage: 'url(/src/assets/images/背景图.png)',
        backgroundSize: 'cover',
        backgroundPosition: 'center'
    }">
        <div class="steps">
            <div class="steps-content">
                <el-steps style="min-width: 60vw" :active="1" align-center>
                    <el-step> <template #title>
                            <div>提交人<br>你是死的</div>
                        </template>
                        <template #icon>
                            <img src="@/assets/images/已审核.png" alt="已审核图标">
                        </template>
                    </el-step>
                    <el-step title="地区财务">
                        <template #icon>
                            <img src="@/assets/images/待审核.png" alt="待审核图标">
                        </template>
                    </el-step>
                    <el-step title="地区负责人">
                        <template #icon>
                            <img src="@/assets/images/还没传到.png" alt="还没传到图标">
                        </template>
                    </el-step>
                    <el-step title="总部财务">
                        <template #icon>
                            <img src="@/assets/images/还没传到.png" alt="还没传到图标">
                        </template>
                    </el-step>
                    <el-step title="指定执行人">
                        <template #icon>
                            <img src="@/assets/images/还没传到.png" alt="还没传到图标">
                        </template>
                    </el-step>
                </el-steps>
            </div>
            <div class="steps-status">
                <el-steps style="min-width: 50vw" :active="1" finish-status="success" simple>
                    <el-step title="已通过" />
                    <el-step title="待审核" />
                    <el-step title="待审核" />
                    <el-step title="待审核" />
                    <el-step title="待审核" />
                </el-steps>
            </div>
            <div class="steps-btn">
                <el-button type="primary">确定</el-button>
            </div>
        </div>
    </el-dialog>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import API from '@/util/http.js'
const uploadUrl = 'https://api.homilychart.com/hljw/api/aws/upload'
import { useAdminStore } from "@/store/index.js"
import { storeToRefs } from "pinia"
import dayjs from 'dayjs'
const adminStore = useAdminStore()
const { adminData, menuTree } = storeToRefs(adminStore)
import { permissionMapping, findMenuById } from "@/utils/menuTreePermission.js"

//客服
const isKF = ref(adminData.value.adminName.includes('客服'))
// 地区
const isDQ = ref(adminData.value.adminName.includes('财务') || adminData.value.adminName.includes('经理') && !adminData.value.adminName.includes('总部'))
// 总部
const isZB = ref(adminData.value.adminName.includes('总部') || adminData.value.adminName.includes('管理员'))
const searchForm = ref({
    jwcode: ''
})
const addForm = ref({
    jwcode: ''
})
const auditForm = ref({
    refundType: ''
})
const pagination = ref({
    pageNum: 1,
    pageSize: 50,
    total: 0
})
const tableData = ref([])
const showAdd = ref(false)
const showAudit1 = ref(false)
const showAudit2 = ref(false)
const showSteps = ref(false)
const uploadRef = ref(null)
const showBack = ref(false)
const showError = ref(false)
const executor = ref([
    {
        value: '305485',
        label: '新:Zoe'
    },
    {
        value: '200723',
        label: '马:Jennifer'
    },
    {
        value: '301388',
        label: '港:林育平'
    },
    {
        value: '839448',
        label: '加:Mandy'
    },
    {
        value: '304904',
        label: '泰:杨丽娇'
    },
    {
        value: '315915',
        label: '越:范秋霞'
    }
])
const products = ref([
    {
        value: '1',
        label: '产品1'
    },
    {
        value: '2',
        label: '产品2'
    }
])
const currencies = ref([
    {
        value: 'CNY',
        label: '人民币'
    },
    {
        value: 'USD',
        label: '美元'
    }
])
const payments = ref([{
    value: '银行转账',
    label: '银行转账'
},
{
    value: '现金',
    label: '现金'
},
{
    value: '支票',
    label: '支票'
},
{
    value: '刷卡',
    label: '刷卡'
},
{
    value: 'Grabpay',
    label: 'Grabpay'
},
{
    value: 'Nets',
    label: 'Nets'
},
{
    value: 'PayPal',
    label: 'PayPal'
},
{
    value: 'Stripe-链接收款',
    label: 'Stripe-链接收款'
},
{
    value: 'Ipay88-链接收款',
    label: 'Ipay88-链接收款'
},
{
    value: 'PaymentAsia-链接收款',
    label: 'PaymentAsia-链接收款'
},
{
    value: 'Stripe-Link平台',
    label: 'Stripe-Link平台'
},
{
    value: 'PaymentAsia-Link平台',
    label: 'PaymentAsia-Link平台'
},
{
    value: 'FirstData-Link平台-Link平台',
    label: 'FirstData-Link平台-Link平台'
},
{
    value: 'IOS-Link平台',
    label: 'IOS-Link平台'
},
{
    value: 'Ipay88-Link平台',
    label: 'Ipay88-Link平台'
}
])
const statusList = ref([
    {
        value: '10',
        label: '地区财务待审核'
    },
    {
        value: '12',
        label: '地区财务驳回'
    },
    {
        value: '20',
        label: '地区负责人待审核'
    },
    {
        value: '22',
        label: '地区负责人驳回'
    },
    {
        value: '30',
        label: '总部财务待审核'
    },
    {
        value: '32',
        label: '总部财务驳回'
    },
    {
        value: '40',
        label: '执行人待处理'
    },
    {
        value: '41',
        label: '退款完成'
    }
])
// 查全部
const getRefund = async function () {
    try {
        const params = {
            cashCollection: {
                status: null,
                //10：地区财务待审核；12：地区财务驳回；
                // 20：地区负责人待审核；22：地区负责人驳回；
                // 30：总部财务待审核；32：总部财务驳回；
                // 40：执行人待处理；41：执行人已处理，退款完成；
                orderCode: null,//订单号
                name: null,//姓名
                jwcode: null,//精网号
                market: null,//地区
                goodsName: null,//商品名
                refundModel: null,//退款类型,
                startTime: "",
                endTime: ""
            }
        }

        const result = await API({
            url: '/hljw/api/refund/query',
            method: 'POST',
            data: params
        })

        tableData.value = result.data || []
    } catch (error) {
        ElMessage.error(error.message || '查询失败')
    }
}
const handleImageChange = (file) => {
    uploadRef.value.submit()
}
const beforeUpload = (file) => {
    const isJPG = file.type === 'image/jpeg'
    const isPNG = file.type === 'image/png'
    const isLt1 = file.size / 1024 < 1024

    if (!isJPG && !isPNG) {
        ElMessage.error('上传图片只能是 JPG 或 PNG 格式')
        return false
    }
    if (!isLt1) {
        ElMessage.error('上传图片大小不能超过1MB')
        return false
    }
    return true
}
const handleUploadSuccess = (response, file, fileList) => {
    try {
        addForm.value.imageUrl = response.data.url
        console.log('==========================', response.data.url)
    } catch (error) {
        console.log('看看报错信息', error)
        ElMessage.error(response.data.msg || '图片上传失败')
    }
}
const handleUploadError = (error) => {
    console.error('上传失败:', error)
    addForm.value.imageUrl = null
    ElMessage.error('图片上传失败，请重试')
}
const customUpload = async (options) => {
    try {
        const formData = new FormData()
        formData.append('file', options.file)

        const response = await API({
            url: uploadUrl,
            method: 'POST',
            data: formData,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
        if (response.code === 200 && response.data) {
            handleUploadSuccess(response, options.file, [options.file])
            ElMessage.success(response.msg || '上传成功')
        } else {
            ElMessage.error(response.msg || '上传失败')
        }
    } catch (error) {
        console.error('上传错误:', error)
        ElMessage.error(`上传失败: ${error.msg || error.message || '网络异常'}`)
    }
}

onMounted(() => {
    console.log('???????????????????', adminData.value)
})
</script>
<style scoped lang="scss">
.condition {
    width: 82vw;
    display: flex;
    align-items: center;
    height: 4vh;

    .item1 {
        width: 18%;
        display: flex;
        align-items: center;
        margin-bottom: 1vh;
        margin-right: 0.5vw;
    }

    .item2 {
        width: 18%;
        display: flex;
        align-items: center;
        margin-right: 0.5vw;
    }
}

.audit1 {
    height: 47vh;

    .audit-item {
        display: flex;
        align-items: center;
        margin-bottom: 1vh;
    }
}

.audit2 {
    width: 30vw;
    height: auto;

    .top {
        width: 30vw;
        height: 17vh;

        .top-item {
            display: flex;
            align-items: center;
            margin-bottom: 1vh;
        }
    }

    .smallTitle {
        background-color: white;
        color: rgb(64, 158, 255);
        border-color: rgb(64, 158, 255);
        margin-bottom: 0.5vh;
    }

    .center {
        display: flex;
        width: 30vw;
        height: 41vh;

        .dialog-item {
            display: flex;
            align-items: center;
            margin-bottom: 1vh;
        }

        .center-left {
            width: 60%;

            .image {
                width: 4vw !important;
                height: 4vw !important;
            }

            :deep(.el-upload--picture-card) {
                width: 4vw !important;
                height: 4vw !important;
                padding: 0 !important;
            }

            .center-item {
                display: flex;
                align-items: center;
                margin-bottom: 1vh;
            }
        }

        .center-right {

            .right-item {
                display: flex;
                align-items: center;
                margin-bottom: 1vh;
            }
        }
    }

    .bottom {
        width: 30vw;
        height: 5vh;

        .bottom-item {
            display: flex;
            align-items: center;
            margin-bottom: 1vh;
        }
    }
}

.steps {
    .steps-content {
        width: 45vw;
        height: 15vh;
        display: flex;
        justify-content: center;
        padding-top: 15vw;
        padding-left: 8vw;
    }

    .steps-status {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .steps-btn {
        height: 15vh;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}
</style>