<template>
	<view>
	    <template v-if="items && items.length > 0">
			<uni-card
				v-for="(item,index) in items"
				:key="item.saleid"
				:class="index % 2 === 0 ? 'card-even' : 'card-odd'"
				:extra="item.contractcode"
				:title="`${index + 1}. ${item.usernames}`"
				style="margin: 0;margin-bottom: 20px"
				@click="handleDetail(item)"
			>
				<view class="context" v-if="userType === '01' || userType === '02'" >
					<view class="label">
						<text>货主:</text>
						<text>{{ item.companyName }}</text>
					</view>
					<view class="label">
						<text>金额:</text>
						<text>{{ formatPrice(item.contractmoney) }}</text>
					</view>
					<view class="label">
						<text>订单类型:</text>
						<view>
							<dict-tag :options="props.orderTypeList" :value="item.type"/>
						</view>
					</view>
					<view class="label">
						<text>制单日期:</text>
						<text>{{ formatDate(item.adddate, 'yyyy-MM-dd') }}</text>
					</view>
					<view class="label">
						<text>状态:</text>
						<text>
							<text>{{ item.stateText }}</text>
						</text>
					</view>
					<view class="label">
						<text>操作:</text>
							<view  class="action-buttons">
								<button type='primary' 
									:plain="true" 
									@click.stop="Edit(item)" 
									class="btn"
									v-if="item.state == 0 "
								>
									编辑
								</button>
								<button type='warn' 
									:plain="true" 
									@click.stop="deleteData(item)" 
									class="btn"
									v-if="item.state == 0 || item.state == 1 || item.state == -1 || item.state == -2"
								>删除</button>
							</view>
					</view>
				</view>
				<view class="context" v-else >
					<view class="label">
						<text>货主:</text>
						<text>{{ item.companyName }}</text>
					</view>
					<view class="label">
						<text>发货仓库:</text>
						<text>{{ item.warehouseName }}</text>
					</view>
					<view class="label">
						<text>制单日期:</text>
						<text>{{ formatDate(item.adddate, 'yyyy-MM-dd') }}</text>
					</view>
					<view class="label">
						<text>订单金额:</text>
						<text>{{ formatPrice(item.contractmoney) }}</text>
					</view>
					<view class="label">
						<text>补差金额:</text>
						<text>{{ formatPrice(item.disCount) }}</text>
					</view>
					<view class="label">
						<text>本月/下月:</text>
						<text>{{ item.actioninfo }}</text>
					</view>
					<view class="label">
						<text>支付方式:</text>
						<view>
							<text>{{ item.creditperiods > 0 ? '非现款' : '现款' }}</text>
						</view>
					</view>
					<view class="label">
						<text>状态:</text>
						<text>
							<text>{{ item.stateText }}</text>
						</text>
					</view>
					<!-- <view class="label">
						<text>操作:</text>
							<view  class="action-buttons">
								<button type='primary' 
									:plain="true" 
									@click.stop="Edit(item)" 
									class="btn"
								>
									编辑
								</button>
							</view>
					</view> -->
				</view>
			</uni-card>
		</template>
		<view v-else class="no-data">
			<text>暂无数据</text>
		</view>
	</view>
</template>

<script setup>
import {defineOptions, defineProps, getCurrentInstance,computed,onMounted, ref} from 'vue'
import UniCard from "../../../uni_modules/uni-card/components/uni-card/uni-card.vue";
import {formatDate} from "../../../uni_modules/uni-dateformat/components/uni-dateformat/date-format";
import {formatPrice} from "../../../utils/utils";
import DictTag from "../../../components/dict-tag/dict-tag.vue";

import {delsalemain} from "../../../api/orderManager/index.js"
import { getUserProfile } from "@/api/system/user"
const {proxy} = getCurrentInstance()

// const {order_state, dazhong_dingdan_type} = proxy.useDict(['order_state', 'dazhong_dingdan_type'])

import {getDicts} from "../../../api/system/dict/data.js"

defineOptions({
    name: 'ListItem'
})

// 定义 props - 从父组件接收所有需要的数据
const props = defineProps({
    items: {
        type: Array,
        default: () => []
    },
    orderTypeList: {
        type: Array,
        default: () => []
    },
    userType: {
        type: String,
        default: ''
    }
})
const emit = defineEmits(['refresh'])
// 初始化数据
onMounted(async () => {
    try {
        
    } finally {
    }
})
const Edit = async (row) => {
    try {
        console.log(row,'当前行数据')
        const params = {
            companyId: row.companyId,
            saleid: row.saleid,
            userid: row.userid,
        }
        const paramsStr = encodeURIComponent(JSON.stringify(params))
        proxy.$tab.navigateTo(`/pages/work/OrderManager/components/EditData?data=${paramsStr}`)
    } catch (error) {
        console.error('失败:', error)
    }
}
// 删除
const deleteData = async (row) => {
    uni.showModal({
        title: '删除确认',
        content: `确定删除 ${row.contractcode || ''} 吗？`,
        confirmText: '确定',
        cancelText: '取消',
        confirmColor: '#DD524D',
        success: async (res) => {
            if (res.confirm) {
                try {
                    uni.showLoading({
                        title: '删除中...',
                        mask: true
                    })
                    
                    await delsalemain(row.saleid)
                    
                    uni.hideLoading()
                    uni.showToast({
                        title: '删除成功',
                        icon: 'success'
                    })
                    
                    // 触发父组件刷新
                    emit('refresh')
                    
                } catch (error) {
                    uni.hideLoading()
                    console.error('删除失败:', error)
                    uni.showToast({
                        title: '删除失败',
                        icon: 'error'
                    })
                }
            }
        }
    })
}

const handleDetail = (row) => {
    console.log(props.userType,'userType')
    if(props.userType == '01' || props.userType == '02'){
        proxy.$tab.navigateTo(`/pages/work/OrderManager/components/LookDataSY?id=${row.saleid}`)
    }else{
        proxy.$tab.navigateTo(`/pages/work/OrderManager/components/LookDataNB?id=${row.saleid}`)
    }
}
</script>

<style lang="scss" scoped>
	:deep().uni-app-tag-span{
		font-size:12px
	}
	button{font-size: 12px;margin:0 5px;}
	.btn{
		padding-left:6px;
		padding-right: 6px;
		line-height:2;
	}
	/* 操作按钮 */
	.action-buttons {
	  display: flex;
	  justify-content: center;
	}
	.no-data {
	    text-align: center;
	    padding: 40px 0;
	    color: #999;
	    font-size: 14px;
	    background-color: #fff;
	    border-radius: 8px;
	    margin: 20px 0;
	}
</style>