'use client'

import { useEffect, useState } from 'react'
import { useSurveySaleTabState } from '@/store/surveySaleTabState'
import type { SurveyBasicRequest } from '@/types/Survey'
import type { Mode } from 'fs'
import { useSessionStore } from '@/store/session'
import { usePopupController } from '@/store/popupController'
import { useAddressStore } from '@/store/addressStore'

export default function BasicForm(props: { basicInfo: SurveyBasicRequest; setBasicInfo: (basicInfo: SurveyBasicRequest) => void; mode: Mode }) {
  const { basicInfo, setBasicInfo, mode } = props
  const { setBasicInfoSelected } = useSurveySaleTabState()
  const [isFlip, setIsFlip] = useState<boolean>(true)

  const { session } = useSessionStore()
  const { addressData } = useAddressStore()

  useEffect(() => {
    setBasicInfoSelected()
  }, [])

  useEffect(() => {
    if (session?.isLoggedIn) {
      setBasicInfo({
        ...basicInfo,
        representative: session.userNm ?? '',
        store: session.role === 'Partner' ? null : session.storeNm ?? null,
        constructionPoint: session.builderNo ?? null,
      })
    }
    if (addressData) {
      setBasicInfo({
        ...basicInfo,
        postCode: addressData.post_code,
        address: addressData.address,
        addressDetail: addressData.address_detail,
      })
    }
  }, [session, addressData])

  const popupController = usePopupController()

  return (
    <>
      <div className={`sale-detail-toggle-bx ${isFlip ? 'act' : ''}`}>
        <div className="sale-detail-toggle-head" onClick={() => setIsFlip(!isFlip)}>
          <div className="sale-detail-toggle-name">基本情報</div>
          <div className="sale-detail-toggle-btn-wrap">
            <button className="sale-detail-toggle-btn"></button>
          </div>
        </div>
        <div className="sale-detail-toggle-cont">
          <div className="sale-frame">
            <div className="data-form-wrap">
              <div className="data-input-form-bx">
                <div className="data-input-form-tit">担当者名</div>
                <input
                  type="text"
                  className="input-frame"
                  readOnly
                  value={basicInfo?.representative ?? ''}
                  onChange={(e) => setBasicInfo({ ...basicInfo, representative: e.target.value })}
                />
              </div>
              {(session?.role === 'Builder' || session?.role?.includes('Admin')) && (
                <div className="data-input-form-bx">
                  <div className="data-input-form-tit">販売店</div>
                  <input
                    type="text"
                    className="input-frame"
                    readOnly
                    value={basicInfo?.store ?? ''}
                    onChange={(e) => setBasicInfo({ ...basicInfo, store: e.target.value })}
                  />
                </div>
              )}
              {(session?.role === 'Builder' || session?.role === 'Partner') && (
                <div className="data-input-form-bx">
                  <div className="data-input-form-tit">施工店</div>
                  <input
                    type="text"
                    className="input-frame"
                    readOnly
                    value={basicInfo?.constructionPoint ?? ''}
                    onChange={(e) => setBasicInfo({ ...basicInfo, constructionPoint: e.target.value })}
                  />
                </div>
              )}
            </div>
          </div>
          <div className="sale-frame">
            <div className="data-form-wrap">
              <div className="data-input-form-bx">
                <div className="data-input-form-tit">現地調査日</div>
                {['CREATE', 'EDIT'].includes(mode as 'CREATE' | 'EDIT') ? (
                  <div className="date-input">
                    <button className="date-btn">
                      <i className="date-icon"></i>
                    </button>
                    <input
                      id="investigationDate"
                      type="date"
                      className="date-frame"
                      defaultValue={basicInfo?.investigationDate?.toString()}
                      onChange={(e) => setBasicInfo({ ...basicInfo, investigationDate: e.target.value })}
                    />
                  </div>
                ) : (
                  <input type="date" className="input-frame" disabled defaultValue={basicInfo?.investigationDate?.toString()} />
                )}
              </div>
              <div className="data-input-form-bx">
                {/* 건물명 */}
                <div className="data-input-form-tit">建物名</div>
                <input
                  type="text"
                  className="input-frame"
                  readOnly={mode === 'READ'}
                  defaultValue={basicInfo?.buildingName ?? ''}
                  onChange={(e) => setBasicInfo({ ...basicInfo, buildingName: e.target.value })}
                />
              </div>
              <div className="data-input-form-bx">
                {/* 고객명 */}
                <div className="data-input-form-tit">お客様名</div>
                <input
                  type="text"
                  className="input-frame"
                  readOnly={mode === 'READ'}
                  defaultValue={basicInfo?.customerName ?? ''}
                  onChange={(e) => setBasicInfo({ ...basicInfo, customerName: e.target.value })}
                />
              </div>
              <div className="data-input-form-bx">
                <div className="data-input-form-tit">郵便番号/都道府県</div>
                <div className="form-flex">
                  {/* 우편번호 */}
                  <div className="form-bx">
                    <input type="text" className="input-frame" readOnly={mode === 'READ'} defaultValue={basicInfo?.postCode ?? ''} disabled />
                  </div>
                  {/* 도도부현 */}
                  <div className="form-bx">
                    <input type="text" className="input-frame" readOnly={mode === 'READ'} defaultValue={basicInfo?.address ?? ''} />
                  </div>
                </div>
                {/* 주소 */}
                <div className="form-btn">
                  <button className="btn-frame n-blue icon" onClick={() => popupController.setZipCodePopup(true)}>
                    郵便番号<i className="btn-arr"></i>
                  </button>
                </div>
              </div>

              <div className="data-input-form-bx">
                <div className="data-input-form-tit">市区町村名, 以後の住所</div>
                <input type="text" className="input-frame" defaultValue={basicInfo?.addressDetail ?? ''} readOnly={mode === 'READ'} />
              </div>
            </div>
          </div>
        </div>
      </div>
    </>
  )
}
