# -*- coding: utf-8 -*-
import sys; sys.path.insert(0, '.'); import wholesale_path
from wholesale import SooinSession
from bs4 import BeautifulSoup
import re

s = SooinSession()
s.login()
s.clear_cart()

result = s.search_products('코자정')
product = result['items'][0]
s.add_to_cart(product['internal_code'], qty=1, price=product['price'], stock=product['stock'])

resp = s.session.get(f'{s.BAG_VIEW_URL}?currVenCd={s.vendor_code}', timeout=15)
soup = BeautifulSoup(resp.content, 'html.parser')
form = soup.find('form', {'id': 'frmBag'})

form_data = {}
for inp in form.find_all('input'):
    name = inp.get('name', '')
    if not name: continue
    inp_type = inp.get('type', '').lower()
    if inp_type == 'checkbox':
        # 체크박스는 'on' 값으로 전송!
        form_data[name] = 'on'
    else:
        form_data[name] = inp.get('value', '')

form_data['kind'] = 'order'
form_data['x'] = '10'
form_data['y'] = '10'

print('체크박스 포함된 form_data:')
print(f"  chk_0: {form_data.get('chk_0')}")

resp = s.session.post(s.BAG_URL, data=form_data, timeout=30)
alert_match = re.search(r'alert\("([^"]*)"\)', resp.text)
alert_msg = alert_match.group(1) if alert_match else 'N/A'
print(f'alert 메시지: {alert_msg}')

# 장바구니 확인
resp2 = s.session.get(f'{s.BAG_VIEW_URL}?currVenCd={s.vendor_code}', timeout=15)
soup2 = BeautifulSoup(resp2.content, 'html.parser')
int_array = soup2.find('input', {'name': 'intArray'})
val = int_array.get('value') if int_array else '없음'
print(f'주문 후 intArray: {val}')

if val == '-1':
    print('\n🎉 주문 성공!')
else:
    print('\n❌ 주문 실패')
