<template>
    <div>
        <ListPage :column="listPageData.tableColumn" :tableApi="listPageData.tableApi" :isSelect="listPageData.isSelect"
            :scopeBtns="listPageData.scopeBtns" :searchBtns="listPageData.searchBtns" ref="userTableRef"
            v-if="options && options.length > 0" />
        <DialogForm ref="dialogFormRef" :title="title" :column="dialogData.column" @submit="submitForm" />
    </div>
</template>

<script setup>
import { ref } from 'vue';
import ListPage from '@/views/components/ListPage/index.vue';
import { list, checkOrderRefund } from "@/api/order/examine.js";
import { getTreeList } from "@/api/staff/price.js";

import DialogForm from '@/views/components/DialogForm/index.vue';
const { proxy } = getCurrentInstance();
const { jlzj_order_refund,lrr_service_status } = proxy.useDict("jlzj_order_refund","lrr_service_status");

const options = ref([]);
const userTableRef = ref();
const dialogFormRef = ref(null);
const listPageData = reactive({
    tableColumn: [
        {
            label: '时间',
            prop: 'createTime',
            type: 'date',
            isSearch: false,
            width: '180px'
        },
        {
            label: '姓名',
            prop: 'volunteerName',
            type: 'input',
            isSearch: true,
        },
        {
            label: '服务项目',
            prop: 'businessManagementId',
            type: 'cascader',
            isSearch: true,
            tableProp: 'businessTierName',
            options: options,
            props: { label: 'businessName', value: 'id', checkStrictly:true },
        },
        {
            label: '被服务人员',
            prop: 'clientName',
        },
        {
            label: '总次数',
            prop: 'totalTimes',
        },
        {
            label: '已完成次数',
            prop: 'finishTimes',
        },
        {
            label: '订单状态',
            prop: 'orderStatus',
            type: 'dict',
            dict: lrr_service_status,
            isSearch: true,
        },
        {
            label: '退单状态',
            prop: 'orderRefundStatus',
            type: 'dict',
            dict: jlzj_order_refund
        },
        {
            label: '退款金额',
            prop: 'refundAmount',
        },
        {
            label: '备注',
            prop: 'refundReason',
        },
    ],
    searchBtns: [],
    tableApi: list,//接口地址
    isSelect: false,//是否勾选
    scopeBtns: [
        {
            label: '审核',
            type: 'primary',
            hasPermi: ['manage:examine'],
            key: 'examine',
            func: (row) => {
                console.log(row)
                openDialog(row, 'examine')
            },
            show: (row) => {
                return row.orderRefundStatus === '0'
            }
        },
        {
            label: '查看详情',
            type: 'primary',
            hasPermi: ['manage:details'],
            key: 'details',
            func: (row) => {
                console.log(row)
                openDialog(row)
            },
            show: (row) => {
                return row.orderRefundStatus !== '0'
            }
        },
    ]
})
const dialogData = reactive({
    title: '',
    column: [
        {
            label: '时间',
            prop: 'createTime',
            type: 'input',

        },
        {
            label: '姓名',
            prop: 'volunteerName',
            type: 'input',

        },
        {
            label: '服务项目',
            prop: 'businessTierName',
            type: 'input',
        },
        {
            label: '被服务人员',
            prop: 'clientName',
            type: 'input',
        },
       
        {
            label: '退款金额',
            prop: 'refundAmount',
        },
        {
            label: '照片上传',
            prop: 'refundPicture',
            type: 'img',
        },
        {
            label: '备注',
            prop: 'refundReason',
            type: 'textarea',
        },
        {
            label: '审核状态',
            prop: 'status',
            type: 'radio',
            rules: [
                { required: true, message: '请选择审核状态', trigger: 'blur' }
            ],
            options: [
                {
                    label: '通过',
                    value: '2'
                },
                {
                    label: '不通过',
                    value: '3'
                }
            ]
        },
        {
            label: '驳回原因',
            prop: 'rejectionReason',
            type: 'textarea',
            show:(form)=>{
                console.log('驳回原因',form);
                
                return form.status === '3'
            }
        },
    ]
})
const { title } = toRefs(dialogData);
const parentId = ref(0);

const openDialog = (row, type) => {
    console.log('row', row);
    try {
        const disabledData = {
            createTime:true,
            volunteerName:true,
            businessTierName:true,
            clientName:true,
            refundAmount:true,
            refundReason:true,

        }

        if (type) {
            //审核
            title.value = '审核'

        } else {
            disabledData['status'] = true;
            //查看详情
            title.value = '查看详情'
        }

        dialogFormRef.value.initForm(row, disabledData)

    } catch (error) {
        console.log('error', error);
    } finally {
        console.log('dialogFormRef.value', dialogFormRef.value);

        dialogFormRef.value.handleDialog(true);
    }
}
const handleDelete = (ids) => {
    proxy.$modal.confirm('是否确认删除角色编号为"' + ids + '"的数据项?').then(function () {
        return true
    }).then(() => {
        userTableRef.value.resetForm();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

const submitForm = async (parmas) => {

    try {
        console.log('submit', parmas);
        if (title.value === '审核') {
            console.log('审核',);            
            const res = await checkOrderRefund({
                mainOrderId: parmas.mainOrderId,
                orderRefundId: parmas.orderRefundId,
                status: parmas.status,
                rejectionReason: parmas.rejectionReason,
                refundAmount: parmas.refundAmount
            })
            if (res.code === 200) {
                proxy.$modal.msgSuccess("审核成功");
                return;
            }
            proxy.$modal.msgSuccess(res.msg);
        }
    } catch (error) {
        console.log('error', error);

    } finally {
        userTableRef.value.resetForm();
        dialogFormRef.value.handleDialog(false);
    }

}


const getTreeListData = async () => {
    try {
        const res = await getTreeList({ parentId: parentId.value });
        console.log('res', res);
        options.value = res.data;
    } catch (error) {

    }
}

getTreeListData();

</script>

<style lang='scss' scoped></style>