<template>
    <global-page title="AOG施工详情" showFooterBtn footerBtnText="修改状态" @handleFooterClick="handleFooterClick">
        <view class="mocp-cell">
            <up-cell-group v-if="getBbDetails">
                <up-cell title="负责人" :value="getBbDetails.personLiable || '-'"></up-cell>
                <up-cell title="开始时间" :value="getBbDetails.startTime || '-'"></up-cell>
                <up-cell title="预计完成时间" :value="getBbDetails.expectCompleteTime || '-'"></up-cell>
                <up-cell title="实际完成时间" :value="getBbDetails.realityCompleteTime || '-'"></up-cell>
                <up-cell title="状态">
                    <template #value>
                        <global-text-status :value="getBbDetails.state" dictkey="bbDetails_status"></global-text-status>
                    </template>
                </up-cell>
                <up-cell title="步骤" :label="getBbDetails.stepContent || '-'"></up-cell>
            </up-cell-group>
        </view>
        <global-action-sheet
            v-model="showAction"
            :options="[
                { label: '无', value: '-1' },
                { label: '开始施工', value: '0' },
                { label: '已完成', value: '1' }
            ]"
            @select="handleSelect"
        ></global-action-sheet>
        <up-modal
            title="重置状态"
            content="重置施工状态后，开始时间和完成时间将会清空，确认操作？"
            :show="showConfirm"
            showCancelButton
            closeOnClickOverlay
            @confirm="updateAogConstruction"
            @cancel="show = false"
            @close="show = false"
        ></up-modal>
    </global-page>
</template>

<script setup>
import { storeToRefs } from 'pinia'
import useAogStore from 'mocp/store/aog'
import { ref } from 'vue'
import { updateAogConstructionApi } from 'mocp/api/aog'

const aogStore = useAogStore()
const { getBbDetails, details } = storeToRefs(aogStore)
//修改状态
const showAction = ref(false)
const showConfirm = ref(false)
const handleFooterClick = () => {
    showAction.value = true
}
const state = ref('')
const handleSelect = (value) => {
    state.value = value
    if (value == '-1') {
        showConfirm.value = true
    } else {
        updateAogConstruction()
    }
}
const updateAogConstruction = async () => {
    const params = {
        id: getBbDetails.value?.id,
        realityCompleteTime: 0,
        startTime: 0,
        state: state.value,
        workbenchId: details.value?.workbenchId
    }
    if (state.value === '0') {
        params.startTime = Date.now()
    } else if (state.value === '1') {
        params.realityCompleteTime = Date.now()
    }
    const res = await updateAogConstructionApi(params, { loading: true })
    if (res.code == 200) {
        uni.$mocpJump.navigateBack()
        uni.$mocpMessage.success(res.message)
        uni.$emit('updateBbData')
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
</script>
<style lang="scss" scoped>
.mocp-cell {
    background: #fff;
}
</style>
