<?php
$supplyer_data = $db->selectAll('suppliers', 'where isActive=1 order by name asc', 'id,name');
$units = $db->selectAll('unit', 'where isActive=1 order by title asc');
$general->arrayIndexChange($units, 'id');
$product_data = $db->getProductData();
$all_truk = $smt->get_all_truk();
$all_additional_expense = $smt->get_all_additional_expense();
$pay_by = [
    1=>['id' => 1, 'name' => 'Supplier'],
    2=>['id' => 2, 'name' => 'Self']
];
?>
<script>
    const productData = <?= json_encode($product_data) ?>;
    const all_truk = <?= json_encode($all_truk) ?>;
    const all_additional_expense = <?= json_encode($all_additional_expense) ?>;
    const all_pay_by = <?= json_encode($pay_by) ?>;
</script>
<?php
if (isset($_GET['edit'])) {
    include(__DIR__ . "/update.php");
} else {
    $draftID = 0;
    $supInvNo = '';
    $purType = '';
    $discount = '';
    $mrr_code = '';
    $op_no = '';
    $purDate = date('d-m-Y');
    if (isset($_GET['draftID'])) {
        $draftID = intval($_GET['draftID']);
        $draft = $db->get_rowData('purchase_draft', 'purID', $draftID);
        //$general->printArray($draft);
        if (!empty($draft)) {
            if ($draft['supID'] == $supID) {
                $drData = $general->getJsonFromString($draft['sData']);
                //$general->printArray($drData);
                if (isset($drData['supInvNo'])) {
                    $supInvNo = $drData['supInvNo'];
                }
                if (isset($drData['purType'])) {
                    $purType = $drData['purType'];
                }
                if (isset($drData['purDate'])) {
                    $purDate = $drData['purDate'];
                }
                if (isset($drData['mrr_code'])) {
                    $mrr_code = $drData['mrr_code'];
                }
                if (isset($drData['op_no'])) {
                    $op_no = $drData['op_no'];
                }
                if (isset($drData['discount'])) {
                    $discount = $drData['discount'];
                }
            } else {
                $draftID = 0;
            }
        } else {
            $draftID = 0;
        }
        ?>
        <script type="text/javascript">
            $(document).ready(function () {
                purchaseSubTotal();
            });
        </script>
        <?php
    }
    $general->pageHeader($rModule['title'], [$pUrl => $rModule['title'], 1 => 'New']);

    $products = [];

    $products = $db->selectAll('products', 'where isActive=1', 'id as pID, title,code');
    if (!empty($products)) {
        foreach ($products as $k => $p) {
            $products[$k]['title'] = $p['code'] . ' ' . $p['title'];
        }
    }
    ?>
    <input type="hidden" id="draftID" value="<?php echo $draftID; ?>">
    <div class="row">
        <div class="col-sm-12">
            <div class="white-box border-box">
                <div><?php show_msg(); ?></div>
                <hr>
                <div class="row">
                    <div class="col-xs-6 col-md-4 col-sm-6">
                        <?php
                            $general->inputBoxSelect($supplyer_data, 'Supplier Name', 'supplier_id', 'id', 'name');
                            $general->inputBoxText('challan-no', 'Challan No');
                            $general->inputBoxSelect($pay_by, 'Truck rent pay by', 'truk_rent_pay_by', 'id', 'name');
                        ?>
                    </div>
                    <div class="col-xs-6 col-md-4 col-sm-6">
                        <?php
                        $general->inputBoxText('supplier-no', 'Bill No', $supInvNo);
                        $general->inputBoxText('challan-date', 'Challan Date', $purDate, '', 'daterangepicker');
                        ?>
                    </div>
                    <div class="col-xs-6 col-md-4 col-sm-6">
                        <?php
                        $general->inputBoxText('bill-date', 'Bill Date', $purDate, '', 'daterangepicker');
                        $general->inputBoxText('po-no', 'PO No', $op_no);
                        ?>
                    </div>
                </div>
                <hr>
                <div class="row">
                    <div class="col-md-6">
                        <?php
                            $general->inputBoxSelect($all_truk, 'Truk', 'truk_id', 'id', 'title');
                        ?>
                        <?php
                            $general->inputBoxText('truk_rent', 'Rent');
                        ?>
                        <button onclick="operation_truk_add_to_cart()" class="btn btn-info">Add Truk</button>
                    </div>
                    <div class="col-md-6">
                        <table class="table table-border">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Truk</th>
                                    <th class="amount_td">Rent</th>
                                    <th class="amount_td">Remove</th>
                                </tr>
                            </thead>
                            <tbody id="truk_list"></tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="2" class="amount_td"><b>Total</b></td>
                                    <td class="amount_td"><b id="truk_total">0.00</b></td>
                                    <td>&nbsp;</td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
                <hr>
                <div class="row">
                    <div class="col-md-3">
                        <?php
                        $general->inputBoxSelect($all_truk, 'Truk', 'ade_truk_id', 'id', 'title');
                        ?>
                    </div>
                    <div class="col-md-2">
                        <?php
                        $general->inputBoxSelect($all_additional_expense, 'Expense', 'ade_id', 'id', 'title');
                        ?>
                    </div>
                    <div class="col-md-2">
                        <?php
                        $general->inputBoxSelect($pay_by, 'Pay by', 'ade_pay_by', 'id', 'name');
                        ?>
                    </div>
                    <div class="col-md-2">
                        <?php
                        $general->inputBoxText('ade_amount', 'Amount');
                        ?>
                    </div>
                    <div class="col-md-3">
                        <button onclick="operation_adc_add_to_cart()" class="btn btn-info">Add Additional cost</button>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <table class="table table-border">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Truk</th>
                                    <th>Expense</th>
                                    <th>Pay by</th>
                                    <th class="amount_td">Amount</th>
                                    <th class="amount_td">Remove</th>
                                </tr>
                            </thead>
                            <tbody id="ade_list">

                            </tbody>
                            <tfoot></tfoot>
                            <tr>
                                <td colspan="4" class="amount_td"><b>Total</b></td>
                                <td class="amount_td"><b id="ade_total">0.00</b></td>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                    </div>
                </div>

                <hr>
                <div class="row">
                    <div class="col-xs-6 col-md-4 col-sm-6">
                        <?php
                        $general->inputBoxSelect($product_data, 'Item', 'product_id', 'id', 't', script: 'onchange="purchaseProductChange(this.value)"');
                        ?>

                    </div>
                    <div class="col-xs-6 col-md-4 col-sm-3">
                        <div class="form-group row mb-1">
                            <label for="qty" class="col-md-4 col-form-label">Quantity</label>
                            <div class="col-md-8">
                                <div class="input-group qty-input-group">
                                    <input class="form-control amount_td" value="" placeholder="Quantity" id="qty"
                                        type="text">
                                    <div class="input-group-append">
                                        <span class="input-group-text" id="qtyLabel">--</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-6 col-md-4 col-sm-6">
                        <?php
                        $general->inputBoxText('unitPrice', 'Unit Price', '', '', 'amount_td');
                        $general->inputBoxText('total', 'Total', '', '', 'amount_td', 'readonly');
                        ?>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <button onclick="purchaseAddToCart()"
                            class="m-2 btn btn-info waves-effect waves-light pull-right m-t-10">Add to cart</button>
                    </div>
                </div>
                <div class="row">
                    <div style="display: none;">
                        <table>
                            <tbody>
                                <tr id="purchaseProductsTr">
                                    <td class="autoSerial"></td>
                                    <td><input type="hidden" class="pID" value=""><span class="pTitle"></span></td>
                                    <?php
                                    if ($use_product_category == 1) {
                                        ?>
                                        <td class="category"></td>
                                        <td><input type="hidden" class="categoryID" value=""><span class="subCategory"></span>
                                        </td>
                                        <?php
                                    }
                                    ?>
                                    <td class="unTitle"></td>
                                    <td class="unitPrice amount_td"></td>
                                    <td class="salePrice amount_td"></td>
                                    <td class="qty amount_td"></td>
                                    <td class="total amount_td"></td>
                                    <td class="amount_td"><button class="btn btn-danger remove">X</button></td>
                                </tr>
                                <tr id="truk_tr">
                                    <td class="autoSerial"></td>
                                    <td><input type="hidden" class="truk_id" value=""><span class="truk_title"></span></td>
                                    <td class="truk_rent amount_td"></td>
                                    <td class="amount_td"><button class="btn btn-danger remove">X</button></td>
                                </tr>
                                <tr id="adc_tr">
                                    <td class="autoSerial"></td>
                                    <td><input type="hidden" class="truk_id" value=""><span class="truk_title"></span></td>
                                    <td><input type="hidden" class="ade_id" value=""><span class="ade_title"></span></td>
                                    <td><input type="hidden" class="pay_by" value=""><span class="pay_title"></span></td>
                                    <td class="amount amount_td"></td>
                                    <td class="amount_td"><button class="btn btn-danger remove">X</button></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-md-12">
                        <table class="table table-border">
                            <thead>
                                <tr>
                                    <th style="width: 5%;">#</th>
                                    <th>Product</th>
                                    <th style="width: 10%;">Unit</th>
                                    <th style="width: 10%;" class="amount_td">Unit price</th>
                                    <th style="width: 10%;" class="amount_td">Sale Price</th>
                                    <th style="width: 10%;" class="amount_td">Qty</th>
                                    <th style="width: 10%;" class="amount_td">Total</th>
                                    <th style="width: 5%;" class="amount_td">X</th>
                                </tr>
                                <thead>
                                <tbody id="purchaseProducts">
                                    <?php
                                    $serial = 1;
                                    if (isset($drData['products'])) {
                                        foreach ($drData['products'] as $pID => $pr) {
                                            $p = $productData[$pID];
                                            $id = 'pd_' . $pID . '_' . $serial;
                                            ?>
                                            <tr id="<?php echo $id ?>">
                                                <td class="autoSerial"><?php echo $serial++ ?></td>
                                                <td>
                                                    <input type="hidden" class="pID" value="<?php echo $pID ?>">
                                                    <span class="pTitle"><?php echo $p['t'] ?></span>
                                                </td>
                                                <td class="unTitle"><?php echo $p['u'] ?></td>
                                                <td class="unitPrice amount_td"><?php echo $pr['up'] ?></td>
                                                <td class="salePrice amount_td"><?php echo $pr['sp'] ?></td>
                                                <td class="qty amount_td"><?php echo $pr['q'] ?></td>
                                                <td class="total amount_td"><?php echo (float) ($pr['up'] * $pr['q']) ?></td>
                                                <td class="amount_td"><button class="btn btn-danger remove"
                                                        onclick="remove_row_by_id('<?php echo $id ?>');purchaseSubTotal()">X</button>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                    ?>
                                </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="6" class="amount_td"><b>Sub Total</b></td>
                                    <td class="amount_td"><b id="subTotal">0.00</b></td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="6" class="amount_td"><b>Discount</b></td>
                                    <td class="amount_td"><input type="text" class="form-control amount_td" value=""
                                            id="discount"></td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="6" class="amount_td"><b>VAT</b></td>
                                    <td class="amount_td"><input type="text" class="form-control amount_td" value=""
                                            id="VAT"></td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="6" class="amount_td"><b>AIT</b></td>
                                    <td class="amount_td"><input type="text" class="form-control amount_td" value=""
                                            id="AIT"></td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="6" class="amount_td"><b>Net Payable</b></td>
                                    <td class="amount_td"><b id="netPayable">0.00</b></td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="6" class="amount_td"><b>Note</b></td>
                                    <td class="amount_td"><textarea type="text" class="form-control" value=""
                                            id="note"></textarea></td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="9" class="amount_td">
                                        <?php
                                        if ($db->permission(4)) {
                                            ?>
                                            <button onclick="purchaseAdd()"
                                                class="m-1 p-1 btn btn-lg btn-success pull-right purchaseAdd">Purchase</button>
                                            <?php
                                        }
                                        ?>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>

                </div>
            </div>
        </div>
        <?php
}