<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/estimate/fetch_unitprice.php");
$pdo = db_connect();

// 스크린 원단 가격
$price_screen = 0;

// 절곡판 단가 4종
$price_egi_1_2t = 0;  // EGI 1.15T, 1.2T
$price_egi_1_6t = 0;  // EGI 1.55T, 1.6T
$price_sus_1_2t = 0;
$price_sus_1_5t = 0;

// price_raw_materials 테이블에서 스크린 실리카 가격
$query = "SELECT itemList FROM {$DB}.price_raw_materials WHERE is_deleted IS NULL OR is_deleted = 0 ORDER BY num DESC LIMIT 1";
$stmt = $pdo->prepare($query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
    $itemList = json_decode($row['itemList'], true);
    $price_screen = slatPrice($itemList, '스크린', '실리카');
}

// BDmodels 테이블에서 절곡판 단가 4종 로딩
$query = "SELECT * FROM {$DB}.BDmodels WHERE seconditem = '절곡판' AND is_deleted IS NULL ORDER BY num DESC";
$stmt = $pdo->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $spec = $row['spec'] ?? '';
    $finishing = $row['finishing_type'] ?? '';
    $unitprice = intval(str_replace(',', '', $row['unitprice'] ?? 0));

    if (in_array($spec, ['1.15T', '1.2T']) && $finishing === 'EGI') $price_egi_1_2t = $unitprice;
    if (in_array($spec, ['1.55T', '1.6T']) && $finishing === 'EGI') $price_egi_1_6t = $unitprice;
    if ($spec === '1.2T' && $finishing === 'SUS') $price_sus_1_2t = $unitprice;
    if ($spec === '1.5T' && $finishing === 'SUS') $price_sus_1_5t = $unitprice;
}
?>

<div class="container mt-2">
    <h5 class="text-center mb-2">단가 수정 테이블 (스크린 / 절곡판)</h5>
    <div class="table-responsive">
        <table class="table table-bordered text-center align-middle table-sm">
            <thead class="table-secondary">
                <tr>
                    <th>항목명</th>
                    <th>단가 (원)</th>
                    <th>단가 적용</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>스크린 원단 (㎡)</td>
                    <td><input type="text" class="form-control text-end price-input" id="price_screen" value="<?= number_format($price_screen) ?>"></td>
                    <td><button type="button" class="btn btn-outline-primary btn-sm apply-price" data-target="price_screen">적용</button></td>
                </tr>
                <tr>
                    <td>EGI 1.15T / 1.2T</td>
                    <td><input type="text" class="form-control text-end price-input" id="price_egi_1_2t" value="<?= number_format($price_egi_1_2t) ?>"></td>
                    <td><button type="button" class="btn btn-outline-primary btn-sm apply-price" data-target="price_egi_1_2t">적용</button></td>
                </tr>
                <tr>
                    <td>EGI 1.55T / 1.6T</td>
                    <td><input type="text" class="form-control text-end price-input" id="price_egi_1_6t" value="<?= number_format($price_egi_1_6t) ?>"></td>
                    <td><button type="button" class="btn btn-outline-primary btn-sm apply-price" data-target="price_egi_1_6t">적용</button></td>
                </tr>
                <tr>
                    <td>SUS 1.2T</td>
                    <td><input type="text" class="form-control text-end price-input" id="price_sus_1_2t" value="<?= number_format($price_sus_1_2t) ?>"></td>
                    <td><button type="button" class="btn btn-outline-primary btn-sm apply-price" data-target="price_sus_1_2t">적용</button></td>
                </tr>
                <tr>
                    <td>SUS 1.5T</td>
                    <td><input type="text" class="form-control text-end price-input" id="price_sus_1_5t" value="<?= number_format($price_sus_1_5t) ?>"></td>
                    <td><button type="button" class="btn btn-outline-primary btn-sm apply-price" data-target="price_sus_1_5t">적용</button></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
$(document).ready(function () {
    $('.apply-price').on('click', function () {
        const targetId = $(this).data('target');
        const price = parseFloat($('#' + targetId).val().replace(/,/g, '')) || 0;

        $('.unit-price-input').each(function () {
            const $this = $(this);
            if ($this.data('target') === targetId) {
                const row = $this.closest('tr');
                const su = parseFloat(row.find('.su-input').val().replace(/,/g, '')) || 0;
                const total = price * su;
                $this.val(price.toLocaleString());
                row.find('.total-price').text(total.toLocaleString());
            }
        });
    });
});
</script>
