<template>
    <view>
        <!-- 空状态 -->
        <view v-if="!orderList || orderList.length === 0" class="empty-state">
            <image src="/static/images/ydd/empty.png" mode="aspectFit" class="empty-image"></image>
            <text class="empty-text">暂无订单数据</text>
        </view>
        
        <!-- 订单列表 -->
        <up-list v-else @scrolltolower="scrolltolower">
        <up-list-item v-for="item in orderList">
            <view class="mb28 container-list-item">
                <view class="flex-between flex" style="background: #FFF4E5;padding: 22rpx 42rpx">
                    <view>
                        <text v-if="current != 2">待接单</text>
                    </view>
                    <view>本单酬劳
                        <text style="color: #FF530A">￥{{ item.orderGive }}</text>
                    </view>
                </view>
                <view class="container-list">
                    <view class="flex mb28" 
					@click="toDetail(pet.id)"
					v-for="(pet, index) in item.h5OrderVO.petVOList">
                        <up-image style="flex-shrink:0" class="mr20" width="70px" height="70px" :src="pet.photo"
                            shape="circle"></up-image>
                        <view>
                            <view class="font28 col3">服务天数: 共{{ pet.orderServiceText.length }}天 I 
                                {{ pet.orderServiceText.length >= 2 ? `${pet.orderServiceText[0]}，${pet.orderServiceText[pet.orderServiceText.length - 1]}` : pet.orderServiceText.join(',') }}
                            </view>
                            <!-- <view class="font28 col3">服务天数: 共{{ item.h5OrderVO.orderServiceList.length }}天 I

                                <text v-for="itemService in item.h5OrderVO.orderServiceList" :key="itemService.id">
                                    {{ itemService.serviceDate }}
                                </text>

                            </view> -->

                            <!-- <view style="margin: 18rpx 0">期望上门时间：{{itemService.expectServiceTime}}</view> -->
                            <view class="" style="margin-top: 10rpx;">{{ pet.breed }}({{ pet.bodyType }}) | {{ pet.productNameText.join(',') }}

                                <!-- <text v-for="itemPet in item.h5OrderVO.orderItemList" :key="itemPet.id">
                                    {{ itemPet.productName }}
                                </text> -->

                            </view>

                        </view>
                    </view>
                </view>
                <view class="btm">
                	<view class="mb28 address" style="font-size: 26rpx;">{{ item.address }}</view>
					
                	<view class="mb28" style="padding-left: 20rpx;font-size: 26rpx;"
                	v-if="item.type == 0">订单为系统派发，请确认订单信息后再抢单</view>
					
                	<view class="flex flex-between" v-if="item.status != 2">
                	    <up-button @click="unableToAcceptOrder(item.id)" text="无法接单" shape="circle" plain
                	        class="mr20"></up-button>
                	    <up-button @click="toOrderDetail(item.id)" type="primary" text="查看详情后接单" shape="circle"
                	        color="#FFAA48"></up-button>
                	</view>
					<!-- <view class="flex flex-between" v-if="item.status == 2">
					    <up-button @click="toOrderDetail(item.id)" type="primary" text="查看派单详情" shape="circle"
					        color="#FFAA48"></up-button>
					</view> -->
                </view>
            </view>
        </up-list-item>
    </up-list>
    </view>

    <Modal @confirm="confirm" @cancel="cancel" ref="modal">
        <template>
            <view class="tip">
                确认要拒绝该系统派单的订单吗?
            </view>
        </template>
    </Modal>
    <up-popup :show="showCause" mode="bottom" @close="close" @open="open" :round="10" :closeable="true"
        :safeAreaInsetBottom="false" :customStyle="{ padding: '60rpx 20rpx 40rpx 20rpx;position:relative;height:auto;' }">
        <view>
            <view style="position:absolute;top: 20rpx;text-align: center;width: 100%;">
                请补充无法接单原因
            </view>
            <view style="margin: 20rpx 0rpx;">
                <up-textarea style="background-color: #f0f0f0;" v-model="cause" placeholder="请输入内容"
                    border="none"></up-textarea>
            </view>
            <up-button @click="noneOrder" color="#FFBF60" text="提交" shape="circle"></up-button>
        </view>
    </up-popup>
</template>
<script setup>
import {
    computed,
    ref
} from "vue";
import Modal from "@/components/Modal/index.vue"
import {
    endByOrderId
} from "@/api/receivingHall/index.js"
import { useStore } from "vuex"

const modal = ref(null);
const showCause = ref(false);
const cause = ref("");
const id = ref(0);
const store = useStore();

const userInfo = computed(() => {
    return store.getters.userInfo
})

const partnerMaster = computed(() => {
	const { userHh } = userInfo.value

	if (!userHh) {
		return ''
	}

	const key = `partner_${LEVEL_AND_KEY_FIELDS_MAPPING[userHh]}`

	return configList.value?.[key]?.paramValueImage
})

const scrolltolower = () => {

}

const props = defineProps({
    orderList: {
        type: Array,
        default: () => []
    },
    current: {
        type: Number,
        required: true
    }
})

const emits = defineEmits(['update']);

// 跳转订单详情
const toOrderDetail = (id) => {
	if(checkUserBcs()) return
	
    uni.navigateTo({
        url: `/otherPages/orderTakingManage/detail/index?id=${id}`
    });
}

const confirm = () => {
    open();
}

const cancel = () => {

}


const toDetail = (id) => {
	uni.navigateTo({
		url: `/otherPages/orderTakingManage/pet/detail?id=${id}`
	})
}

function checkUserBcs(){
	const { userBcs, userBcsRole } = userInfo.value
	
	if(userBcs == 0){
		uni.showModal({
			title: '完成伴宠师认证认证后在进行操作！',
			content: '当前无法操作，如果您已经提交伴宠师认证，请耐心等待',
			confirmText: '去查看',
			success(res) {
				if(res.confirm){
					uni.navigateTo({
						url: "/otherPages/authentication/list/index"
					})
				}
			}
		})
		return true
	}
	return false
}

const unableToAcceptOrder = (currentId) => {
	if(checkUserBcs()) return
    modal.value.open();
    id.value = currentId;
}

const open = () => {
    showCause.value = true;
}

const close = () => {
    showCause.value = false;
}


// 无法接单逻辑
const noneOrder = async () => {
    if (!id.value || !userInfo.value.userId) return
    if (!cause.value) {
        return uni.showToast({
            title: '提交无法接单原因成功~',
            icon: "none"
        })
    }

    let response = await endByOrderId({
        id: id.value,
        reason: cause.value,
        userId: userInfo.value.userId
    });
    if (response.code == 200 && response.data) {
        uni.showToast({
            title: '提交无法接单原因成功~',
            icon: "none"
        });
        emits('update');
        close();
    }
}
</script>

<style scoped lang="scss">
@import "../index";

.tip {
    text-align: center;
    padding: 25rpx 0rpx;
}
.text-ellipsis{
  overflow:hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  -o-text-overflow:ellipsis;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80rpx 40rpx;
    
    .empty-image {
        width: 200rpx;
        height: 200rpx;
        margin-bottom: 20rpx;
    }
    
    .empty-text {
        color: #999;
        font-size: 28rpx;
    }
}
</style>