<template>

    <view>
        <uni-section
            :sub-title="queryParams.contractcode"
            :title="queryParams.user_name"
            padding="0 0 5px 10px"
            style="margin-bottom: 20px"
        />

        <MBCard style="margin: 15px">
            <view class="context">
                <view class="label">
                    <text>制单日期:</text>
                    <text>{{ queryParams.adddate }}</text>
                </view>
                <view class="label">
                    <text>支付方式:</text>
                    <text>{{ queryParams.ispaynow }}</text>
                </view>
                <view class="label">
                    <text>联系方式:</text>
                    <text>{{ queryParams.phonenumber }}</text>
                </view>
                <view class="label">
                    <text>订单金额:</text>
                    <text>{{ formatPrice(queryParams.contractmoney) }}</text>
                </view>
                <view class="label">
                    <text>补差金额:</text>
                    <text>{{ formatPrice(queryParams.piaokou) }}</text>
                </view>
                <view class="label">
                    <text>折后订单金额:</text>
                    <text>{{ formatPrice(queryParams.piaokous) }}</text>
                </view>

                <view class="label">
                    <text>本月商务额度:</text>
                    <text>{{ formatPrice(queryParams.budgetmoney1 ?? 0) }}</text>
                </view>
                <view class="label">
                    <text>本月商务可用:</text>
                    <text>{{ formatPrice(queryParams.CAN ?? 0) }}</text>
                </view>
                <view class="label">
                    <text>本月大区额度:</text>
                    <text>{{ formatPrice(queryParams.budgetmoney_area ?? 0) }}</text>
                </view>
                <view class="label">
                    <text>本月大区可用:</text>
                    <text>{{ formatPrice(queryParams.CAN_area ?? 0) }}</text>
                </view>
                <view class="label">
                    <text>执行月份:</text>
                    <DictSelect v-model="queryParams.actionType" dict="dazhong_thismonthornext" disabled/>
                </view>
            </view>
            <view
                style="display: flex;gap: 4px;font-size: 14px;color: #666;margin-bottom: 4px;font-weight: 500;flex-direction: column;">
                <view style="width: 100px">收货地址:</view>
                <view>{{ queryParams.place }}</view>
            </view>
        </MBCard>

        <uni-section class="mb-10" padding="0 0 5px 10px" title="商品信息"/>

        <uni-card
            v-for="item in queryParams.goods"
            :key="item.goodsname"
            :title="item.goodsname"
        >
            <view class="context">
                <view class="label">
                    <text>件装数:</text>
                    <text>{{ item.packingnum }}</text>
                </view>
                <view class="label">
                    <text>供应参考价:</text>
                    <text>{{ item.invoiceprice }}</text>
                </view>
                <view class="label">
                    <text>前三月平均数:</text>
                    <text>{{ item.mon3 }}</text>
                </view>
                <view class="label">
                    <text>采购数量:</text>
                    <text>{{ item.goodsnum }}</text>
                </view>
                <view class="label">
                    <text>小计:</text>
                    <text>{{ formatPrice(item.allmoney) }}</text>
                </view>
                <view class="label">
                    <text>使用票扣:</text>
                    <text>{{ formatPrice(item.piaokou) }}</text>
                </view>
                <view class="label">
                    <text>税率:</text>
                    <text>{{ item.taxrate }}</text>
                </view>
            </view>
        </uni-card>
        <view style="display: flex;margin: 0 15px;justify-content: space-between;">
            <uni-tag
                style="font-size: 20px;line-height: 28px;font-weight: bold;padding: 5px 25px;"
                text="回退"
                type="success"
                @click="handleCallBack"
            />
            <uni-tag
                style="font-size: 20px;line-height: 28px;font-weight: bold;padding: 5px 25px;"
                text="审核"
                type="primary"
                @click="handleSubmit"
            />
        </view>
    </view>
</template>

<script setup>
import {getCurrentInstance, onMounted, ref} from 'vue'
import {onLoad} from '@dcloudio/uni-app'
import {
    businessManagerReviewGoodsList,
    managerConfirm,
    managerRefuse,
    salemainDetailSWJL
} from "../../../api/BusinessApproval";
import UniCard from "../../../uni_modules/uni-card/components/uni-card/uni-card.vue";
import UniTag from "../../../uni_modules/uni-tag/components/uni-tag/uni-tag.vue";
import {formatPrice} from "../../../utils/utils";
import DictSelect from "../../../components/DictSelect/DictSelect.vue";
import MBCard from "../../../components/MB/MBCard.vue";

const id = ref('')
const queryParams = ref({})

const {proxy} = getCurrentInstance()

onLoad((options) => {
    id.value = options.id
})

onMounted(async () => {
    const actionType = new Date().getDate() < 25 ? '1' : '0'
    const {data} = await salemainDetailSWJL({
        id: id.value,
        isThisMonth: actionType
    })
    queryParams.value = data[0]

    const {data: goods} = await businessManagerReviewGoodsList(id.value)
    queryParams.value = {
        ...queryParams.value,
        actionType: actionType,
        goods: goods
    }
})


const handleCallBack = () => {
    managerRefuse({
        saleIds: queryParams.value.saleid,
        actionType: queryParams.value.actionType
    }).then(() => {
        proxy.$modal.msgSuccess('回退成功！');
        proxy.$tab.navigateTo('/pages/work/BusinessApproval/index')
    })
}

const handleSubmit = () => {
    const orderAmount = parseFloat(queryParams.value.contractmoney || 0);
    const swAvailable = parseFloat(queryParams.value.CAN || 0);
    const dqAvailable = parseFloat(queryParams.value.CAN_area || 0);

    if (orderAmount > swAvailable) {
        proxy.$modal.msgError(`订单金额 ${orderAmount} 大于本月商务可用额度 ${swAvailable}`);
        return false;
    }

    if (orderAmount > dqAvailable) {
        proxy.$modal.msgError(`订单金额 ${orderAmount} 大于本月大区可用额度 ${dqAvailable}`);
        return false;
    }

    managerConfirm({
        saleIds: queryParams.value.saleid,
        actionType: queryParams.value.actionType
    }).then(() => {
        proxy.$modal.msgSuccess('审核成功！');
        proxy.$tab.navigateTo('/pages/work/BusinessApproval/index')
    })
}

</script>
