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

        <MBCard style="margin: 15px">
            <view class="context">
                <view class="label">
                    <text>制单日期:</text>
                    <text>{{ formatDate(queryParams.adddate, 'yyyy-MM-dd') }}</text>
                </view>
                <view class="label">
                    <text>发货仓库:</text>
                    <text>{{ queryParams.warehouseName }}</text>
                </view>
                <view class="label">
                    <text>订单金额:</text>
                    <text>{{ formatPrice(queryParams.contractmoney) }}</text>
                </view>
                <view class="label">
                    <text>补差金额:</text>
                    <text>{{ formatPrice(queryParams.disCount) }}</text>
                </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.candisnum }}</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>{{ item.piaokoutotal }}</text>
                </view>
            </view>
        </uni-card>

        <view style="display: flex;margin: 0px 15px;justify-content: space-between;flex-direction: column;gap: 10px;">
            <uni-tag
                style="font-size: 20px;line-height: 28px;font-weight: bold;padding: 5px 25px;text-align: center;"
                text="强制推送至OA"
                type="success"
                @click="handlePushOA"
            />

            <uni-tag
                style="font-size: 20px;line-height: 28px;font-weight: bold;padding: 5px 25px;text-align: center;"
                text="强制通过OA,直接到待分配"
                type="success"
                @click="handlePushOAAwait"
            />

            <uni-popup ref="inputDialogRef" type="dialog">
                <uni-popup-dialog
                    ref="inputClose"
                    mode="input"
                    placeholder="请输入密码"
                    title="请输入密码"
                    value="对话框预置提示内容!"
                    @confirm="dialogInputConfirm"
                />
            </uni-popup>


            <uni-tag
                style="font-size: 20px;line-height: 28px;font-weight: bold;padding: 5px 25px;text-align: center;"
                text="审核回退"
                type="primary"
                @click="handleCallBack"
            />
        </view>

    </view>
</template>

<script setup>
import {getCurrentInstance, onMounted, ref} from 'vue'
import {salemainDetail} from "../../../api/orderManager";
import {onLoad} from '@dcloudio/uni-app'
import {formatPrice} from "../../../utils/utils";
import MBCard from "../../../components/MB/MBCard.vue";
import {formatDate} from "../../../uni_modules/uni-dateformat/components/uni-dateformat/date-format";
import {
    disOrderDisDetailList,
    orderReviewBack,
    orderReviewForceOaToFenPei,
    orderReviewSendOaQZ
} from "../../../api/Salemain";
import UniCard from "../../../uni_modules/uni-card/components/uni-card/uni-card.vue";


const id = ref('')
const queryParams = ref({})
const inputDialogRef = ref('')
const {proxy} = getCurrentInstance()

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


onMounted(async () => {
    const {data} = await salemainDetail(id.value)
    queryParams.value = data
    const {data: goods} = await disOrderDisDetailList(id.value)
    queryParams.value = {
        ...queryParams.value,
        goods: goods
    }
})

const pushOAStatus = ref('0')
// 强制推送 OA
const handlePushOA = () => {
    pushOAStatus.value = '1'
    inputDialogRef.value.open()
}

// 强制通过OA,直接到待分配
const handlePushOAAwait = () => {
    pushOAStatus.value = '2'
    inputDialogRef.value.open()
}

const dialogInputConfirm = (e) => {
    if (!e.trim()) return '';

    if (pushOAStatus.value === '1') {
        orderReviewSendOaQZ({
            saleIds: form.value.saleid,
            psw: e,
            companyId: queryParams.value.companyId,
            state: 1
        }).then(res => {
            proxy.$modal.msgSuccess('已强制推送至OA');
        })
    }
    if (pushOAStatus.value === '2') {
        orderReviewForceOaToFenPei({
            id: id.value,
            psw: e
        })
            .then(() => {
                proxy.$modal.msgSuccess('回退成功！');
                proxy.$tab.navigateTo('/pages/work/OrderApproval/index')
            })
    }
    console.log(e)
}

// 审核回退
const handleCallBack = () => {
    orderReviewBack(id.value)
        .then(() => {
            proxy.$modal.msgSuccess('回退成功！');
            proxy.$tab.navigateTo('/pages/work/OrderApproval/index')
        })
}
</script>