<template>
    <span v-if="actionShow">
	<view style="display:flex">
	<view style="margin:auto">
	<button
            v-for="item in transitions"
            :key="item.id"
            :type="item.attribute_type === 2 ? 'warn' : 'primary'"
            :loading="isSaveing"
            :disabled="isSaveing"
            @click="handleTransition(item)"
            style="margin-right: 2px"
            size="mini"
        >{{ item.name }}</button>
	</view>
<!-- 	<view style="margin-left:auto">
		<button v-if="props.ticket_?.state_.enable_retreat && isOwn" type="warn" size="mini"
		                        @click="handleRetreat">撤回</button>
	</view> -->
	</view>
	</span>
</template>
<script setup>
import { ref, reactive, onMounted, defineEmits, watch } from 'vue'
import API from '@/utils/api';

const props = defineProps({
    workflow_key: {type: String, default: null, required: false},
    ticket_: {type: Object, default: null, required: false},
    t_id: {type: String, default: null, required: true},
    title: {type: String, default: null, required: false},
    submit_b_func: {type: Function, default: null, required: false},
    ticket_data: {type: Object, default: null, required: false},
})

const workflow = ref(null);
const transitions = ref([]);
onMounted(async () => {
    await getUser()
	setTimeout(()=>{init()}, 500)
})
const actionShow = ref(false);
const suggestion = ref("")
const isOwn = ref(false)
const init = async () => {
	transitions.value = [];
	actionShow.value = false;
    if (props.ticket_ && Object.keys(props.ticket_).length > 0) {
		isOwn.value = props.ticket_.create_by === currentUser.value;
        const isParticipant =
        (props.ticket_.participant_type === 1 && props.ticket_.participant === currentUser.value) ||
        (props.ticket_.participant_type === 2 && props.ticket_.participant.includes(currentUser.value))
        if (isParticipant) {
            actionShow.value = true;
			transitions.value = await API.getTransition(props.ticket_.id);
        }
    }else if (props.workflow_key) {
        let res = await API.workflowInitkey(props.workflow_key);
        actionShow.value = true
        transitions.value = res.transitions;
        workflow.value = res.workflow
    }else{
		uni.showToast({
			icon:"error",
			title:"缺少workflow_key或ticketId"
		})
    }
}
const currentUser = ref(null);
const getUser = async() =>{
	currentUser.value = uni.getStorageSync("userInfo").id
}

const isSaveing = ref(false);

const emit = defineEmits(["success"]);
const submit = async (transition_id) => {
    isSaveing.value = true;
	if (props.submit_b_func) {
	    try{
	        await props.submit_b_func();
	    }catch (e) {
	        isSaveing.value = false;
	        return;
	    }
	}
    if (props.ticket_?.id) {
		console.log('props.ticket_data',props.ticket_data)
        let params = new Object();
        params.transition = transition_id;
		params.suggestion = suggestion.value;
        if (props.ticket_data) {
            params.ticket_data = props.ticket_data;
        }else{
            params.ticket_data = {};
        }
        try{
            let res = await API.ticketHandle(props.ticket_.id, params);
            isSaveing.value = false;
            uni.showToast({
                title: '提交成功',
                icon: 'success'
            });
            emit("success", props.ticket_.id)
        } catch (e) {
            isSaveing.value = false;
            return;
        }
    } else {
        let ticket = {};
        ticket.title = props.title;
        ticket.workflow = workflow.value;
        if (props.t_id != null && props.t_id != undefined) {
            ticket.ticket_data = {
                t_id: props.t_id,
            };
        } else {
            uni.showToast({
                title: '缺少t_id',
                icon: 'error'
            });
            isSaveing.value = false;
            return;
        }
        
        ticket.transition = transition_id;
        try {
            let res = await API.ticketCreate(ticket);
            isSaveing.value = false;
            uni.showToast({
                title: '提交成功',
                icon: 'success'
            });
            emit("success", res.id)
        } catch (e) {
            isSaveing.value = false;
            return;
        }
    }
};
// const handleRetreat = async () =>{
// 	await API.ticketRetreat(props.ticket_.id, {"suggestion": suggestion.value});
// }
const handleTransition = async (item) =>{
	if (item.attribute_type != 1) {
		uni.showModal({
			title: "处理意见",
			editable: true,
			success(res) {
				if(res.confirm){
					suggestion.value = res.content?res.content:"";
					submit(item.id);
				}
			}	
		})
	} else {
		submit(item.id);
	}
}
</script>