<!--
  This example requires Tailwind CSS v2.0+

  This example requires some changes to your config:

  ```
  // tailwind.config.js
  module.exports = {
    // ...
    plugins: [
      // ...
      require('@tailwindcss/forms'),
    ],
  }
  ```
-->
<template>
    <div class="m-8">
        <form
            class="space-y-8 divide-y divide-gray-200"
            @submit.prevent="doCreate"
        >
            <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="first-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="first-name"
                                v-model="name"
                                type="text"
                                name="first-name"
                                autocomplete="given-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="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"
                                                        disabled
                                                        class="opacity-75 focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300"
                                                    />
                                                    <label
                                                        for="filter-mid"
                                                        class="opacity-75 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"
                                                        disabled
                                                        class="opacity-75 focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300"
                                                    />
                                                    <label
                                                        for="filter-low"
                                                        class="opacity-75 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-end">
                    <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
                        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>
        </form>
    </div>
</template>

<script setup lang="ts">
const router = useRouter();

definePageMeta({
    middleware: 'check-auth-op',
});

const name = ref('');
const level = ref('high');

async function doCancel() {
    router.back();
}

watch(level, (newValue, oldValue) => {
    console.log('level 의 변이가 감지되었을 때 ', {
        newValue,
        oldValue,
    });
});

async function doCreate() {
    const responseJson = await _crossCtl.doComm('local/insert', 'key', {
        name: name.value,
        level: level.value,
    });
    console.log('responseJson=', responseJson);
    if (responseJson['responseMessage'] == 'ok') {
        alert('ok');
        router.back();
    } else {
        if (responseJson['responseMessage'] == 'exceed limit') {
            alert(
                '키 생성 갯수 제한을 초과 할  수 없습니다. 관리자에게 문의하세요.'
            );
        } else {
            responseJson['responseMessage'];
        }
    }
}
</script>
