import WithDraggable from '@/components/common/draggable/withDraggable'
import Image from 'next/image'
import { useState } from 'react'
import { useRecoilValue } from 'recoil'
import { contextPopupPositionState } from '@/store/popupAtom'
import { usePopup } from '@/hooks/usePopup'
import { useMessage } from '@/hooks/useMessage'

export default function RowInsert(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition, apply } = props
  const { closePopup } = usePopup()
  const [selectedType, setSelectedType] = useState(1)
  const { getMessage } = useMessage()
  const handleApply = () => {
    if (apply) apply()
    closePopup(id)
  }

  const HandleRadioChange = (e) => {
    setSelectedType(Number(e.target.value))
  }

  return (
    <WithDraggable isShow={true} pos={pos}>
      <div className={`modal-pop-wrap r`}>
        <div className="modal-head">
          <h1 className="title">{getMessage('modal.row.insert')} </h1>
          <button className="modal-close" onClick={() => closePopup(id)}>
            닫기
          </button>
        </div>
        <div className="modal-body">
          <div className="properties-setting-wrap">
            <div className="setting-tit">{getMessage('modal.row.insert.info')}</div>
            <div className="additional-wrap">
              <div className="additional-radio-wrap">
                <div className="d-check-radio pop">
                  <input type="radio" name="radio01" id="ra01" onChange={HandleRadioChange} value={1} checked={selectedType === 1} />
                  <label htmlFor="ra01">{getMessage('modal.row.insert.type.up')}</label>
                </div>
                <div className="d-check-radio pop">
                  <input type="radio" name="radio01" id="ra02" onChange={HandleRadioChange} value={2} checked={selectedType === 2} />
                  <label htmlFor="ra02">{getMessage('modal.row.insert.type.down')}</label>
                </div>
              </div>
              <div className="additional-img-wrap">
                {selectedType === 1 && (
                  <Image
                    src="/static/images/canvas/additional_bundle-edit01.svg"
                    alt="react"
                    width={0}
                    height={0}
                    style={{ width: 'auto', height: 'auto' }}
                  />
                )}
                {selectedType === 2 && (
                  <Image
                    src="/static/images/canvas/additional_bundle-edit02.svg"
                    alt="react"
                    width={0}
                    height={0}
                    style={{ width: 'auto', height: 'auto' }}
                  />
                )}
              </div>
            </div>
          </div>
          <div className="properties-setting-wrap">
            <div className="setting-tit">{getMessage('legend')}</div>
            <div className="module-table-box">
              <div className="module-table-inner">
                <div className="additional-color-wrap">
                  <div className="additional-color-box">
                    <span className="additional-color pink"></span>
                    <span className="normal-font">{getMessage('modal.panel.select.row')}</span>
                  </div>
                  <div className="additional-color-box">
                    <span className="additional-color white"></span>
                    <span className="normal-font">{getMessage('modal.panel.insert.row')}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div className="grid-btn-wrap">
            <button className="btn-frame modal act" onClick={handleApply}>
              {getMessage('modal.common.save')}
            </button>
          </div>
        </div>
      </div>
    </WithDraggable>
  )
}
