<template>
    <div class="m-8">
        <form
            class="space-y-8 divide-y divide-gray-200"
            @submit.prevent="doUpdate"
        >
            <div class="space-y-8 divide-y divide-gray-200 sm:space-y-5">
                <div
                    class="divide-y divide-gray-200 pt-8 space-y-6 sm:pt-10 sm:space-y-5"
                >
                    <div>
                        <h3 class="text-lg leading-6 font-medium text-gray-900">
                            API 키 상세 보기
                        </h3>
                        <p class="mt-1 max-w-2xl text-sm text-gray-500">
                            API키의 설정을 변경하거나 삭제할 수 있습니다.
                        </p>
                    </div>

                    <div
                        class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5"
                    >
                        <label
                            for="name"
                            class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2"
                        >
                            이름
                        </label>
                        <div class="mt-1 sm:mt-0 sm:col-span-2">
                            <input
                                id="name"
                                v-model="name"
                                type="text"
                                name="name"
                                autocomplete="name"
                                class="max-w-lg block w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm border-gray-300 rounded-md"
                            />
                        </div>
                    </div>
                    <div
                        class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5"
                    >
                        <label
                            for="api-key"
                            class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2"
                        >
                            API 키
                        </label>
                        <div class="mt-1 sm:mt-0 sm:col-span-2">
                            <input
                                id="api-key"
                                v-model="apiKey"
                                type="text"
                                name="api-key"
                                autocomplete="api-key"
                                disabled
                                class="max-w-lg block w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm border-gray-300 rounded-md"
                            />
                        </div>
                    </div>

                    <div
                        class="space-y-6 sm:space-y-5 divide-y divide-gray-200"
                    >
                        <div class="pt-6 sm:pt-5">
                            <div
                                role="group"
                                aria-labelledby="label-notifications"
                            >
                                <div
                                    class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-baseline"
                                >
                                    <div>
                                        <div
                                            id="label-notifications"
                                            class="text-base font-medium text-gray-900 sm:text-sm sm:text-gray-700"
                                        >
                                            필터 단계
                                        </div>
                                    </div>
                                    <div class="sm:col-span-2">
                                        <div class="max-w-lg">
                                            <p class="text-sm text-gray-500">
                                                필터 대상이 되는 단계를
                                                선택합니다. (현재는 최대 필터만
                                                동작)
                                            </p>
                                            <div class="mt-4 space-y-4">
                                                <div class="flex items-center">
                                                    <input
                                                        id="filter-high"
                                                        v-model="level"
                                                        value="high"
                                                        name="filter-level"
                                                        type="radio"
                                                        class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300"
                                                    />
                                                    <label
                                                        for="filter-high"
                                                        class="ml-3 block text-sm font-medium text-gray-700"
                                                    >
                                                        최대 필터
                                                    </label>
                                                </div>
                                                <div class="flex items-center">
                                                    <input
                                                        id="filter-mid"
                                                        v-model="level"
                                                        value="mid"
                                                        name="filter-level"
                                                        type="radio"
                                                        class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300"
                                                    />
                                                    <label
                                                        for="filter-mid"
                                                        class="ml-3 block text-sm font-medium text-gray-700"
                                                    >
                                                        중간 필터
                                                    </label>
                                                </div>
                                                <div class="flex items-center">
                                                    <input
                                                        id="filter-low"
                                                        v-model="level"
                                                        value="low"
                                                        name="filter-level"
                                                        type="radio"
                                                        class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300"
                                                    />
                                                    <label
                                                        for="filter-low"
                                                        class="ml-3 block text-sm font-medium text-gray-700"
                                                    >
                                                        가장 가벼운 필터
                                                    </label>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pt-5">
                <div class="flex justify-between">
                    <div>
                        <button
                            type="button"
                            class="bg-red-600 border border-transparent rounded-md shadow-sm py-2 px-4 inline-flex justify-center text-sm font-medium text-white hover:bg-red-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
                            @click="doDelete"
                        >
                            {{ status == 0 ? '삭제' : '복구' }}
                        </button>
                    </div>
                    <div>
                        <button
                            type="button"
                            class="bg-white py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                            @click="doCancel"
                        >
                            이전화면
                        </button>
                        <button
                            v-if="status == 0"
                            type="submit"
                            class="ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                        >
                            저장
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</template>
<script setup lang="ts">
const route = useRoute();
const router = useRouter();
definePageMeta({
    middleware: 'check-auth-admin',
});

const hero = route.params.target;

console.log('hero=', hero);

const apiKey = ref('');
const name = ref('');
const level = ref('');
const status = ref(0);

const responseJson = await _crossCtl.doComm('local/select', 'admin:key', {
    hero: hero,
});

const keyInfo = responseJson['data'][0];

apiKey.value = keyInfo['api_key'];
name.value = keyInfo['name'];
level.value = keyInfo['level'];
status.value = keyInfo['status'];

async function doUpdate() {
    const responseJson = await _crossCtl.doComm('local/update', 'admin:key', {
        hero: hero,
        name: name.value,
        level: level.value,
        status: status.value,
    });
    console.log('responseJson=', responseJson);
    if (responseJson['responseMessage'] == 'ok') {
        alert('ok');
    } else {
        alert(responseJson['responseMessage']);
    }
}

async function doDelete() {
    if (status.value == 0) {
        const responseJson = await _crossCtl.doComm(
            'local/delete',
            'admin:key',
            {
                hero: hero,
            }
        );
        console.log('responseJson=', responseJson);
        if (responseJson['responseMessage'] == 'ok') {
            status.value = 4;
            alert('ok');
        } else {
            alert(responseJson['responseMessage']);
        }
    } else {
        const responseJson = await _crossCtl.doComm(
            'local/update',
            'admin:key',
            {
                hero: hero,
                name: name.value,
                level: level.value,
                status: 0,
            }
        );
        console.log('responseJson=', responseJson);
        if (responseJson['responseMessage'] == 'ok') {
            status.value = 0;
            alert('ok');
        } else {
            alert(responseJson['responseMessage']);
        }
    }
}

async function doCancel() {
    router.back();
}
</script>
