# -*- coding: utf-8 -*-
"""실제 POST 데이터 확인"""
import sys; sys.path.insert(0, '.'); import wholesale_path
from bs4 import BeautifulSoup
import re

import importlib
import wholesale.sooin
importlib.reload(wholesale.sooin)
from wholesale import SooinSession

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

# 2개 품목 담기
r1 = s.search_products('코자정')
s.add_to_cart(r1['items'][0]['internal_code'], qty=1, price=r1['items'][0]['price'], stock=r1['items'][0]['stock'])
r2 = s.search_products('디카맥스')
s.add_to_cart(r2['items'][0]['internal_code'], qty=1, price=r2['items'][0]['price'], stock=r2['items'][0]['stock'])

# row 0 취소 (디카맥스)
s.cancel_item(row_index=0)

# Bag.asp GET
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', 'text').lower()
    
    if inp_type == 'checkbox':
        if inp.get('checked') is not None:
            form_data[name] = 'on'
        continue
    
    form_data[name] = inp.get('value', '')

form_data['kind'] = 'order'
form_data['tx_memo'] = '선별 테스트'

print('=== POST할 데이터 (체크박스 관련) ===')
for k, v in form_data.items():
    if 'chk' in k.lower():
        print(f"  {k}: {v}")

print(f"\n=== 실제 POST ===")
resp = s.session.post(
    s.ORDER_END_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_msg}")

# 장바구니 확인
cart = s.get_cart()
print(f"\n남은 품목: {cart['total_items']}개")
for item in cart['items']:
    print(f"  - {item['product_name']}")
