<!-- Main content -->
<div class="content-wrapper">

    <!-- Inner content -->
    <div class="content-inner">

        <!-- card card-header -->
        <div class="card card-header mb-2">

            <div class="row border-bottom pb-2">
                <div class="col-xl-9 col-lg-9">
                    <h4 class="page-title fw-bold col-md-6 mb-0"><?= $translate["purchases_list"]; ?></h4>
                </div>
                <div class="col-xl-3 col-lg-3 col-md-6 btn-group">
                    <button class="btn btn-indigo text-uppercase btnAddPurchase" type="button"> <i class="ph-plus me-2"></i> <?= $translate["new_purchase"]; ?></button>
                </div>
            </div>

            <div class="row mt-3">

                <?php
                if ($_SESSION["user_profile"] == "ROOT") { ?>

                    <div class="col-lg-12 col-md-12 col-sm-12 mb-3">
                        <label class="form-label fw-medium text-uppercase"><?= $translate["enterprise"]; ?></label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="ph ph-building-office"></i></span>
                            <select class="form-control form-control-lg select2" id="ent_id_value" onchange="selectBranchByUserPermission(this.value);" data-width="1%">
                                <?php

                                $sql = "SELECT ent_id,ent_name FROM tb_enterprises WHERE ent_contract_status = 'Enabled' ORDER BY ent_name ASC;";
                                $answer = executePDOFetchAll($sql, "");

                                foreach ($answer as $key => $value) {
                                    echo "<option value='{$value['ent_id']}' > {$value['ent_name']}</option>";
                                }

                                ?>
                            </select>
                        </div>
                    </div>

                <?php } else { ?>
                    <input type="hidden" id="ent_id_value" value="<?= $_SESSION["user_ent_id"]; ?>">
                <?php }
                ?>

                <div class="col-lg-3 col-md-6 col-sm-12 mb-2">
                    <label class="form-label fw-medium text-uppercase"><?= $translate["branch"]; ?></label>
                    <div class="input-group">
                        <span class="input-group-text"><i class="ph ph-storefront"></i></span>
                        <select class="form-control form-control-lg select2" id="pos_id_value" onchange="listDataToDatatable();" data-width="1%">
                        </select>
                    </div>
                </div>

                <div class="col-xl-3 col-lg-4 col-md-6 col-sm-12 mb-2">
                    <label class="form-label fw-medium text-uppercase"><?= $translate["date_range"]; ?></label>
                    <div class="input-group">
                        <span class="input-group-text"><i class="ph-calendar"></i></span>
                        <input type="text" class="form-control form-control-lg daterange-predefined dateSelected" value="<?= date('Y-m-d', strtotime('-6 days')); ?> / <?= date('Y-m-d'); ?>" />
                    </div>
                </div>

                <div class="col-lg-3 col-md-6 col-sm-12 mb-3">
                    <label class="form-label fw-medium text-uppercase"><?= $translate["status"]; ?></label>
                    <div class="input-group">
                        <span class="input-group-text"><i class="ph ph-list-checks"></i></span>
                        <select class="form-control form-control-lg select2 searchStatus" onchange="listDataToDatatable();" data-width="1%">
                            <option value="Enabled" selected><?= $translate["active"]; ?></option>
                            <option value="Disabled"><?= $translate["canceled_f"]; ?></option>
                        </select>
                    </div>
                </div>

                <div class="col-lg-2 col-md-6 col-sm-12 mb-2">
                    <label class="form-label fw-medium text-uppercase">&nbsp;</label>
                    <div class="btn-group d-flex">
                        <button type="button" class="btn btn-warning text-uppercase btnSearch"><i class="ph-magnifying-glass me-1"></i> <?= $translate["search"]; ?></button>
                    </div>
                </div>

            </div>

        </div>
        <!-- /card card-header -->


        <!-- Content area -->
        <!-- <div class="content"> -->

        <!-- Card layout -->
        <div class="card card-header mt-1">

            <table class="table table-sm table-hover purchases-table">
                <thead>
                    <tr class="table-dark text-uppercase">
                        <th>#</th>
                        <th><?= $translate["branch"]; ?></th>
                        <th><?= $translate["date"]; ?> </th>
                        <th><?= $translate["provider"]; ?> </th>
                        <th><?= $translate["user"]; ?></th>
                        <th><?= $translate["total_purchase"]; ?></th>
                        <th><?= $translate["sale_estimate"]; ?></th>
                        <th><?= $translate["profit_estimate"]; ?></th>
                        <th><?= $translate["status"]; ?></th>
                        <th><?= $translate["options"]; ?></th>
                    </tr>
                </thead>
                <tfoot>
                    <tr class="table-dark text-uppercase fw-bold">
                        <td>__</td>
                        <td>__</td>
                        <td>__</td>
                        <td>__</td>
                        <td>__</td>
                        <td><span id="finalCost">0.00</span></td>
                        <td><span id="finalSale">0.00</span></td>
                        <td><span id="finalBenefit">0.00</span></td>
                        <td>__</td>
                        <td>__</td>
                </tfoot>
            </table>

        </div>
        <!-- /Card layout -->


        <!-- </div> -->
        <!-- /content area -->

    </div>
    <!-- /inner content -->

</div>
<!-- /main content -->


<!-- VIEW PURCHASE MODAL -->
<div id="viewPurchaseModal" class="modal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?= $translate["purchase_details"]; ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <input type="hidden" id="pur_print_id">

                <div class="row">

                    <div class="mb-1 text-uppercase fw-medium">
                        <div class="col-lg-12">
                            <div class="input-group">
                                <span class="form-control bg-light"><?= $translate["branch"]; ?></span>
                                <span class="form-control bg-light pos_description"></span>
                            </div>
                        </div>
                    </div>

                    <div class="mb-1 text-uppercase fw-medium">
                        <div class="col-lg-12">
                            <div class="input-group">
                                <span class="form-control bg-light"><?= $translate["date"]; ?></span>
                                <span class="form-control bg-light pur_span_date"><?= date("Y-m-d"); ?></span>
                            </div>
                        </div>
                    </div>

                    <div class="mb-1 text-uppercase fw-medium">
                        <div class="col-lg-12">
                            <div class="input-group">
                                <span class="form-control bg-light"><?= $translate["provider"]; ?></span>
                                <span class="form-control bg-light pur_span_provider"></span>
                            </div>
                        </div>
                    </div>


                    <div class="col-lg-12 col-md-12 col-sm-12 mt-1">
                        <div class="table-responsive">

                            <table class="table table-hover table-bordered">
                                <thead class="table-secondary">
                                    <tr class="text-uppercase fs-6">
                                        <th width="25%"><?= $translate["transport"]; ?></th>
                                        <th width="25%"><?= $translate["total_purchase"]; ?></th>
                                        <th width="25%"><?= $translate["sale_estimate"]; ?></th>
                                        <th width="25%"><?= $translate["profit_estimate"]; ?></th>
                                    </tr>
                                    <tr class="text-uppercase fs-6">
                                        <th width="25%"><span id="pur_span_transport">0.00</span></th>
                                        <th width="25%"><span id="pur_span_total">0.00</span></th>
                                        <th width="25%"><span id="pur_span_sale_estimate">0.00</span></th>
                                        <th width="25%"><span id="pur_span_profit_estimate">0.00</span></th>
                                    </tr>
                                </thead>
                            </table>

                        </div>
                    </div>

                    <div class="col-lg-12 col-md-12 col-sm-12 text-center text-uppercase fs-6 mt-3 mb-2">
                        <span><?= $translate["products_list"]; ?></span>
                    </div>


                    <div class="col-lg-12 col-md-12 col-sm-12" style="margin-top: -25px">
                        <div class="table-responsive">

                            <table class="table table-hover tbViewPurchaseDetails">
                                <thead class="table-primary">
                                    <tr class="table-dark text-uppercase">
                                        <th><?= $translate["product"]; ?></th>
                                        <th><?= $translate["quantity"]; ?></th>
                                        <th><?= $translate["total_cost"]; ?></th>
                                        <th><?= $translate["purchase_price"]; ?></th>
                                        <th><?= $translate["sale_price"]; ?></th>
                                    </tr>
                                </thead>
                            </table>

                        </div>
                    </div>

                </div>

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-info btnPrintPurchase" data-bs-dismiss="modal"><i class="ph-printer me-1"></i> <?= $translate["print"]; ?></button>
                <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="mi-close me-1"></i> <?= $translate["close"]; ?></button>
            </div>
        </div>
    </div>
</div>
<!-- VIEW PURCHASE MODAL -->


<!-- ADD PURCHASE MODAL -->
<div id="addEditPurchaseModal" class="modal" data-bs-keyboard="false" data-bs-backdrop="static" tabindex="-1">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title span-modal-title"></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <form action="" id="frmAddEditPurchaseData" autocomplete="off" method="POST">

                <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

                <div class="modal-body">

                    <div class="row">

                        <input type="hidden" name="pur_id" id="pur_id">
                        <input type="hidden" name="pur_ent_id" id="pur_ent_id">
                        <input type="hidden" name="pur_pos_id" id="pur_pos_id">
                        <input type="hidden" name="pur_cost" id="pur_cost">

                        <div class="mb-1 text-uppercase fw-medium notAllowed">
                            <div class="col-lg-12">
                                <div class="input-group">
                                    <span class="input-group-text form-control form-control-lg content-left"><?= $translate["branch"]; ?></span>
                                    <span class="input-group-text form-control form-control-lg pos_description content-right"></span>
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-12 col-md-12 col-sm-12 mt-1">
                            <div class="table-responsive">

                                <table class="table table-hover table-bordered">
                                    <thead class="table-secondary">
                                        <tr class="text-uppercase fs-6">
                                            <th><span><?= $translate["total_purchase"]; ?></span></th>
                                            <th><?= $translate["sale_estimate"]; ?></th>
                                            <th><?= $translate["profit_estimate"]; ?></th>
                                        </tr>
                                        <tr class="text-uppercase fs-6">
                                            <th><span id="spanTotalPurchase">0.00</span></th>
                                            <th><span id="spanTotalSale">0.00</span></th>
                                            <th><span id="spanTotalProfit">0.00</span></th>
                                        </tr>
                                    </thead>
                                </table>

                            </div>
                        </div>

                        <div class="col-lg-3 col-md-12 mb-2 mt-3">
                            <label class="form-label"><?= $translate["date"]; ?></label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="ph-calendar"></i></span>
                                <input type="text" class="form-control form-control-lg datepicker-date-format" name="pur_date" id="pur_date" value="<?= date('Y-m-d'); ?>">
                            </div>
                        </div>

                        <div class="col-lg-3 col-md-12 mb-2 mt-3">
                            <label class="form-label"><?= $translate["provider"]; ?></label>

                            <select class="form-control form-control-lg select2" name="pur_provider_id" id="pur_provider_id" data-minimum-results-for-search="Infinity">
                            </select>
                        </div>

                        <div class="col-lg-3 col-md-12 mb-2 mt-3">
                            <label class="form-label"><?= $translate["transport_cost"]; ?></label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="ph-currency-dollar"></i></span>
                                <input type="text" min="0" class="form-control form-control-lg" name="pur_shipping" id="pur_shipping" value="0.00">
                            </div>
                        </div>

                        <div class="col-lg-3 col-md-12 mb-2 mt-3">
                            <label class="form-label fw-medium text-uppercase">&nbsp;</label>
                            <div class="btn-group d-flex">
                                <button type="button" class="btn btn-info text-uppercase btnAddNewPurchase"><i class="ph-plus me-1"></i> <?= $translate["add"]; ?></button>
                            </div>
                        </div>

                        <div class="col-lg-12 col-md-12 col-sm-12 mt-2" style="margin-top: -25px">
                            <div class="table-responsive">

                                <table class="table table-sm table-hover tbAddEditPurchaseDetails">
                                    <thead>
                                        <tr class="table-dark text-uppercase">
                                            <th>x</th>
                                            <th><?= $translate["product"]; ?></th>
                                            <th><?= $translate["quantity"]; ?></th>
                                            <th><?= $translate["total_cost"]; ?></th>
                                            <th><?= $translate["purchase_price"]; ?></th>
                                            <th><?= $translate["sale_price"]; ?></th>
                                        </tr>
                                    </thead>
                                </table>

                            </div>
                        </div>

                    </div>

                </div>

            </form>

            <div class=" modal-footer">
                <button type="button" class="btn btn-indigo text-uppercase btnSave"><i class="ph-floppy-disk-back me-1"></i> <?= $translate["save"]; ?></button>
                <button type="button" class="btn btn-indigo text-uppercase btnEdit"><i class="ph-floppy-disk-back me-1"></i> <?= $translate["save"]; ?></button>
                <button type="button" class="btn btn-light text-uppercase" data-bs-dismiss="modal"><i class="mi-close me-1"></i> <?= $translate["close"]; ?></button>
            </div>
        </div>
    </div>
</div>
<!-- ADD PURCHASE MODAL -->

<!-- LIST PRODUCT MODAL -->
<div id="listProductModal" class="modal" data-bs-keyboard="false" data-bs-backdrop="static" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?= $translate["products_list"]; ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">

                <div class="table-responsive">

                    <table class="table table-hover tbProductPurchaseList">
                        <thead class="table-primary">
                            <tr class="table-dark text-uppercase">
                                <th><?= $translate["add"]; ?></th>
                                <th><?= $translate["product"]; ?></th>
                                <th><?= $translate["stock"]; ?></th>
                            </tr>
                        </thead>
                    </table>

                </div>

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="mi-close me-1"></i> <?= $translate["close"]; ?></button>
            </div>
        </div>
    </div>
</div>
<!-- LIST PRODUCT MODAL -->

<script src="scripts/purchases.js?v=<?= md5(time()) ?>"></script>