'use client'
import { useRouter, useSearchParams } from 'next/navigation'
import { useServey } from '@/hooks/useSurvey'
import { useSessionStore } from '@/store/session'
import { SurveyBasicInfo } from '@/types/Survey'
import { useState } from 'react'

export default function DetailButton({ surveyDetail }: { surveyDetail: SurveyBasicInfo | null }) {
  const router = useRouter()
  const { session } = useSessionStore()
  const { submitSurvey, deleteSurvey } = useServey(surveyDetail?.ID ?? 0)

  const searchParams = useSearchParams()
  const isTemp = searchParams.get('isTemporary')
  const [isTemporary, setIsTemporary] = useState(isTemp === 'true')

  const checkRole = () => {
    switch (session?.role) {
      case 'T01':
        return session?.userNm === surveyDetail?.REPRESENTATIVE ? true : false
      case 'Admin':
        return session?.storeNm === surveyDetail?.STORE ? true : false
      case 'Admin_Sub':
        return session?.storeNm === surveyDetail?.STORE ? true : false
      case 'Builder':
        return session?.builderNo === surveyDetail?.CONSTRUCTION_POINT ? true : false
      case 'Partner':
        return session?.builderNo === surveyDetail?.CONSTRUCTION_POINT ? true : false
      default:
        return ''
    }
  }

  const handleSubmit = async () => {
    const result = checkRole()
    if (result) {
      if (isTemporary) {
        alert('一時保存されたデータは提出できません。')
        return
      }
      window.neoConfirm(
        '提出しますか？？',
        async () => {
          if (surveyDetail?.ID) {
            // TODO: 제출 페이지 추가
            alert('SUBMIT POPUP!!!!!!!!!!!')
            await submitSurvey()
          }
        },
        () => null,
      )
    }
  }
  const handleUpdate = () => {
    const result = checkRole()
    if (result) {
      // router.push(`/survey-sale/basic-info?id=${surveyDetail?.ID}&isTemp=${isTemporary}`)
      router.push(`/survey-sale/regist?id=${surveyDetail?.ID}`)
    } else {
      alert('担当者のみ修正可能です。')
    }
  }
  const handleDelete = async () => {
    window.neoConfirm(
      '削除しますか？',
      async () => {
        if (surveyDetail?.ID) {
          if (session.userNm === surveyDetail?.REPRESENTATIVE) {
            await deleteSurvey()
            alert('削除されました。')
            router.push('/survey-sale')
          } else {
            alert('担当者のみ削除可能です。')
          }
        }
      },
      () => null,
    )
  }

  const isSubmitter = session?.storeNm === surveyDetail?.STORE && session?.builderNo === surveyDetail?.CONSTRUCTION_POINT

  return (
    <div className="btn-flex-wrap">
      <div className="btn-bx">
        <button className="btn-frame n-blue icon" onClick={() => router.push('/survey-sale')}>
          リスト<i className="btn-arr"></i>
        </button>
      </div>
      {isSubmitter && surveyDetail?.SUBMISSION_STATUS ? (
        <></>
      ) : (
        <>
          {isTemporary || surveyDetail?.SUBMISSION_STATUS ? (
            <></>
          ) : (
            <>
              <div className="btn-bx">
                <button className="btn-frame red icon" onClick={handleSubmit}>
                  提出<i className="btn-arr"></i>
                </button>
              </div>
            </>
          )}
          <div className="btn-bx">
            <button className="btn-frame n-blue icon" onClick={handleUpdate}>
              修正<i className="btn-arr"></i>
            </button>
          </div>
          <div className="btn-bx">
            <button className="btn-frame n-blue icon" onClick={handleDelete}>
              削除<i className="btn-arr"></i>
            </button>
          </div>
        </>
      )}
    </div>
  )
}
