import { useMessage } from '@/hooks/useMessage'
import WithDraggable from '@/components/common/draggable/WithDraggable'
import QSelectBox from '@/components/common/select/QSelectBox'
import { useRoofAllocationSetting } from '@/hooks/roofcover/useRoofAllocationSetting'
import { usePopup } from '@/hooks/usePopup'
import { useRecoilValue } from 'recoil'
import { contextPopupPositionState } from '@/store/popupAtom'

export default function RoofAllocationSetting(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition } = props
  const { getMessage } = useMessage()
  const { closePopup } = usePopup()
  const { handleSave, onAddRoofMaterial, onDeleteRoofMaterial, values, roofMaterials, selectedRoofMaterial, setSelectedRoofMaterial } =
    useRoofAllocationSetting(id)

  return (
    <WithDraggable isShow={true} pos={pos}>
      <div className={`modal-pop-wrap ml mount`}>
        <div className="modal-head">
          <h1 className="title">{getMessage('plan.menu.estimate.roof.alloc')}</h1>
          <button className="modal-close" onClick={() => closePopup(id)}>
            닫기
          </button>
        </div>
        <div className="modal-body">
          <div className="properties-guide">{getMessage('modal.roof.alloc.info')}</div>
          <div className="allocation-select-wrap">
            <span>{getMessage('modal.roof.alloc.select.roof.material')}</span>
            <div className="grid-select">
              <QSelectBox options={roofMaterials} value={selectedRoofMaterial} onChange={(e) => setSelectedRoofMaterial(e)} />
            </div>
            <button
              className="allocation-edit"
              onClick={() => {
                onAddRoofMaterial()
              }}
            >
              <i className="edit-ico"></i>
              {getMessage('modal.common.add')}
            </button>
          </div>
          <div className="grid-option-wrap">
            {values.map((value, index) => (
              <div className="grid-option-box" key={index}>
                <div className="d-check-radio pop no-text">
                  <input type="radio" name="radio01" />
                  <label htmlFor="ra01"></label>
                </div>
                <div className="grid-option-block-form">
                  <div className="block-box">
                    <div className="flex-ment">
                      <div className="grid-select" style={{ width: '248px' }}>
                        <QSelectBox options={roofMaterials} value={value} />
                      </div>
                      {index === 0 && <span className="dec">基本屋根材</span>}
                      {index !== 0 && <button className="delete" onClick={() => onDeleteRoofMaterial(value.id)}></button>}
                    </div>
                  </div>
                  <div className="block-box">
                    {value.type === 'A' ? (
                      <>
                        <div className="flex-ment">
                          <span>W</span>
                          <div className="select-wrap" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>265</option>
                            </select>
                          </div>
                        </div>
                        <div className="flex-ment">
                          <span>L</span>
                          <div className="select-wrap" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>235</option>
                            </select>
                          </div>
                        </div>
                        <div className="flex-ment">
                          <span>{getMessage('modal.placement.initial.setting.rafter')}</span>
                          <div className="select-wrap" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>455</option>
                            </select>
                          </div>
                        </div>
                      </>
                    ) : value.type === 'B' ? (
                      <>
                        <div className="flex-ment">
                          <span>{getMessage('hajebichi')}</span>
                          <div className="grid-select no-flx" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>265</option>
                            </select>
                          </div>
                        </div>
                        <div className="flex-ment">
                          <span>{getMessage('modal.placement.initial.setting.rafter')}</span>
                          <div className="grid-select no-flx right" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>265</option>
                            </select>
                          </div>
                        </div>
                      </>
                    ) : value.type === 'C' ? (
                      <>
                        <div className="flex-ment">
                          <span>{getMessage('hajebichi')}</span>
                          <div className="grid-select no-flx" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>265</option>
                            </select>
                          </div>
                        </div>
                      </>
                    ) : value.type === 'D' ? (
                      <>
                        <div className="flex-ment">
                          <span>L</span>
                          <div className="grid-select no-flx" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>265</option>
                            </select>
                          </div>
                        </div>
                        <div className="flex-ment">
                          <span>{getMessage('modal.placement.initial.setting.rafter')}</span>
                          <div className="grid-select no-flx right" style={{ width: '84px' }}>
                            <select className="select-light dark" name="" id="">
                              <option>265</option>
                            </select>
                          </div>
                        </div>
                      </>
                    ) : (
                      ''
                    )}
                  </div>
                  <div className="block-box">
                    <div className="icon-btn-wrap">
                      <button className={value.alignType === 'parallel' ? 'act' : ''}>
                        {getMessage('modal.roof.alloc.select.parallel')}
                        <i className="allocation01"></i>
                      </button>
                      <button className={value.alignType === 'stairs' ? 'act' : ''}>
                        {getMessage('modal.roof.alloc.select.stairs')} <i className="allocation02"></i>
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            ))}
          </div>
          <div className="grid-btn-wrap">
            <button className="btn-frame modal act" onClick={handleSave}>
              {getMessage('modal.roof.alloc.apply')}
            </button>
          </div>
        </div>
      </div>
    </WithDraggable>
  )
}
