<?php
if (
    !isset($permissions['finance']['invoices'])
    || isset($permissions['finance']['invoices'])
    && $permissions['finance']['invoices'] == 0
) redirect('admin/dashboard');

?>
<script src="https://cdn.lordicon.com/libs/mssddfmo/lord-icon-2.1.0.js"></script>
<div id="main p-0" role="main" class="order_page">
    <!-- <div class="header-bg shadow-sm p-0" style="background: url(https://images.pexels.com/photos/1475033/pexels-photo-1475033.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)"> -->
    <!-- <div class="header-bg shadow-lg p-0" style="background: url(https://images.pexels.com/photos/1487834/pexels-photo-1487834.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)"></div> -->
    <div id="content" style=" padding-top: 0px; ">
        <div class="container">
            <div class="row mt-3 justify-content-center">
                <div class="header_invoice_wrapper shadow">
                    <div class="fs-1 w-25 text-white d-flex align-items-center"><?php echo $this->lang->line('orders'); ?></div class="fs-1">
                    <div class="new_invoice_btn">
                        <button type="button" class="shadow-lg add_invoice_button" data-bs-toggle="modal" data-bs-target="#myModal" title="<?php echo $this->lang->line('new_order'); ?>">
                            <lord-icon src="https://cdn.lordicon.com/mecwbjnp.json" trigger="loop" delay="1000" colors="primary:#356359,secondary:#00bcd4" style="width:32px;height:32px">
                            </lord-icon>
                            <span class="pt-1 text-dark">
                                <?php echo $this->lang->line('new_order'); ?>
                            </span>
                        </button>
                        <!-- <button type="button" type="button" id="status_form" class="shadow-lg excel_exporter p-2" style="width: 50px;" data-bs-toggle="collapse" href="#date_search" role="button" aria-expanded="false" rel-order="-1" aria-controls="date_search"><?php echo $this->lang->line('status'); ?></button> -->
                        <button title="prepair to exit" type="button" class="shadow-lg excel_exporter p-2" style="width: 50px;" id="out_order" rel-order="-1">
                            <i class="fa fa-truck"></i>
                        </button>
                        <button title="destroy" type="button" class="shadow-lg excel_exporter p-2" style="width: 50px;" id="destroy_table" rel-order="-1" rel-action="2" rel-close="-1">
                            <i class="fas fa-sync"></i> </button>
                        <button type="button" class="filed_effect shadow-lg excel_exporter px-3" data-bs-toggle="modal" data-bs-target="#ExcelExportModal">
                            <i class="fas fa-file-excel text-success"></i>
                        </button>
                        <button type="button" class="shadow-lg excel_exporter p-2" style="width: 50px;" id="" rel-order="-1" data-bs-target="">
                            <i class="fas fa-file-pdf text-warning"></i>
                        </button>
                        <button type="button" class=" filed_effect shadow-lg excel_exporter  p-2" style="width: 50px;" data-bs-toggle="modal" id="" rel-order="-1" data-bs-target="">
                            <i class="fas fa-copy text-primary"></i>
                        </button>
                        <button title="edit" type="button" class=" filed_effect shadow-lg excel_exporter update_order p-2" style="width: 50px;" id="updateOrderButton" rel-order="-1" data-bs-target="#updateOrder">
                            <i class="fas fa-edit"></i>
                        </button>
                        <!-- <button title="view" type="button" class=" filed_effect shadow-lg excel_exporter view_order p-2" style="width: 50px;" id="viewInvoiceButton">
                            <i class="pt-1 me-1 fas fa-eye text-success"></i>
                        </button> -->
                        <button type="button" class=" shadow-lg excel_exporter cancel_order2 p-2" style="width: 50px;">
                            <i class="pt-1 me-1 fas fa-lock text-danger"></i>
                        </button>
                    </div>
                </div>
                <div class="collapse w-100" id="date_search" style="margin-left: 242px;">
                    <div class>
                        <div class="card p-3 w-25" style="border-radius: 0 0 20px 20px; float: left;">
                            <div>
                                <div>
                                    <select id="indoor_status" name="indoor_status">
                                        <?php foreach ($status as  $status) { ?>
                                            <option value="<?php echo $status->id; ?>"><?php echo $status->text; ?></option>
                                        <?php  } ?>
                                    </select>
                                </div>
                            </div>
                            <div>
                                <button type="submit" id="confirmOrderButton" class="btn btn-success p-2 mt-1">
                                    <?php echo $this->lang->line('change'); ?>
                                </button>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="row">
                    <hr class="bg-white mt-5" style="border-radius: 30px; padding: 2px;" />
                    <div class=" table-responsive card card-body">
                        <table class="table table-sm" id="dt_basic2">
                            <thead>
                                <th></th>
                                <th><?php echo $this->lang->line('order_num'); ?></th>
                                <th><?php echo $this->lang->line('customer'); ?></th>
                                <th><?php echo $this->lang->line('sum'); ?></th>
                                <th><?php echo $this->lang->line('date'); ?></th>
                                <th><?php echo $this->lang->line('close'); ?></th>
                                <th><?php echo $this->lang->line('deliver'); ?></th>
                                <th><?php echo $this->lang->line('status'); ?></th>
                            </thead>
                            <tbody>
                                <?php
                                $close = 0;
                                foreach ($orders as $order) { ?>
                                    <?php if ($order->is_closed == 0) {
                                        $close = 0;
                                    } else {
                                        $close = 1;
                                    } ?>
                                    <tr rel-order="<?php echo $order->id; ?>" rel-close="<?php echo $close; ?>" rel-status="<?php echo $order->status; ?>">
                                        <td>
                                            <div class="form-check">
                                                <input class="form-check-input order_check" type="checkbox" value="" id="orderCheck">
                                            </div>
                                        </td>
                                        <td><?php echo $order->id; ?></td>
                                        <td>
                                            <?php echo $order->customerName; ?>
                                        </td>
                                        <td><?php echo $order->total; ?></td>
                                        <td><?php echo date('d/m/Y H:i:s', strtotime($order->dateCreation)); ?></td>
                                        <td><?php if ($order->is_closed == 0) { ?>
                                                <span class="badge  bg-primary p-1 bg-info "><?php echo $this->lang->line('no'); ?></span>
                                            <?php } else { ?>
                                                <span class="badge  bg-danger p-1 bg-info "><?php echo $this->lang->line('yes'); ?></span>
                                            <?php   } ?>
                                        </td>
                                        <td>
                                            <?php if ($order->status == 4) { ?>
                                                <span class="badge  bg-primary p-1 bg-success  "> <?php echo $this->lang->line('full'); ?> </span>
                                            <?php  } else if ($order->status == 3) { ?>
                                                <span class="badge  bg-primary p-1 bg-warning  "> <?php echo $this->lang->line('partial'); ?></span>
                                            <?php } else { ?>

                                            <?php } ?>
                                        </td>

                                        <td class="status_td">
                                            <div class="popup">
                                                <?php if ($order->status == 1) { ?>
                                                    <div class="label bg-primary  p-1" rel-action="<?php $order->status ?>"><?php echo $this->lang->line('new'); ?></div>
                                                <?php } else { ?>
                                                    <div class="label bg-success p-1" rel-action="<?php $order->status ?>"><?php echo $this->lang->line('Confirmed'); ?></div>
                                                <?php  }  ?>
                                                <div class="menu">
                                                    <?php foreach ($statues as $current_status) { ?>
                                                        <div class="menu-item" rel-action="<?php echo $current_status->id; ?>"><?php echo $current_status->text; ?></div>
                                                    <?php }; ?>
                                                </div>
                                            </div>
                                        </td>
                                        <!-- <td>
                                                <button type="button" class="btn btn-info btn-circle sendOrder" id="out_order" rel-action="<?php echo $order->id; ?>"> <i class="fa fa-truck"></i></button>
                                            </td> -->
                                        <!-- <td>
                                                <div class="" role="group" aria-label="Basic example">
                                                    <a href="/admin/orders/view/<?php echo $order->id; ?>" target="_blank" title="<?php echo $this->lang->line('orders'); ?>" class="btn btn-danger btn-circle"><i class="fa fa-eye"></i></a>
                                                    <button type="button" class="btn btn-info btn-circle editOrder" rel-action="<?php echo $order->id; ?>"> <i class="fa fa-edit"></i> </button>
                                                </div>
                                            </td> -->
                                    </tr>
                                <?php }; ?>
                            </tbody>
                            <tfoot>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal" rel-modaltype="mymodal" id="myModal" role="dialog">
    <div class="modal-dialog modal-dialog-centered ">
        <div class="modal-content position-relative">
            <div class="close_modal close_modal_bottom" id="close_modal" rel-modaltype="mymodal">
                <i class="fas fa-times-circle text-danger"></i>
            </div>
            <div class="modal-body" style="background-color: #f5f5f5;">
                <div id="content">
                    <form action="<?php echo site_url(); ?>admin/orders/action" method="POST" class="createOrder" autocomplete="OFF">
                        <div class="first-stage position-relative" id="page_one">
                            <div class="form-header">
                                <div class="invoice-head">
                                    <label for=""><?php echo $this->lang->line('order'); ?></label>
                                    <input class="form-control" value="<?php echo getNewOrderNo() ?>" name="order_code" readonly />
                                </div>

                                <div class="date_wrapper d-flex flex-column justify-content-end align-items-end">
                                    <div class="invoice-head mb-2" style="width: 70%;">
                                        <label for=""><?php echo $this->lang->line('date'); ?></label>
                                        <input type="text" value="<?php $now = date('Y-m-d');
                                                                    echo date("d/m/Y", strtotime($now)); ?>" name="order_date" id="order_date" class=" form-control" readonly />
                                    </div>
                                    <div class="invoice-head" style="width: 70%;">
                                        <label for=""><?php echo $this->lang->line('delivery_date'); ?></label>
                                        <input type="text" value="<?php $now = date('Y-m-d');
                                                                    echo date("d/m/Y", strtotime($now)); ?>" name="delivery_date" id="delivery_date" class=" form-control" />
                                    </div>
                                </div>
                            </div>
                            <div class="">
                                <div class="form-full">
                                    <label for=""><?php echo $this->lang->line('customer'); ?></label>
                                    <div class="selectorHolder">
                                        <select class="order_for select2" id="order_for" name="order_for">
                                            <option value="">נא בחר</option>
                                            <?php foreach ($customers as  $customer) { ?>
                                                <option value="<?php echo $customer->customerId; ?>"><?php echo $customer->customerName; ?></option>
                                            <?php  } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-full">
                                    <div class="invoice-custom_75 d-flex" style="width: 80%;">
                                        <label for="" class="" style="width: 20.3%;">
                                            <div style="max-width: 20px;">
                                                <?php echo $this->lang->line('manger_note'); ?>

                                            </div>
                                        </label>
                                        <input id="fill_boss_Comment1" class="form-control w-75" name="boss_Comment">
                                    </div>
                                    <div class="invoice-custom_25 d-flex" style="width: 30%">
                                        <label class="w-50">יתרת הלקוח</label>
                                        <input id="customer_credit" type="text" class="form-control w-50 p-1 text-center" value="" name="credit" style="font-size:8px; color: tomato !important; font-weight: bold !important;" />
                                    </div>
                                </div>
                                <div class="form-full">
                                    <label for=""><?php echo $this->lang->line('details'); ?></label>
                                    <input class="form-control fill_cust_comment" name="custComment" id="custComment">
                                </div>
                                <div class="form-full">
                                    <label for=""><?php echo $this->lang->line('warehouse'); ?></label>
                                    <div class="selectorHolder">
                                        <select name="warehouse" class="select2" id="storage">
                                            <?php foreach ($warehouse as  $house) { ?>
                                                <option value="<?php echo $house->ID; ?>"><?php echo $house->Name; ?></option>
                                            <?php  } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-full">
                                    <label for=""><?php echo $this->lang->line('quantity_type'); ?></label>
                                    <div class="selectorHolder">
                                        <select name="quantity_type" class="form-control" id="quantity_type">
                                            <option value="cartoon_quantity"><?php echo $this->lang->line('cartoon'); ?></option>
                                            <option value="unit"><?php echo $this->lang->line('piece'); ?></option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-full" style="margin-top: 50px;">
                                    <label for=""><?php echo $this->lang->line('notes'); ?></label>
                                    <textarea class="form-control" style="width: 85%; height: 150px;" placeholder="<?php echo $this->lang->line('Enter_your_comment'); ?>" id="comment1" name="comment1"></textarea>
                                </div>
                            </div>
                            <div class="row w-100 d-flex justify-content-end m-0">
                                <button type="button" class="btn btn-success rounded float-left p-1 next-stage" style="width: 100px;">
                                    <?php echo $this->lang->line('confirm'); ?>
                                </button>
                            </div>
                        </div>
                        <page size="A4" style="display: none;">
                            <div class="second_stage_header row align-items-start">
                                <div class="right-section w-50">
                                    <div class="w-50 m-2">
                                        <label for="" class="me-1"><?php echo $this->lang->line('order'); ?>: </label>
                                        <label for="" class="fw-bold"><?php echo getNewOrderNo() ?></label>
                                    </div>
                                    <div class="w-50 m-2">
                                        <label for="" class="me-1" name="customer" id="customer"><?php echo $this->lang->line('customer'); ?>: </label>
                                        <label for=""></label>
                                    </div>
                                    <div class="form-full w-50 m-2">
                                        <label for="" class="me-1"><?php echo $this->lang->line('Enter_your_comment'); ?>: </label>
                                        <input class="form-control" name="cust_comment" id="cust_comment" readonly />
                                    </div>
                                </div>
                                <div class="left-section w-50">
                                    <div class="invoice-head w-75 m-2">
                                        <label for="" class="me-1 w-100"><?php echo $this->lang->line('date'); ?>: </label>
                                        <input class="form-control" value="" name="order_date1" id="order_date1" readonly />
                                    </div>
                                    <div class="invoice-head w-75 m-2">
                                        <label for="" class="me-1 w-100"><?php echo $this->lang->line('delivery_date'); ?>: </label>
                                        <input class="form-control" value="" name="delivery_date1" id="delivery_date1" readonly />
                                    </div>
                                    <div class=" w-75 m-2">

                                        <button class="btn btn-primary " id="open_catalog"><?php echo $this->lang->line('products_categories'); ?></button>
                                    </div>
                                </div>

                            </div>
                            <div class=" row container-lg mx-auto p-0">
                                            <div class="col col-lg-12 col-md-12 col-sm-12" id="invoice_details">
                                                <table class="table" id="invoice_table">
                                                    <thead>
                                                        <tr>
                                                            <th colspan="3"><?php echo $this->lang->line('BARCODE'); ?></th>
                                                            <th><?php echo $this->lang->line('description'); ?></th>
                                                            <th><?php echo $this->lang->line('unit'); ?></th>
                                                            <th><?php echo $this->lang->line('unit_price'); ?></th>
                                                            <th><?php echo $this->lang->line('sum'); ?></th>
                                                            <th><?php echo $this->lang->line('in_stock'); ?></th>
                                                            <th><?php echo $this->lang->line('actions'); ?></th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <tr class="items">
                                                            <td colspan="3">

                                                                <select name="items[product_id][]" class="select2Custom">
                                                                    <optgroup class="products_list" label="products" name="products">
                                                                        <option value="">נא בחר</option>
                                                                        <?php foreach ($products as $product) { ?>
                                                                            <option value="<?php echo $product->id ?>"><?php echo $product->code ?></option>
                                                                        <?php }; ?>
                                                                    </optgroup>
                                                                </select>
                                                                <input type="hidden" name="items[code][]" rel-end="0">
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" name="items[description][]" class="item_desc form-control text-center" readonly="readonly" rel-end="0" />
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" name="items[unit][]" class="item_unit form-control text-center" rel-end="0" />
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" name="items[price][]" class="item_price form-control text-center" rel-end="0" />
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" name="items[full_price][]" class="full_price form-control text-center" readonly="readonly" style="width: 100%;" rel-end="0" />
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" name="items[in_stock][]" class="in_stock form-control text-center" style="width: 100%;" rel-end="1" />
                                                            </td>
                                                            <td>
                                                                <div href="#0" class="remove_this_item btn btn-sm bg-danger text-white">
                                                                    <i class="fa fa-times"></i>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>

                                            <div class="final_info">
                                                <div class="form-full">
                                                    <textarea class="form-control w-75" style="height: 100px" placeholder="<?php echo $this->lang->line('notes'); ?>" id="fill_comment2" name="comment2"></textarea>
                                                </div>
                                                <div class="final_number">
                                                    <div class="form-full mt-1" style="margin: 0px;">
                                                        <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_before_vat'); ?></label>
                                                        <input type="text" value="0" name="total_before_vat" class="fill_total_before_vat form-control w-50 text-center p-0" readonly />
                                                    </div>
                                                    <div class="form-full mt-1" style="margin: 0px;">
                                                        <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('vat_17'); ?></label>
                                                        <input type="text" value="0" name="vat" class="form-control w-50 fill_vat text-center p-0" readonly />
                                                    </div>
                                                    <div class="form-full mt-1" style="margin: 0px;">
                                                        <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_shekel'); ?></label>
                                                        <input type="text" value="0" name="total" class="fill_total form-control w-50 text-center p-0" readonly />
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col col-lg-12 col-lg-12 col-sm-12">
                                                <div class="row justify-content-between">
                                                    <div class="col-lg-3 d-flex align-items-center">
                                                        <div class="invoice-custom_25 d-flex">
                                                            <label class="w-50">יתרת הלקוח</label>
                                                            <input type="text" class="form-control w-50 p-1 text-center" value="" name="credit1" id="credit1">
                                                        </div>
                                                    </div>
                                                    <div class="align-items-center col-lg-5 d-flex justify-content-end" style="margin-top: 10px;">
                                                        <button type="button" class="reverse-stage btn btn-primary p-1"><i class="fas fa-angle-right me-1"></i>חזור</button>
                                                        <a href="#0" class="btn btn-danger close_modal_bottom" rel-modaltype="mymodal" style=" margin: 0 10px !important;padding: 4px 20px !important;">
                                                            <?php echo $this->lang->line('close'); ?>
                                                        </a>
                                                        <a href="#0" class="save_order btn btn-success p-1 mr-1 ">
                                                            <?php echo $this->lang->line('save_order'); ?>
                                                        </a>
                                                    </div>
                                                </div>
                                            </div>
                                    </div>
                        </page>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal" rel-modaltype="delivery" id="deliveryModal" role="dialog">
    <div class="modal-dialog modal-lg modal-dialog-centered ">
        <div class="modal-content position-relative">
            <div class="close_modal close_delivery_modal close_modal_bottom" rel-modaltype="delivery" id="close_modal">
                <i class="fas fa-times-circle text-danger"></i>
            </div>

            <div class="modal-body" style="background-color: #f5f5f5;">
                <div id="content">
                    <page class="check_items" size="A4" id="check_items">
                        <div class="second_stage_header row align-items-start">
                            <div class="right-section w-50">
                                <div class="w-50 m-2">
                                    <label for="" class="me-1"><?php echo $this->lang->line('order'); ?>: </label>
                                    <label for="" id="out_order_id" class="fw-bold"></label>
                                </div>
                                <div class="w-50 m-2">
                                    <label for="" class="me-1" name="customer"><?php echo $this->lang->line('customer'); ?>: </label>
                                    <label id="out_customer" for=""></label>
                                </div>
                            </div>
                            <div class="left-section w-50">
                                <div class="invoice-head w-50 m-2">
                                    <label for="" class="me-1"><?php echo $this->lang->line('date'); ?>: </label>
                                    <input type="text" value="<?php $now = date('Y-m-d');
                                                                echo date("d/m/Y", strtotime($now)); ?>" name="order_date" id="out_order_date" class=" form-control" readonly />
                                </div>
                            </div>
                        </div>
                        <div class="row container-sm mx-auto p-0">
                            <div class="col col-lg-12 col-md-12 col-sm-12" id="order_details">
                                <table class="table" id="send_order_table">
                                    <thead>
                                        <tr>
                                            <th colspan="3" style=" width: 100px; "><?php echo $this->lang->line('BARCODE'); ?></th>
                                            <th><?php echo $this->lang->line('description'); ?></th>
                                            <th><?php echo $this->lang->line('unit'); ?></th>
                                            <th><?php echo $this->lang->line('rest_delivery'); ?></th>
                                            <th><?php echo $this->lang->line('delivery_amount'); ?></th>
                                            <th><?php echo $this->lang->line('in_stock'); ?></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>

                            <div class="final_info">

                                <div class="final_number flex-row w-100">
                                    <div class="form-full mt-1 w-50" style="margin: 0px;">
                                        <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_shekel'); ?></label>
                                        <input type="text" value="0" name="total" id="total_items" class="form-control w-50 text-center p-0" readonly />
                                    </div>
                                    <div class="align-items-center col-lg-5 d-flex justify-content-end w-50">
                                        <a href="#0" class="btn btn-danger close_modal_bottom" rel-modaltype="delivery" style=" margin: 0 10px !important;padding: 4px 20px !important;">
                                            <?php echo $this->lang->line('close'); ?>
                                        </a>

                                        <a href="#0" class="btn btn-primary p-1 mr-1" id="full_order">
                                            <?php echo $this->lang->line('Full_delivery'); ?>
                                        </a>
                                        <a href="#0" class="btn btn-success p-1 mr-1" id="to_invoice" style="margin-right:10px ; ">
                                            <?php echo $this->lang->line('Issuing_an_invoice'); ?>
                                        </a>


                                    </div>
                                </div>
                            </div>

                            <div class="col col-lg-12 col-lg-12 col-sm-12">
                                <div class="row justify-content-between">
                                </div>
                            </div>
                        </div>
                    </page>
                    <form action="<?php echo site_url(); ?>admin/orders/orderOutwithInvoice" method="POST" class="createInvoiceOrderForm" autocomplete="OFF">
                        <input type="hidden" value="" id="secret_order_id" name="order_id" />
                        <div class="first-stage position-relative" id="out_page_one" style="display: none;">
                            <div class="form-header">
                                <div class="invoice-head">
                                    <label for="invoice_code"><?php echo $this->lang->line('invoices'); ?></label>
                                    <input class="form-control" value="<?php echo  getCustomerNewInvoiceNo() ?>" name="invoice_code" readonly />
                                </div>
                                <div class="invoice-head">
                                    <label for="invoice_date"><?php echo $this->lang->line('date'); ?></label>
                                    <input type="text" value="<?php echo date("d/m/Y", strtotime('now')); ?>" name="invoice_date" id="invoice_date" class="datepicker form-control" />
                                </div>
                            </div>
                            <div class="ZSHEL">
                                <div class="form-full  justify-content-start">
                                    <label for=""><?php echo $this->lang->line('customer'); ?></label>
                                    <div style="margin-right: 37px;">
                                        <input class="form-control" value="" id="invoice_for" name="invoice_for" readonly />
                                        <input type="hidden" value="" name="invoice_customer" id="customer_id">
                                    </div>
                                </div>
                                <div class="form-full">
                                    <div class="invoice-custom_75 d-flex" style="width: 80%;">
                                        <label for="" class="" style="width: 20.3%;">
                                            <div style="max-width: 20px;">
                                                <?php echo $this->lang->line('manger_note'); ?>
                                            </div>
                                        </label>
                                        <input id="fill_boss_Comment1" class="form-control w-75" name="boss_Comment">
                                    </div>
                                    <div class="invoice-custom_25 d-flex" style="width: 30%">
                                        <label class="w-50"><?php echo $this->lang->line('customer_balance'); ?></label>
                                        <input id="out_customer_credit" type="text" class="form-control w-50 p-1 text-center" value="" name="customer_credit" style="font-size:8px; color: tomato !important; font-weight: bold !important;" readonly />
                                    </div>
                                </div>
                                <div class="form-full">
                                    <label for=""><?php echo $this->lang->line('details'); ?></label>
                                    <input class="form-control fill_cust_comment" name="custComment" id="cust_Comment">
                                </div>
                                <div class="form-full  justify-content-start">
                                    <label for=""><?php echo $this->lang->line('warehouse'); ?></label>
                                    <div style="margin-right: 77px;">
                                        <input class="form-control" value="" id="warehouse_name" name="warehouse_name" readonly />
                                        <input type="hidden" value="" id="warehouse_name1" name="warehouse">
                                    </div>
                                </div>
                                <div class="form-full" style="margin-top: 50px;">
                                    <label for="">הערות</label>
                                    <textarea class="form-control" style="width: 85%; height: 150px;" placeholder="<?php echo $this->lang->line('Enter_your_comment'); ?>" id="comment1" name="comment1"></textarea>
                                </div>
                            </div>
                            <div class="w-20 d-inline-flex p-2 float-start">
                                <button type="button" class="btn btn-primary rounded p-1" id="reverse-stage"><?php echo $this->lang->line('back'); ?></button>
                                <button type="button" class="btn btn-success rounded p-1 " id="next-page" style="margin-right: 10px;">
                                    אשור
                                </button>
                            </div>
                        </div>
                        <page size="A4" style="display: none;" id="page_two">
                            <div class="second_stage_header row align-items-start">
                                <div class="right-section w-50">
                                    <div class="w-50 m-2">
                                        <label for="" class="me-1"><?php echo $this->lang->line('invoices'); ?>: </label>
                                        <label for="" class="fw-bold"><?php echo getCustomerNewInvoiceNo() ?></label>
                                    </div>
                                    <div class="w-50 m-2">
                                        <label for="" class="me-1" name="customer" id="customer"><?php echo $this->lang->line('customer'); ?>: </label>
                                        <label for=""></label>
                                    </div>
                                    <div class="form-full w-50 m-2">
                                        <label for="" class="me-1"><?php echo $this->lang->line('details'); ?>: </label>
                                        <input class="form-control" name="cust_comment" id="cust_comment1" readonly />
                                    </div>
                                </div>
                                <div class="left-section w-50">
                                    <div class="invoice-head w-50 m-2">
                                        <label for="" class="me-1"><?php echo $this->lang->line('date'); ?>: </label>
                                        <input class="form-control" value="" name="invoice_date1" id="invoice_date1" readonly />
                                    </div>
                                </div>
                            </div>
                            <div class="row container-sm mx-auto p-0">
                                <div class="col col-lg-12 col-md-12 col-sm-12" id="invoice_details">
                                    <table class="table" id="order_invoice_table">
                                        <thead>
                                            <tr>
                                                <th colspan="3" style=" width: 100px; "><?php echo $this->lang->line('BARCODE'); ?></th>
                                                <th><?php echo $this->lang->line('description'); ?></th>
                                                <th><?php echo $this->lang->line('unit'); ?></th>
                                                <th><?php echo $this->lang->line('unit_price'); ?></th>
                                                <th><?php echo $this->lang->line('sum'); ?></th>
                                                <th><?php echo $this->lang->line('actions'); ?></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div>

                                <div class="final_info">
                                    <div class="form-full">
                                        <textarea class="form-control w-75" style="height: 100px" placeholder="<?php echo $this->lang->line('notes'); ?>" id="floatingTextarea" name="comment2"></textarea>
                                    </div>
                                    <div class="final_number">
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_before_vat'); ?></label>
                                            <input type="text" value="0" name="total_before_vat" class="fill_total_before_vat form-control w-50 text-center p-0" readonly />
                                        </div>
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('vat_17'); ?></label>
                                            <input type="text" value="0" name="vat" class="form-control w-50 fill_vat text-center p-0" readonly />
                                        </div>
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_shekel'); ?></label>
                                            <input type="text" value="0" name="total" class="fill_total form-control w-50 text-center p-0" readonly />
                                        </div>
                                    </div>
                                </div>

                                <div class="col col-lg-12 col-lg-12 col-sm-12">
                                    <div class="row justify-content-between">
                                        <div class="col-lg-3 d-flex align-items-center">
                                            <div class="invoice-custom_25 d-flex">
                                                <label class="w-50"><?php echo $this->lang->line('customer_balance'); ?></label>
                                                <input type="text" class="form-control w-50 p-1 text-center" value="" name="credit1" id="customer_credit1" readonly>
                                            </div>
                                        </div>
                                        <div class="align-items-center col-lg-5 d-flex justify-content-end">
                                            <button type="button" class="reverse-stage btn btn-primary p-1"><i class="fas fa-angle-right me-1"></i><?php echo $this->lang->line('back'); ?></button>
                                            <a href="#0" class="btn btn-danger close_modal_bottom" id="close_invoiceForm" rel-modaltype="delivery" style=" margin: 0 10px !important;padding: 4px 20px !important;">
                                                <?php echo $this->lang->line('close'); ?>
                                            </a>
                                            <a href="#0" class=" save_order_invoice btn btn-success p-1 mr-1">
                                                <?php echo $this->lang->line('invoicing_save'); ?>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </page>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal" rel-modaltype="delete" id="DeleteOrder" role="dialog" style="display: none;">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content position-relative">
            <a href="#0" class="close_modal close_modal_bottom" rel-modaltype="delete" rel-delete="true">
                <i class="fas fa-times-circle text-danger"></i>
            </a>

            <div class="modal-body" style="background-color: #f5f5f5;">
                <div id="content">
                    <form action="<?php echo site_url(); ?>admin/orders/deleteOrder" method="POST" id="deleteOrderForm" autocomplete="OFF">
                        <div class="first-stage">
                            <div class="form-header">
                                <div class="invoice-head">
                                    <label for=""><?php echo $this->lang->line('order'); ?></label>
                                    <input id="fill_order_code" class="form-control" value="" name="invoice_code" readonly>
                                </div>
                                <div class="  d-flex flex-column align-items-end ">
                                    <div class="invoice-head w-75">
                                        <label for=""><?php echo $this->lang->line('date'); ?></label>
                                        <input id="fill_order_date" type="text" name="order_date" class=" form-control" readonly />
                                    </div>
                                    <div class="invoice-head w-75">
                                        <label for=""><?php echo $this->lang->line('delivery_date'); ?></label>
                                        <input id="fill_delivery_date" type="text" name="delivery_date" class="form-control" readonly />
                                    </div>
                                </div>
                            </div>
                            <div class="form-full">
                                <label for=""><?php echo $this->lang->line('customer'); ?></label>
                                <div class="selectorHolder">
                                    <input type="text" class="form-control w-100" id="fill_customer" readonly>
                                </div>

                            </div>
                            <div class="form-full">
                                <div class="w-75 invoice-custom_75 d-flex">
                                    <label for="" class="" style="width: 20%;"><?php echo $this->lang->line('manger_note'); ?></label>
                                    <input id="fill_boss_Comment" class="form-control w-75" value="<?php echo $this->lang->line('Please_do_not_open_an_order_higher_than_1000'); ?>" name="boss_Comment" readonly>
                                </div>
                                <div class="w-25 invoice-custom_25 d-flex">
                                    <label class="w-50"><?php echo $this->lang->line('customer_balance'); ?></label>
                                    <input id="fill_customer_credit" type="text" class="form-control w-50 p-1 text-center" value="" name="customer_credit" readonly style="font-size:8px; color: tomato !important; font-weight: bold !important;" />
                                </div>
                            </div>
                            <div class="form-full">
                                <label for=""><?php echo $this->lang->line('details'); ?></label>
                                <input class="form-control fill_cust_comment" name="custComment" readonly>
                            </div>
                            <div class="form-full">
                                <label for=""><?php echo $this->lang->line('warehouse'); ?></label>
                                <input id="fill_warehouse" class="form-control" placeholder="<?php echo $this->lang->line('first'); ?>" name="invoice_customer" readonly>
                            </div>
                            <div class="form-full" style="margin-top: 50px;">
                                <label for=""><?php echo $this->lang->line('notes'); ?></label>
                                <textarea id="fill_comment1" class="form-control" style="width: 85%; height: 150px;" placeholder="<?php echo $this->lang->line('Enter_your_comment'); ?>" name="comment1" readonly></textarea>
                            </div>
                        </div>
                        <page size="A4" style="display: none;">
                            <div class="second_stage_header row align-items-start">
                                <div class="right-section w-50">
                                    <div class="w-50 m-2">
                                        <label for="" class="me-1"><?php echo $this->lang->line('order'); ?>: </label>
                                        <label for="" class="fw-bold invoiceCodeLabel text-danger"></label>
                                    </div>
                                    <div class="w-50 m-2">
                                        <label for="" class="me-1" name="customer"><?php echo $this->lang->line('customer'); ?>: </label>
                                        <label for="" id="fill_customer2"></label>
                                    </div>
                                    <div class="form-full w-50 m-2">
                                        <label for="fill_cust_comment" class="me-1"><?php echo $this->lang->line('details'); ?>: </label>
                                        <input id="fill_cust_comment" class="form-control fill_cust_comment" name="cust_comment" readonly>
                                    </div>
                                </div>
                                <div class="left-section w-50">
                                    <div class="invoice-head m-2" style="width: 60%;">
                                        <label for="" class="me-1"><?php echo $this->lang->line('date'); ?> </label>
                                        <input id="fill_order_date1" class="form-control" value="" name="invoice_date1" readonly>
                                    </div>
                                    <div class="invoice-head  m-2" style="width: 60%;">
                                        <label for="" class="me-1"><?php echo $this->lang->line('delivery_date'); ?></label>
                                        <input id="fill_delivery_date1" class="form-control" value="" name="delivery_date1" readonly>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col col-lg-12 col-md-12 col-sm-12">
                                    <table class="table">
                                        <thead>
                                            <tr>
                                                <th colspan="3" style=" width: 100px; "><?php echo $this->lang->line('BARCODE'); ?></th>
                                                <th><?php echo $this->lang->line('description'); ?></th>
                                                <th><?php echo $this->lang->line('unit'); ?></th>
                                                <th><?php echo $this->lang->line('unit_price'); ?></th>
                                                <th><?php echo $this->lang->line('sum'); ?></th>
                                                <th><?php echo $this->lang->line('in_stock'); ?></th>
                                            </tr>
                                        </thead>
                                        <tbody id="fill_items" class="bg-white">
                                        </tbody>
                                    </table>
                                </div>
                                <div class="final_info">
                                    <div class="form-full">
                                        <textarea class="form-control w-75" style="height: 100px" placeholder="<?php echo $this->lang->line('notes'); ?>" id="delete_comment2" name="comment2"></textarea>
                                    </div>
                                    <div class="final_number">
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_before_vat'); ?></label>
                                            <input type="text" value="0" name="total_before_vat" class="fill_total_before_vat form-control w-50 text-center p-0" readonly />
                                        </div>
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('vat_17'); ?></label>
                                            <input type="text" value="0" name="vat" class="form-control w-50 fill_vat text-center p-0" readonly />
                                        </div>
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_shekel'); ?></label>
                                            <input type="text" value="0" name="total" class="fill_total form-control w-50 text-center p-0" readonly />
                                        </div>
                                    </div>
                                </div>
                                <div class="col col-lg-12 col-lg-12 col-sm-12">
                                    <div class="row justify-content-between">
                                        <div class="col-lg-3 d-flex align-items-center">
                                            <div class="invoice-custom_25 d-flex">
                                                <label class="w-50"><?php echo $this->lang->line('customer_balance'); ?></label>
                                                <input type="text" class="form-control w-50 p-1 text-center" value="" name="credit1" id="fill_credit1">
                                            </div>
                                        </div>
                                        <div class="d-flex justify-content-end position-absolute bottom-0 end-0" style="margin-bottom: 10px;">
                                            <button type="button" class="reverse-stage btn btn-primary p-1" rel-delete="true"><i class="fas fa-angle-right me-1"></i><?php echo $this->lang->line('back'); ?></button>
                                            <a href="#0" class="btn btn-danger close_modal_bottom" rel-modaltype="delete" style=" margin: 0 10px !important;padding: 4px 20px !important;" rel-delete="true">
                                                <?php echo $this->lang->line('close'); ?>
                                            </a>
                                            <a href="#0" class="btn btn-warning p-1 mr-1 executeInvoiceDelete" id="del_order" rel-order="">
                                                <?php echo $this->lang->line('Delete_Order'); ?> </a>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </page>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade  " id="categories_Modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-xl  modal-fullscreen-sm-down ">

        <div class="modal-content " dir="auto" style="height: 70vh; ">
            <div class="d-flex flex-wrap justify-content-between align-items-center">
                <h3 class="text-center mt-3 "></h3>
                <h1 class="text-center mt-3 "> <?php echo $this->lang->line('products'); ?></h3>
                    <button type="button" class="btn-close btn-close-balck" style="margin-right: 10px; color:red" aria-label="Close" data-bs-dismiss="modal"></button>
            </div>
            <div class="w-100 d-flex justify-content-center mt-2">
                <div class="input-group mb-3 ms-2" style="width: 300px;">
                    <input type="text" class="form-control" id="search_for_item" placeholder="<?php echo $this->lang->line('search_for_product'); ?>" aria-label="<?php echo $this->lang->line('search_for_product'); ?>" aria-describedby="btn_search_for_item">
                    <button class="btn btn-outline-secondary" type="button" id="btn_search_for_item"><i class="fas fa-search"></i></button>
                </div>
            </div>
            <div class="d-flex flex-wrap justify-content-center p-3" id="items_cards" style="overflow-y: auto;">


            </div>
            <h3 class="text-center" id="items_empty"><?php echo $this->lang->line('empty_products'); ?></h3>
        </div>
    </div>
</div>
<div class="modal" rel-modaltype="update" id="updateOrder" role="dialog" style="display: none;">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content position-relative">
            <a href="#0" class="close_modal close_modal_bottom" id="close_DeleteModal" rel-modaltype="update" rel-delete="true">
                <i class="fas fa-times-circle text-danger"></i>
            </a>
            <div class="modal-body" style="background-color: #f5f5f5;">
                <div id="content">
                    <form action="<?php echo site_url(); ?>admin/orders/updateaction" method="POST" id="updateOrderForm" autocomplete="OFF">
                        <page size="A4" style="">
                            <div class="second_stage_header row align-items-start">
                                <div class="right-section w-50">
                                    <div class="w-50 m-2">
                                        <input type="hidden" value="" name="order_id" id="update_order_id" />
                                        <label for="" class="me-1"><?php echo $this->lang->line('order'); ?>: </label>
                                        <label for="" class="fw-bold orderCodeLabel text-danger"></label>
                                    </div>
                                    <div class="w-50 m-2">
                                        <label for="" class="me-1" name="customer"><?php echo $this->lang->line('customer'); ?>: </label>
                                        <label for="" id="fill_update_customer"></label>
                                        <input type="hidden" id="update_order_for" name="order_for" />
                                    </div>
                                    <div class="form-full w-50 m-2">
                                        <label for="fill_cust_comment" class="me-1"><?php echo $this->lang->line('details'); ?>: </label>
                                        <input id="fill_updat_cust_comment" class="form-control fill_cust_comment" name="cust_comment" readonly>
                                    </div>
                                    <div class="form-full d-flex m-2" style="width: 55%;">
                                        <label for="fill_cust_comment" class="me-1"><?php echo $this->lang->line('warehouse'); ?>: </label>
                                        <div class="selectorHolder">
                                            <select class="select2" id="update_warehouse" name="warehouse">
                                                <optgroup class="warehouse_list_from" label="ספקים" id="" name="update_warehouse">
                                                    <option value=""><?php echo $this->lang->line('select2'); ?></option>
                                                    <?php foreach ($warehouse as  $house) { ?>
                                                        <option value="<?php echo $house->ID; ?>"><?php echo $house->Name; ?></option>
                                                    <?php  } ?>
                                                </optgroup>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="left-section w-50">
                                    <div class="invoice-head m-2" style="width: 60%;">
                                        <label for="" class="me-1"><?php echo $this->lang->line('date'); ?> </label>
                                        <input id="fill_update_order_date" class="form-control" value="" name="order_date" readonly>
                                    </div>
                                    <div class="invoice-head  m-2" style="width: 60%;">
                                        <label for="" class="me-1"><?php echo $this->lang->line('delivery_date'); ?></label>
                                        <input id="fill_update_delivery_date" class="form-control datepicker" value="" name="delivery_date">
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col col-lg-12 col-md-12 col-sm-12">
                                    <table class="table" id="order_update_table">
                                        <thead>
                                            <tr>
                                                <th colspan="3" style=" width: 100px; "><?php echo $this->lang->line('BARCODE'); ?></th>
                                                <th><?php echo $this->lang->line('description'); ?></th>
                                                <th><?php echo $this->lang->line('unit'); ?></th>
                                                <th><?php echo $this->lang->line('unit_price'); ?></th>
                                                <th><?php echo $this->lang->line('sum'); ?></th>
                                                <th><?php echo $this->lang->line('in_stock'); ?></th>
                                                <th><?php echo $this->lang->line('actions'); ?></th>
                                            </tr>
                                        </thead>
                                        <tbody id="fill_update_items" class="bg-white">
                                        </tbody>
                                    </table>
                                </div>
                                <div class="final_info">
                                    <div class="form-full">
                                        <textarea class="form-control w-75" style="height: 100px" placeholder="<?php echo $this->lang->line('notes'); ?>" id="update_comment" name="comment"></textarea>
                                    </div>
                                    <div class="final_number">
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_before_vat'); ?></label>
                                            <input type="text" value="0" name="total_before_vat" class="fill_total_before_vat form-control w-50 text-center p-0" readonly />
                                        </div>
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('vat_17'); ?></label>
                                            <input type="text" value="0" name="vat" class="form-control w-50 fill_vat text-center p-0" readonly />
                                        </div>
                                        <div class="form-full mt-1" style="margin: 0px;">
                                            <label class="w-50 fw-bold" for="" style="font-size: 11px; font-weight: bold !important;"><?php echo $this->lang->line('total_shekel'); ?></label>
                                            <input type="text" value="0" name="total" class="fill_total form-control w-50 text-center p-0" readonly />
                                        </div>
                                    </div>
                                </div>
                                <div class="col col-lg-12 col-lg-12 col-sm-12">
                                    <div class="row justify-content-between">
                                        <div class="col-lg-3 d-flex align-items-center">
                                            <div class="invoice-custom_25 d-flex">
                                                <label class="w-50"><?php echo $this->lang->line('customer_balance'); ?></label>
                                                <input type="text" class="form-control w-50 p-1 text-center" value="" name="credit1" id="fill_update_credit">
                                            </div>
                                        </div>
                                        <div class="d-flex justify-content-end position-absolute bottom-0 end-0" style="margin-bottom: 10px;">
                                            <a href="#0" class="btn btn-danger close_modal_bottom" rel-modaltype="update" id="close_update_orderForm" style=" margin: 0 10px !important;padding: 4px 20px !important;">
                                                <?php echo $this->lang->line('close'); ?>
                                            </a>
                                            <button type="submit" class=" update_order_submit btn btn-warning p-1 mr-1 " rel-order="">
                                                <?php echo $this->lang->line('Update_Order'); ?>
                                            </button>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </page>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" rel-modaltype="excel" id="ExcelExportModal" tabindex="-1" aria-labelledby="ExcelExportModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content position-relative p-3" style="background-color: #f5f5f5;">
            <!-- <form action="no-action" method="POST" id="search_form"> -->
            <div class="modal-body" style="background-color: #f5f5f5;">
                <div class="close_modal close_modal_bottom" id="close_ExportModal" rel-modaltype="excel" rel-export="true">
                    <i class="fas fa-times-circle text-danger"></i>
                </div>
                <div class="d-flex flex-column justify-content-center align-items-center w-75 excel-container">
                    <div class="excelTitle">
                        <?php echo $this->lang->line('Filter_export_by'); ?>:
                    </div>
                    <div class="ExcelFilteration">
                        <div class="label-excel-wrapper">
                            <input type="checkbox" name="exportByCustome" id="exportByCustomer" rel-exportby="customer">
                            <label for="exportByCustomer"><?php echo $this->lang->line('By_Customer'); ?>:</label>
                        </div>
                        <div class="exportChoice_wrapper">
                            <select name="" id="selectCustomer" class="w-100"></select>
                        </div>
                    </div>
                    <hr class="w-100 mx-auto bg-success">
                    <div class="ExcelFilteration">
                        <div class="label-excel-wrapper">
                            <input type="checkbox" name="exportByInvoice" id="exportByInvoice" rel-exportby="invoice">
                            <label for="exportByInvoice"><?php echo $this->lang->line('By_Invoice'); ?>:</label>
                        </div>
                        <div class="exportChoice_wrapper">
                            <select name="" id="selectInvoiceFrom" class="w-100 mb-2"></select>
                            <select name="" id="selectInvoiceTo" class="w-100"></select>
                        </div>
                    </div>
                    <hr class="w-100 mx-auto bg-success">
                    <div class="ExcelFilteration">
                        <div class="label-excel-wrapper">
                            <input type="checkbox" name="exportByDate" id="exportByDate" rel-exportby="date">
                            <label for="exportByDate"><?php echo $this->lang->line('By_Date'); ?>:</label>
                        </div>
                        <div class="exportChoice_wrapper">
                            <input type="text" placeholder="<?php echo $this->lang->line('from_date'); ?>" name="invoice_date" id="invoiceDateFrom" class="datepicker form-control" />
                            <input type="text" placeholder="<?php echo $this->lang->line('to_date'); ?>" name="invoice_date" id="invoiceDateTo" class="datepicker form-control" />
                        </div>
                    </div>
                    <div class="excel_execute w-100 mt-3">
                        <button class="btn btn-primary btn-sm p-1 float-start" id="buildExcel">
                            <?php echo $this->lang->line('export'); ?>
                        </button>
                    </div>
                </div>
            </div>
            <!-- </form> -->
        </div>
    </div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>
<!-- toastify-js  js -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
<!--/////////////////-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.5.2/bootbox.min.js" integrity="sha512-RdSPYh1WA6BF0RhpisYJVYkOyTzK4HwofJ3Q7ivt/jkpW6Vc8AurL1R+4AUcvn9IwEKAPm/fk7qFZW3OuiUDeg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css">
<!-- toastify-js  css -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
<!--///////////////////-->
<script>
    <?php
    $url = '';
    if ($this->session->userdata('site_lang') == 'hebrew') {
        $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
    } else if ($this->session->userdata('site_lang') == 'arabic') {
        $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Arabic.json';
    } else if ($this->session->userdata('site_lang') == 'english') {
        $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/English.json';
    } else {
        $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
    }
    ?>

    function closeModal(modalId, closeRelation) {
        console.log(modalId, closeRelation);
        if (closeRelation == $(`#${modalId}`).attr('rel-modaltype')) {
            let modalSelector = `#${modalId}`;
            console.log(modalSelector);
            $(modalSelector).modal('hide');
            $(modalSelector).hide();
        }
    }

    $(document).ready(function() {





        $("#search_for_item").on("input", function() {
            $('.pick_product').removeClass('d-none');
            var filter = $("#search_for_item").val();
            $('.pick_product').find('.name:not(:contains("' + filter + '"))').parent().parent().addClass('d-none');
            checkEmptyDoms('pick_product', 'items_empty')
        })
        $('#search_for_item').keyup(function(event) {
            if (event.which === 13) {
                $('.pick_product').removeClass('d-none');
                var filter = $("#search_for_item").val();
                $('.pick_product').find('.name:not(:contains("' + filter + '"))').parent().parent().addClass('d-none');
                checkEmptyDoms('pick_product', 'items_empty')
            }
        });

        $('#open_catalog').on('click', function() {
            $("#items_cards").empty();
            $('#categories_Modal').modal('show');
            $(`#items_empty`).addClass("d-none");
            const warehouse_id = $('#storage').val();
            var type = $('#quantity_type').val();
            var data = {
                warehouse_id,
                type
            };
            //////// get products from server and set in html 
            $.post(root + 'admin/ajax/getProducts', data, function(products) {
                $.each(products, function(index, product) {
                    product.img = 'https://images.unsplash.com/photo-1496979551903-46e46589a88b?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=cda12b505afa1beb06e49d89014cbd65&auto=format&fit=crop&w=634&q=80';
                    $("#items_cards").append(`
                    <div class="pick_product product_card mx-2 my-2 d-flex flex-column   align-items-end justify-content-between " item_quantity="${product.warehouse_quantity}" item_name="${product.name}" item_price="${product.price}" item_id="${product.id}" style="${product.warehouse_quantity ? "cursor: pointer;" : "cursor: not-allowed;"}  background-image: url(${product.img});">
                        <div class="${!product.warehouse_quantity ? "out_of_stock" : ""} d-flex justify-content-center  align-items-center w-100" style="background-color:#fff">
                        <span  style="color:red; margin-left:10px ;display: ${product.warehouse_quantity ? "none" : "block"} "><?php echo $this->lang->line('out_of_stock'); ?></span>
                        </div>
                       <div class="w-100  d-flex flex-column   align-items-center justify-content-end" style="background-color:#fff">
                       <span class="text-center w-100 name" style="color: #000; word-break: break-all;">${product.name}</span>
                       <span class="text-center w-100 regular_price" style="color: green; word-break: break-all; display:none;">₪${product.price}</span>
                       <span class="text-center w-100 cartoon_price" style="color: green; word-break: break-all; display:none;">₪${product.cartoon_price}</span>
                       <span class="text-center w-100 pb-1" style="color: #000; word-break: break-all;"><?php echo $this->lang->line('quantity'); ?>:${product.warehouse_quantity}</span>
                       </div>
                    </div>
                    `)
                })
                //////// check type of price 
                let price_type = $('#quantity_type').val();
                $('.cartoon_price').hide();
                $('.regular_price').hide();
                $('.box_icon').hide();
                if (price_type == 'cartoon_quantity') {
                    $('.cartoon_price').show();
                    $('.box_icon').show();
                } else if (price_type == 'unit') {
                    $('.regular_price').show();
                } else {
                    $('.cartoon_price').hide();
                    $('.regular_price').hide();
                    $('.box_icon').hide();
                }

            }, 'json');
        });

        /////pick product and add in the list
        $(document).on('click', '.pick_product', function(e) {
            const _id = e.target.attributes['item_id'].value;
            const name = e.target.attributes['item_name'].value;
            const price = e.target.attributes['item_price'].value;
            const quantity = e.target.attributes['item_quantity'].value;
            if (!quantity) {
                return;
            }
            Toastify({
                text: "<?php echo $this->lang->line('product_is_added'); ?>",
                className: "info",
                position: "left",
                gravity: "bottom",
                style: {
                    background: "linear-gradient(to right, #00b09b, #96c93d)",
                }
            }).showToast();



            $('#invoice_table tbody').append(`
                        <tr class="items">
                        <td colspan="3"> <input disabled  value="${_id}" name="items[product_id][]"  rel-end = "0" class="form-control" ></input>
                      
                            <input type="hidden" name="items[code][]">
                            </td>
                            <td> <input type="text" value="${name}" name="items[description][]" class="item_desc form-control" readonly="readonly"/ rel-end = "0"></td>
                            <td> <input type="text" value="" name="items[unit][]" class="item_unit form-control" rel-end = "0"/></td>
                            <td colspan="3"> <input type="text" value="${price}" name="items[price][]" class="item_price form-control" rel-end = "0"/></td>
                            <td> <input type="text" value="" name="items[full_price][]" class="full_price form-control" readonly="readonly" rel-end = "0"/></td>
                            <td> <input type="text" value="0" name="items[in_stock][]" class="in_stock form-control"  rel-end = "1"/></td>
                            <td>
                                <div href="#0" class="remove_this_item btn btn-sm bg-danger text-white"><i class="fa fa-times"></i></div>
                            </td>
                        </tr>`);

        })



        let itemAddition = 0;
        var total_before_vat = 0;
        var vat = 0;
        var total = 0;
        // var warehouse = 0;
        let activities = [];

        function isValidDate(s) {
            // Assumes s is "mm/dd/yyyy"
            if (!/^\d\d\/\d\d\/\d\d\d\d$/.test(s)) {
                return false;
            }
            const parts = s.split('/').map((p) => parseInt(p, 10));
            parts[0] -= 1;
            const d = new Date(parts[2], parts[0], parts[1]);
            return d.getMonth() === parts[0] && d.getDate() === parts[1] && d.getFullYear() === parts[2];
        }
        $(document).on('click', '.order_page .add_invoice_button', function() {
            $('.next-stage').css('display', 'block');
            $('#page_one').show('slow');

            if ($('#myModal > .modal-dialog').hasClass('modal-lg')) {
                $('#myModal > .modal-dialog').removeClass('modal-lg');
            }
            if ($('#DeleteInvoice > .modal-dialog').hasClass('modal-lg')) {
                $('#myModal > .modal-dialog').removeClass('modal-lg');
            }
        });
        $(document).on('change', '.order_page #order_date', function(event) {
            if (!isValidDate($(this).val())) {
                $(this).val("");
            }
        });
        $(document).on('click', '#to_invoice', function(event) {
            let counter = 0;
            var units = 0;
            $(".unitOut").each(function(index) {
                var elem = $(this);
                var unitLeft = Number(elem.val());
                var productId = Number(elem.parent().parent().find('.item_code').attr('rel-product'));
                var units = Number(elem.parent().parent().find('.unitLeft').val());
                var order_id = Number($('#out_order_id').text())
                $('#secret_order_id').val(order_id);
                activities.push({
                    productId,
                    unitLeft,
                    order_id
                });
                if (units < 0) {
                    counter++;
                }
            });
            var total_items = $('#total_items').val();
            if (total_items > 0 && counter == 0) {
                $('#out_page_one').show();
                $('#check_items').hide();
            } else if (total_items == 0) {
                $(".unitOut").each(function(index) {
                    var elem = $(this);
                    elem.css({
                        "color": "red",
                        "border": "2px solid red "
                    });;
                });
            } else {
                $(".unitLeft").each(function(index) {
                    var elem = $(this);
                    elem.css({
                        "color": "red",
                        "border": "2px solid red "
                    });;
                });

            }

        });
        $(document).on('click', '#full_order', function() {
            $(".unitLeft").each(function(index) {
                var elem = $(this);
                var unitLeft = Number(elem.attr('rel-left'));
                var stock = Number(elem.parent().parent().find('.in_stock').attr('rel-stock'));
                // unit_left.val( Number(unit_left.attr('rel-left') ) - Number(newValue)  );
                // in_stock.val( Number(in_stock.attr('rel-stock') ) - Number(newValue)  );
                var stockLeft = stock - unitLeft;
                elem.parent().parent().find('.unitOut').val(unitLeft);
                elem.val(0);
                elem.parent().parent().find('.in_stock').val(stockLeft);
            });
            calcAll()
        });
        $(document).on('click', ' #next-page', function(event) {
            let checkArray = [];
            let elem = $(this);
            let isDeleteModal = elem.attr('rel-delete') === "true" ? true : false;
            let parentPageOne = elem.parent().parent();
            let inputArray = parentPageOne.find('input');
            $.each(inputArray, function(index, el) {
                console.log(el.getAttribute('id'));
                if (el.value == "" || el.value == 0 || el.value == -1) {
                    checkArray.push(false);
                    el.style.border = '2px solid red';
                    el.classList.add('is-invalid');
                } else {
                    el.classList.add('is-valid');
                    el.style.border = '2px solid green';
                    checkArray.push(el.value);
                }
            });
            if (!isDeleteModal) {
                $.each($('#invoice_for'), function(index, el) {
                    if (el.value == 0 ||
                        el.value == "" ||
                        el.value == undefined ||
                        el.value == null ||
                        el.value < 0) {
                        checkArray.push(false);
                        el.parentElement.style.border = "2px solid red";
                        el.parentElement.style.borderRadius = "5px";
                    } else {
                        el.parentElement.style.border = "2px solid green";
                        el.parentElement.style.borderRadius = "5px";
                        checkArray.push(el.value);
                    }
                });
            }
            if ($.inArray(false, checkArray) !== -1) {
                console.log('you forgot an input');
            } else {
                let date = $("#invoice_date").val();
                let comment = $("#cust_Comment").val();
                let credit = $("#out_customer_credit").val();
                let customer = $('#invoice_for').val();
                let elem = $(this);
                $('#page_two').show();
                $("#customer").html("לקוח: " + customer);
                $("#cust_comment1").val(comment);
                $("#invoice_date1").val(date);
                $("#customer_credit1").val(credit);
                // let parentModal = parentPageOne.parent().parent().parent().parent().parent();
                // parentModal.addClass('modal-lg');
                $('#out_page_one').hide();
                $.post(root + 'admin/orders/getItemDetails', {
                    activitiesArray: activities
                }, function(d) {
                    $('#order_invoice_table tbody').html("");
                    $(d.data).each(function(index, value) {
                        var $row = '<tr class="items">';
                        $row += '<td colspan="3" style=" width: 100px;"><input class="form-control" name="items[code][]" id="code" value="' + value.code + '"</td>';
                        $row += '<td><input class="form-control" name="items[product][]" id="" value="' + value.name + '"><input class="form-control" type="hidden" name="items[product_id][]" id="product" value="' + value.productId + '"></td>';
                        $row += '<td><input class="form-control  item_unit" name="items[unit][]" id="unite" value="' + value.unite + '"</td>';
                        $row += '<td><input class="form-control item_price" name="items[price][]" id="price" value="' + value.price + '"</td>';
                        //     $row += '<td><input class="form-control" name="item[' + value.item_id + '][discount]" id="discount" value="' + value.discount + '"</td>';
                        $row += '<td><input class="form-control full_price" name="items[full_price][]" id="full_price" value=""</td>';
                        $row += '<td><div href="#0" class="remove_this_item btn btn-sm bg-danger text-white"><i class="fa fa-times"></i> </div></td>';
                        $row += '</tr>';
                        $('#order_invoice_table tbody').append($row);
                    });
                    calcTotal();
                    // $("#customer").html("לקוח: " + customer);
                }, 'json');
            }
        });
        $(document).on('click', '.next-stage', function() {
            console.log("test");
            let checkArray = [];
            let elem = $(this);
            let isDeleteModal = elem.attr('rel-delete') === "true" ? true : false;
            let parentPageOne = elem.parent().parent();
            let inputArray = parentPageOne.find('input');
            let parentForm = elem.parents('form');
            $.each(inputArray, function(index, el) {
                let elName = el.getAttribute('name');
                if (elName == "boss_Comment" || elName == "custComment") {
                    checkArray.push(true);
                } else {
                    if (el.value == "" || el.value == 0 || el.value == -1) {
                        checkArray.push(false);
                        el.classList.add('is-invalid');
                    } else {
                        el.classList.add('is-valid');
                        checkArray.push(el.value);
                    }
                }
            });
            if (!isDeleteModal) {
                let orderFor = parentPageOne.find('select[name="order_for"]');
                $.each(orderFor, function(index, el) {
                    if (el.value == 0 ||
                        el.value == "" ||
                        el.value == undefined ||
                        el.value == null ||
                        el.value < 0) {
                        checkArray.push(false);
                        el.parentElement.style.border = "2px solid red";
                        el.parentElement.style.borderRadius = "5px";
                    } else {
                        el.parentElement.style.border = "2px solid green";
                        el.parentElement.style.borderRadius = "5px";
                        checkArray.push(el.value);
                    }
                });
            }
            if ($.inArray(false, checkArray) !== -1) {
                console.log('you forgot an input');
            } else {
                let date = parentForm.find("#order_date").val();
                let delivery = parentForm.find('#delivery_date').val();
                let comment = parentForm.find("#custComment").val();
                let credit = parentForm.find("#customer_credit").val();
                let customer = parentForm.find('.order_for').find('option:selected').text();
                let elem = $(this);
                elem.parent().parent().parent().find('page').show();
                parentForm.find("#customer").html("לקוח: " + customer);
                parentForm.find("#cust_comment").val(comment);
                parentForm.find("#order_date1").val(date);
                parentForm.find('#delivery_date1').val(delivery);
                parentForm.find("#credit1").val(credit);
                let parentModal = parentPageOne.parent().parent().parent().parent().parent();
                parentModal.addClass('modal-lg');
                elem.parent().parent().hide();
            }
        });
        $('.order_page #DeleteInvoice').on('hidden.bs.modal', function(e) {
            let elem = $(this);
            elem.find('.first-stage').show();
            elem.find('.modal-dialog').removeClass('modal-lg');
            elem.find('page').hide();
        });
        $('.order_page #myModal').on('hidden.bs.modal', function(e) {
            let elem = $(this);
            elem.find('.first-stage').show();
            elem.find('.modal-dialog').removeClass('modal-lg');
            elem.find('page').hide();
        });
        $(' .reverse-stage').on('click', function() {
            let elem = $(this);
            let isDeleteModal = elem.attr('rel-delete') === "true" ? true : false;

            if (isDeleteModal) {
                $('#DeleteInvoice .modal-dialog').removeClass('modal-lg');
            } else {
                $('#myModal .modal-dialog').removeClass('modal-lg');
            }

            elem.parent().parent().parent().parent().parent().parent().find('.first-stage').show();
            elem.parent().parent().parent().parent().parent().hide();
        });
        $(' #reverse-stage').on('click', function() {
            $('#check_items').show();
            $('#out_page_one').hide();
            $('#myModal .modal-dialog').addClass('modal-lg');
        });
        $('.order_page #dt_basic2').DataTable({
            "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'dt-buttons col-sm-6 col-xs-12 hidden-xs'l>r>" +
                "t" +
                "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
            "buttons": [],
            "processing": false,
            "bStateSave": false,
            "dom": 'Blfrtip',
            "pagingType": "full_numbers",
            "lengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "<?php echo $this->lang->line('Everything'); ?>"]
            ],
            "pageLength": 20,
            "sPaginationType": "full_numbers",
            "iDisplayLength": 10,
            "sScrollY": 500,
            "scrollX": true,
            "bScrollCollapse": true,
            "searchCols": [
                null,
                {
                    "search": "My filter"
                },
                null,
                {
                    "search": "^[0-9]",
                    "regex": true
                }
            ],
            "language": {
                "url": "<?php echo $url; ?>"
            },


            orderCellsTop: true,
            fixedHeader: false,
            searching: false
        });
        $(".order_page #firstPayDate").datepicker({
            dateFormat: 'dd/mm/yy',
            defaultDate: "now",
            changeMonth: true,
            numberOfMonths: 2,
            isRTL: true,
            prevText: '<i class="fa fa-chevron-left"></i>',
            nextText: '<i class="fa fa-chevron-right"></i>',
            onClose: function(selectedDate) {
                $("#firstPayToDate").datepicker("option", "minDate", selectedDate);
            }
        });
        $(".order_page #firstPayToDate").datepicker({
            dateFormat: 'dd/mm/yy',
            defaultDate: "now",
            changeMonth: true,
            numberOfMonths: 2,
            isRTL: true,
            prevText: '<i class="fa fa-chevron-left"></i>',
            nextText: '<i class="fa fa-chevron-right"></i>',
            onClose: function(selectedDate) {
                $("#firstPayDate").datepicker("option", "maxDate", selectedDate);
            }
        });
        $(document).on('click', '.order_page .close_modal, .close_modal_bottom', function() {
            console.log('clicked it ')
            let elem = $(this);
            let parentModal = elem.parents('.modal');
            // console.log(parentModal);
            closeModal(parentModal.attr('id'), elem.attr('rel-modaltype'));
        });
        $(document).on('click', '.order_page .cancel_order2', function() {
            let data = [];
            let order_id = 0;
            let counter = 0;
            $(".order_check").each(function() {

                var elem = $(this);
                if (elem.prop('checked') == true) {
                    order_id = elem.parent().parent().parent().attr('rel-order');
                    data.push(order_id);
                    counter++;
                }

            });
            if (counter >= 1) {
                let text = "<?php echo $this->lang->line('sure_delete_order'); ?>";
                var dialog = bootbox.dialog({
                    title: 'Order Cancelation',
                    message: "<p><?php echo $this->lang->line('sure_delete_order'); ?> ?</p>",
                    size: 'small',
                    buttons: {
                        cancel: {
                            label: "No",
                            className: 'btn-primary',
                            callback: function() {
                                console.log('Custom cancel clicked');
                            }
                        },
                        ok: {
                            label: "Yes",
                            className: 'btn-danger',
                            callback: function() {
                                $.post(root + 'admin/orders/deleteOrder', {
                                    orderArray: data
                                }, function(d) {

                                    if (d.result == true) {
                                        bootbox.alert("<?php echo $this->lang->line('Order_Deleted_successfully'); ?>");
                                        setTimeout(() => location.reload(), 500);
                                    } else {
                                        bootbox.alert("<?php echo $this->lang->line('An_error_occurred'); ?>");
                                    }
                                }, 'json');
                                console.log('Custom OK clicked');
                            }
                        }
                    }
                });
            } else {
                bootbox.alert("<?php echo $this->lang->line('select_order_to_continue'); ?>");
            }

        });
        $(document).on('change', '.order_page .order_check', function() {
            let counter = 0;
            $(".order_check").each(function() {
                var elem = $(this);
                if (elem.prop('checked') == true) {
                    counter++;
                }
            });
            console.log(counter);
            if (counter < 2) {
                $(".filed_effect").attr("disabled", "disabled");
            }
        });
        $(document).on('click', '.order_page #confirmOrderButton', function() {
            let data = [];
            let order_id = 0;
            let elemnt = $(this);
            let status = $('#indoor_status').val();
            let text = $('#indoor_status').find('option:selected').text();
            $(".order_check").each(function() {
                var elem = $(this);
                if (elem.prop('checked') == true) {
                    order_id = elem.parent().parent().parent().attr('rel-order');
                    data.push({
                        order_id,
                        status,
                        text
                    });
                    counter++;
                }

            });
            if (counter >= 1) {
                var elem = $(this);
                $.post(root + 'admin/orders/confirmOrder', {
                    orderData: data
                }, function(d) {
                    if (d.result == true) {
                        bootbox.alert("<?php echo $this->lang->line('Order_Confirmed_successfully'); ?>");
                        setTimeout(() => location.reload(), 500);
                    } else {
                        bootbox.alert("<?php echo $this->lang->line('An_error_occurred'); ?>");
                    }
                }, 'json');
            } else {

                bootbox.alert("<?php echo $this->lang->line('select_order_to_continue'); ?>");
            }
        });
        $(document).on('click', '.order_page #destroy_table', function() {
            let elem = $(this);
            let action = elem.attr('rel-action');
            let close = 0;
            console.log(action);
            $('#dt_basic2').DataTable().clear().destroy();
            let data = {};
            $.post(root + 'admin/orders/getCanceledOrder', data, function(d) {
                if (d.result == true) {
                    if (action % 2 == 0) {
                        $(d.data).each(function(index, value) {
                            if (value.is_closed == 0) {
                                close = 0;
                            } else {
                                close = 1;

                            }
                            var $row = '<tr class= "items" rel-order="' + value.id + '"  rel-close="' + close + '" rel-status = "' + value.status + '">';
                            $row += '<td><div class="form-check"><input class="form-check-input order_check" type="checkbox" value="" id="orderCheck"></div></td>';
                            $row += '<td>' + value.id + ' </td>';
                            $row += '<td>' + value.customerName + '</td>';
                            $row += '<td> ' + value.total + ' </td>';
                            $row += '<td>' + value.order_date + '</td>';
                            $row += '<td>';
                            if (value.is_closed == 0) {
                                $row += '<span class="badge  bg-primary p-1 bg-info  "><?php echo $this->lang->line('no'); ?></span></td>';
                            } else {
                                $row += '<span class="badge  bg-danger p-1 bg-info  "><?php echo $this->lang->line('yes'); ?></span></td>';
                            }
                            $row += '<td>'
                            if (value.status == 4) {
                                $row += '<span class="badge  bg-primary p-1 bg-success "> <?php echo $this->lang->line('full'); ?> </span></td>';
                            } else if (value.status == 3) {
                                $row += '  <span class="badge  bg-primary p-1 bg-warning"> <?php echo $this->lang->line('partial'); ?></span></td>';
                            } else {}
                            $row += '<td class = "status_td"><div class="popup">'
                            if (value.status == 1) {
                                $row += '<div class="label bg-primary  p-1" rel-action="<?php $order->status ?>"><?php echo $this->lang->line('new'); ?></div>';
                            } else {
                                $row += ' <div class="label bg-success p-1" rel-action="<?php $order->status ?>"><?php echo $this->lang->line('Confirmed'); ?></div>';
                            }
                            $row += ' <div class="menu"><?php foreach ($statues as $current_status) { ?><div class="menu-item" rel-action="<?php echo $current_status->id; ?>"><?php echo $current_status->text; ?></div><?php }; ?></div>';
                            $row += '</div></div></td>'
                            $('#dt_basic2 tbody').append($row);

                        });
                        action++;
                        elem.attr('rel-action', action);
                        console.log(action);
                    } else {
                        $(d.data).each(function(index, value) {
                            if (value.is_closed == 0) {
                                var $row = '<tr class= "items" rel-order="' + value.id + '" rel-status = "' + value.status + '" >';
                                $row += '<td><div class="form-check"><input class="form-check-input order_check" type="checkbox" value="" id="orderCheck"></div></td>';
                                $row += '<td>' + value.id + ' </td>';
                                $row += '<td>' + value.customerName + '</td>';
                                $row += '<td> ' + value.total + ' </td>';
                                $row += '<td>' + value.order_date + '</td>';
                                $row += '<td>';
                                if (value.is_closed == 0) {
                                    $row += '<span class="badge  bg-primary p-1 bg-info  "><?php echo $this->lang->line('no'); ?></span></td>';
                                } else {
                                    $row += '<span class="badge  bg-danger p-1 bg-info  "><?php echo $this->lang->line('yes'); ?></span></td>';
                                }
                                $row += '<td>'
                                if (value.status == 4) {
                                    $row += '<span class="badge  bg-primary p-1 bg-success  "> <?php echo $this->lang->line('full'); ?> </span></td>';
                                } else if (value.status == 4) {
                                    $row += '  <span class="badge  bg-primary p-1 bg-warning  "> <?php echo $this->lang->line('partial'); ?></span></td>'
                                } else {}
                                $row += '<td class = "status_td"><div class="popup">'
                                if (value.status == 1) {
                                    $row += '<div class="label bg-primary  p-1" rel-action="<?php $order->status ?>"><?php echo $this->lang->line('new'); ?></div>';
                                } else {
                                    $row += ' <div class="label bg-success p-1" rel-action="<?php $order->status ?>"><?php echo $this->lang->line('Confirmed'); ?></div>';
                                }
                                $row += ' <div class="menu"><?php foreach ($statues as $current_status) { ?><div class="menu-item" rel-action="<?php echo $current_status->id; ?>"><?php echo $current_status->text; ?></div><?php }; ?></div>';
                                $row += '</div></div></td>'
                                $('#dt_basic2 tbody').append($row);

                            }
                        });

                        action++;
                        elem.attr('rel-action', action);
                        console.log(action);

                    }
                    $('.order_page #dt_basic2').DataTable({
                        "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'dt-buttons col-sm-6 col-xs-12 hidden-xs'l>r>" +
                            "t" +
                            "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
                        "buttons": [],
                        "processing": false,
                        "bStateSave": false,
                        "dom": 'Blfrtip',
                        "pagingType": "full_numbers",
                        "lengthMenu": [
                            [10, 25, 50, -1],
                            [10, 25, 50, "<?php echo $this->lang->line('Everything'); ?>"]
                        ],
                        "pageLength": 20,
                        "sPaginationType": "full_numbers",
                        "iDisplayLength": 10,
                        "sScrollY": 500,
                        "scrollX": true,
                        "bScrollCollapse": true,
                        "searchCols": [
                            null,
                            {
                                "search": "My filter"
                            },
                            null,
                            {
                                "search": "^[0-9]",
                                "regex": true
                            }
                        ],
                        "language": {
                            "url": "<?php echo $url; ?>"
                        },


                        orderCellsTop: true,
                        fixedHeader: false,
                        searching: false
                    });
                }

            }, 'json');
        });
        $(document).on('dblclick', '#dt_basic2 tr', function() {
            var selected_row = $('#dt_basic2 tbody tr.selected_row').attr("rel-order");
            var is_close = $('#dt_basic2 tbody tr.selected_row').attr("rel-close");
            console.log(is_close);
            if (selected_row !== undefined && is_close == 0) {
                let elem = $(this);
                let data = {};
                data.id = $('#dt_basic2 tbody tr.selected_row').attr("rel-order");
                $.post(root + 'admin/orders/getOrderData', data, function(d) {
                    if (d.result == true) {
                        let data = d.data.order_info;
                        $('#DeleteOrder .invoice_id_value').text(`${data.id}`);
                        $('#fill_order_code').val(`${data.id}`);
                        $('#fill_customer_credit').val(`${data.customerCredit}`);
                        $('#fill_credit1').val(`${data.customerCredit}`);
                        $('.invoiceCodeLabel').text(`${data.id}`);
                        $('#fill_order_date').val(`${data.order_date}`);
                        $('#fill_delivery_date').val(`${data.delivery_date}`);
                        $('#fill_delivery_date1').val(`${data.delivery_date}`);
                        $('#fill_boss_Comment').val(`${data.bossComment}`);
                        $('#fill_customer').val(`${data.customerName}`);
                        $('#fill_warehouse').val(`${data.warehouseName}`);
                        $('#fill_customer2').text(`${data.customerName}`);
                        $('.fill_cust_comment').val(`${data.custComment}`);
                        $('#fill_comment1').val(`${data.comment1}`);
                        $('#fill_comment2').val(`${data.comment2}`);
                        $('#fill_order_date1').val(`${data.order_date}`);
                        $('.executeInvoiceDelete').attr('rel-invoice', data.invoice_id);

                        let productsRows = ``;
                        let items = d.data.items;

                        for (let i = 0; i < items.length; i++) {
                            productsRows += `
                        <tr class="items">
                            <td colspan="3" style="width: 40%;">
                                <input type="text" value="${items[i].product_code}" class="item_code form-control text-center" readonly/>
                            </td>
                            <td>
                                <input type="text" value="${items[i].item_title}" class="item_product form-control text-center" readonly/>
                            </td>
                            <td>
                                <input type="text" value="${items[i].unit}" class="item_unit form-control text-center" readonly/>
                            </td>
                            <td>
                                <input type="text" value="${items[i].price}" class="item_price form-control text-center" readonly/>
                            </td>
                            <td>
                                <input type="text" value="" class="full_price form-control text-center" readonly="readonly" style="width: 100%;"/>
                            </td>
                            <td>
                            <input type="text" value="${items[i].item_instock}" class="item_inStock form-control text-center" readonly/>
                        </td>
                        </tr>`
                        }

                        $('#deleteOrderForm').css('display', 'flex');
                        $('.first-stage').css('display', 'none');
                        $('.next-stage').css('display', 'none');
                        $('.reverse-stage').css('display', 'none');
                        $('#DeleteOrder > .modal-dialog').addClass('modal-lg');
                        $('#DeleteOrder > .modal-dialog').removeClass('modal-xl');
                        $('#DeleteOrder page').show();
                        $('#DeleteOrder .first-stage').addClass('col-md-4 col-sm-4 col-xs-4');
                        $('.executeInvoiceDelete').css('display', 'none');
                        $('#fill_items').html(productsRows);
                        $('#DeleteOrder').modal('show');
                        calcTotal();
                    }
                }, 'json');
            }
        });
        $(document).on('click', '.order_page #updateOrderButton', function() {
            var selected_row = $('#dt_basic2 tbody tr.selected_row').attr("rel-order");
            var is_close = $('#dt_basic2 tbody tr.selected_row').attr("rel-close");
            console.log(is_close);
            if (selected_row !== undefined && is_close == 0) {
                let elem = $(this);
                let data = {};
                data.id = $('#dt_basic2 tbody tr.selected_row').attr("rel-order");
                $.post(root + 'admin/orders/getOrderData', data, function(d) {
                    if (d.result == true) {
                        let data = d.data.order_info;
                        $('#DeleteOrder .orderCodeLabel').text(`${data.id}`);
                        $('#update_order_id').val(`${data.id}`);
                        $('#fill_update_credit').val(`${data.customerCredit}`);
                        $('.orderCodeLabel').text(`${data.id}`);
                        $('#fill_update_order_date').val(`${data.order_date}`);
                        $('#fill_update_delivery_date').val(`${data.delivery_date}`);
                        // $('#fill_delivery_date1').val(`${data.delivery_date}`);
                        // $('#fill_boss_Comment').val(`${data.bossComment}`);
                        $('#fill_update_customer').text(`${data.customerName}`);
                        $('#fill_updat_cust_comment').val(`${data.custComment}`);
                        $('#update_order_for').val(`${data.customerId}`);
                        $('.update_order_submit').attr('rel-order', data.id);
                        console.log(`${data.customerId}`);
                        let productsRows = ``;
                        let items = d.data.items;
                        warehouse = data.warehouse_id;
                        $('#update_warehouse').val(`${data.warehouse_id}`); // Select the option with a value of '1'
                        $('#update_warehouse').trigger('change');
                        for (let i = 0; i < items.length; i++) {
                            productsRows += `
                        <tr class="items">
                            <td colspan="3" style="width: 40%;">
                                <input type="text" value="${items[i].product_code}" name="items[code][]"  class="item_code form-control text-center" rel-end = "0"/>
                                <input type="hidden" value="${items[i].product_id}" name="items[product_id][]" class= "item_id" />
                            </td>
                            <td>
                                <input type="text" value="${items[i].item_title}" name= "items[name][]" class="item_product form-control text-center" rel-end = "0"/>
                            </td>
                            <td>
                                <input type="text" value="${items[i].unit}" name= "items[unit][]"  class="item_unit form-control text-center" rel-end = "0"/>
                            </td>
                            <td>
                                <input type="text" value="${items[i].price}" name= "items[price][]" class="item_price form-control text-center" rel-end = "0"/>
                            </td>
                            <td>
                                <input type="text" value="" name= "items[full_price][]" class="full_price form-control text-center" style="width: 100%;" rel-end = "0"/>
                            </td>
                            <td>
                            <input type="text" value="${items[i].item_instock}" name = "items[in_stock][]"  class="item_inStock form-control text-center" rel-end = "1"/>
                        </td>
                        <td>
                         <div href="#0" class="remove_order_item btn btn-sm bg-danger text-white" rel-item="${items[i].product_id}" >
                            <i class="fa fa-times"></i>
                         </div>
                        </td>
                        </tr>`
                        }
                        $('#fill_update_items').html(productsRows);
                        $('#updateOrder').css('display', 'flex');
                        $('#updateOrder > .modal-dialog').addClass('modal-lg');
                        $('#updateOrder > .modal-dialog').removeClass('modal-xl');
                        $('#updateOrder page').show();
                        calcTotal();
                    } else {
                        alert("<?php echo $this->lang->line('Error'); ?>");
                    }
                }, 'json');
            } else {
                bootbox.alert("Please Select an invoice from the table below to continue");
            }
        });
        $('.datepicker').datepicker({
            dateFormat: "dd/mm/yy",
            isRTL: true,
            prevText: '<i class="fa fa-chevron-left"></i>',
            nextText: '<i class="fa fa-chevron-right"></i>',
        });
        document.querySelector('page').addEventListener('keydown', function(event) {
            var code = event.keyCode || event.which;
            if (code === 9) {
                // event.preventDefault();
                console.log(event.target.value);
                let is_end = event.target.getAttribute("rel-end");
                if (is_end == 1) {
                    event.target.setAttribute("rel-end", 0);
                    let data = {};
                    let warehouse_id = $('.order_for').val();
                    data.warehouse_id = warehouse_id;

                    var table = $('#invoice_table');
                    let newRow = `
                        <tr class="items">
                            <td colspan="3"> <select name="items[product_id][]" class="select2Custom" rel-end = "0"></select>
                            <input type="hidden" name="items[code][]">
                            </td>
                            <td> <input type="text" value="" name="items[description][]" class="item_desc form-control" readonly="readonly"/ rel-end = "0"></td>
                            <td> <input type="text" value="" name="items[unit][]" class="item_unit form-control" rel-end = "0"/></td>
                            <td> <input type="text" value="" name="items[price][]" class="item_price form-control" rel-end = "0"/></td>
                            <td> <input type="text" value="" name="items[full_price][]" class="full_price form-control" readonly="readonly" rel-end = "0"/></td>
                            <td> <input type="text" value="" name="items[in_stock][]" class="in_stock form-control"  rel-end = "1"/></td>
                            <td>
                                <div href="#0" class="remove_this_item btn btn-sm bg-danger text-white"><i class="fa fa-times"></i></div>
                            </td>
                        </tr>`;
                    // $(newRow).insertBefore('#btns_group');
                    $('#invoice_table tr:last').after(newRow);
                    $('.select2Custom').select2({
                        placeholder: '<?php echo $this->lang->line('Look_for_items'); ?>',
                        selectOnClose: false,
                        tags: true,
                        ajax: {
                            dataType: "json",
                            url: root + 'admin/invoices/getItems',
                            processResults: function(data) {
                                return {
                                    results: data.results
                                }
                            }
                        },
                        "language": {
                            "noResults": function() {
                                return "<?php echo $this->lang->line('Look_for_items'); ?>";
                            }
                        },
                        theme: "bootstrap-5"
                    });
                    calcTotal();
                }
            }
            if (code === 13) {
                event.preventDefault();
                let data = {};
                let warehouse_id = $('.order_for').val();
                data.warehouse_id = warehouse_id;

                var $table = $('#invoice_table');
                let newRow = `
                        <tr class="items">
                            <td colspan="3"> <select name="items[product_id][]" class="select2Custom" rel-end = "0"></select>
                            <input type="hidden" name="items[code][]">
                            </td>
                            <td> <input type="text" value="" name="items[description][]" class="item_desc form-control" readonly="readonly"/ rel-end = "0"></td>
                            <td> <input type="text" value="" name="items[unit][]" class="item_unit form-control" rel-end = "0"/></td>
                            <td> <input type="text" value="" name="items[price][]" class="item_price form-control" rel-end = "0"/></td>
                            <td> <input type="text" value="" name="items[full_price][]" class="full_price form-control" readonly="readonly" rel-end = "0"/></td>
                            <td> <input type="text" value="" name="items[in_stock][]" class="in_stock form-control"  rel-end = "1"/></td>
                            <td>
                                <div href="#0" class="remove_this_item btn btn-sm bg-danger text-white"><i class="fa fa-times"></i></div>
                            </td>
                        </tr>`;
                // $(newRow).insertBefore('#btns_group');
                $('#invoice_table tr:last').after(newRow);


                $('.select2Custom').select2({
                    placeholder: '<?php echo $this->lang->line('Look_for_items'); ?>',
                    selectOnClose: false,
                    tags: true,
                    ajax: {
                        dataType: "json",
                        url: root + 'admin/invoices/getItems',
                        processResults: function(data) {
                            return {
                                results: data.results
                            }
                        }
                    },
                    "language": {
                        "noResults": function() {
                            return "<?php echo $this->lang->line('Look_for_items'); ?>";
                        }
                    },
                    theme: "bootstrap-5"
                });
                calcTotal();
            }
        });
        document.querySelector('#updateOrder').addEventListener('keydown', function(event) {
            var code = event.keyCode || event.which;
            if (code === 9) {
                // event.preventDefault();
                console.log(event.target.value);
                let is_end = event.target.getAttribute("rel-end");
                if (is_end == 1) {
                    event.target.setAttribute("rel-end", 0);
                    let data = {};
                    let warehouse_id = $('.order_for').val();
                    data.warehouse_id = warehouse_id;

                    var $table = $('#order_update_table');
                    let newRow = `
                <tr class="items">
                  <td colspan="3"> <select name="items[product_id][]" class="select2Custom" rel-end = "0"></select>
                                        <input type="hidden" name="items[code][]">
                                        </td>
                <td>
                    <input type="text" value="" name="items[name][]" class="item_desc form-control text-center" rel-end = "0"/>
                </td>
                <td>
                    <input type="text" value="" name="items[unit][]" class="item_unit form-control text-center" rel-end = "0"/>
                </td>
                <td>
                    <input type="text" value="" name="items[price][]" class="item_price form-control text-center" rel-end = "0"/>
                </td>
                <td>
                    <input type="text" value="" name="items[full_price][]" class="full_price form-control text-center" style="width: 100%;" rel-end = "0"/>
                </td>
                <td>
                <input type="text" value="" name="items[in_stock][]" class=" in_stock form-control text-center" rel-end = "1"/>
              </td>
               <td>
               <div href="#0" class="remove_this_item btn btn-sm bg-danger text-white">
                <i class="fa fa-times"></i>
                </div>
                </td>
                 </tr>`;
                    $(newRow).insertBefore('#update_btns_group');
                    $('#order_update_table tr:last').after(newRow);


                    $('.select2Custom').select2({
                        placeholder: '<?php echo $this->lang->line('Look_for_items'); ?>',
                        selectOnClose: false,
                        tags: true,
                        ajax: {
                            dataType: "json",
                            url: root + 'admin/invoices/getItems',
                            processResults: function(data) {
                                return {
                                    results: data.results
                                }
                            }
                        },
                        "language": {
                            "noResults": function() {
                                return "<?php echo $this->lang->line('Look_for_items'); ?>";
                            }
                        },
                        theme: "bootstrap-5"
                    });
                    calcTotal();
                }
            }
            if (code === 13) {
                event.preventDefault();
                let data = {};
                let warehouse_id = $('.order_for').val();
                data.warehouse_id = warehouse_id;

                var $table = $('#order_update_table');
                let newRow = `
                <tr class="items">
                  <td colspan="3"> <select name="items[product_id][]" class="select2Custom" rel-end="0"></select>
                                        <input type="hidden" name="items[code][]">
                                        </td>
                <td>
                    <input type="text" value="" name="items[name][]" class="item_desc form-control text-center" rel-end="0"/>
                </td>
                <td>
                    <input type="text" value="" name="items[unit][]" class="item_unit form-control text-center" rel-end="0"/>
                </td>
                <td>
                    <input type="text" value="" name="items[price][]" class="item_price form-control text-center" rel-end="0"/>
                </td>
                <td>
                    <input type="text" value="" name="items[full_price][]" class="full_price form-control text-center" style="width: 100%;" rel-end="0"/>
                </td>
                <td>
                <input type="text" value="" name="items[in_stock][]" class=" in_stock form-control text-center" rel-end="1"/>
              </td>
               <td>
               <div href="#0" class="remove_this_item btn btn-sm bg-danger text-white">
                <i class="fa fa-times"></i>
                </div>
                </td>
                 </tr>`;
                // $(newRow).insertBefore('#update_btns_group');
                $('#order_update_table tr:last').after(newRow);


                $('.select2Custom').select2({
                    placeholder: '<?php echo $this->lang->line('Look_for_items'); ?>',
                    selectOnClose: false,
                    tags: true,
                    ajax: {
                        dataType: "json",
                        url: root + 'admin/invoices/getItems',
                        processResults: function(data) {
                            return {
                                results: data.results
                            }
                        }
                    },
                    "language": {
                        "noResults": function() {
                            return "<?php echo $this->lang->line('Look_for_items'); ?>";
                        }
                    },
                    theme: "bootstrap-5"
                });
                calcTotal();
            }
        });
        $('.order_page #add_payment_line').on('click', function() {
            var $table = $('#payment_table');
            var new_line = $table.find('tr.payments').first().clone();
            new_line.find("input.total_payment").val("");
            new_line.append('<a href="#0" class="remove_this_payment btn bg-danger btn-xs text-white"><i class="fa fa-times"></i></a>');
            new_line.insertBefore('#btns_group_payments');
        });
        $(document).on('click', '.remove_this_item', function() {
            var elem = $(this);
            var items = $(this).parent().parent().parent().find('tr.items').length;
            if (items <= 1) {
                elem.parent().parent().find('input, select').val("");
            } else {
                elem.parent().parent().remove();
            }
            calcTotal();
            itemAddition--;
        });
        $(document).on('click', ' .remove_order_item', function() {
            let elem = $(this);
            let data = {};
            $(this).parent().parent().remove();
            let product_id = $(this).attr('rel-item');
            let order_id = $('#updateOrderButton').attr('rel-order');
            data.product_id = product_id;
            data.order_id = order_id;
            console.log(data);
            $.post(root + 'admin/orders/deleteOrderItem', data, function(d) {
                console.log(d);
                if (d.result == true) {
                    bootbox.alert("<?php echo $this->lang->line('Item_deleted_successfully'); ?>");
                } else {
                    bootbox.alert("<?php echo $this->lang->line('Only_20_are_allowed'); ?>");
                }
            }, 'json');
            console.log('Custom OK clicked');
            calcTotal();
            itemAddition--;
        });
        $(document).on('click', '.save_order', function(e) {
            let checkArray = [];
            let checkFlag = false;
            let inputArray = $("#invoice_table .items input");
            $.each(inputArray, function(index, el) {
                if (el.value == "" || el.value == 0 || el.value == -1) {
                    checkArray.push(false);
                    el.style.border = '2px solid red';
                } else {
                    el.style.border = '2px solid green';
                    checkArray.push(el.value);
                    $('.createOrder').submit();
                }
            });
            $.each($('#invoice_table .items .select2Custom'), function(index, el) {
                if (el.value == 0 || el.value == "" || el.value == undefined || el.value == null || el.value < 0) {
                    checkArray.push(false);
                    // el.parentElement.style.border = "1px solid red";
                    el.parentElement.style.backgroundColor = "#df6d6d";
                } else {
                    el.parentElement.style.backgroundColor = "#6ddf9d";
                    // el.parentElement.style.border = "2px solid green";
                    checkArray.push(el.value);
                }
            });
            if ($.inArray(false, checkArray) !== -1) {
                console.log('you forget an input');
            } else {
                // $('#createOrder').submit();
            }

        });
        $(document).on('click', '.save_order_invoice', function() {
            console.log('testing where the form is ');
            $(' .createInvoiceOrderForm').submit();
        });
        $(document).on('change', '.order_for', function() {
            let elem = $(this);
            let form = elem.parents('#page_one');
            var data = {};
            data.customerId = $(this).val();
            $.post(root + 'admin/invoices/getCustomerDetailsforInvoice', data, function(d) {
                if (d.result == true) {
                    form.find('#customer_credit').val(d.credits);
                } else {
                    form.find('#customer_credit').val("");
                }
            }, 'json');
        });
        $(document).on('change', '.item_price', function() {
            calcTotal();
        });
        $(document).on('change', '.item_unit', function() {
            calcTotal();
        });
        $(document).on('change', '.select2Custom', function() {
            var elem = $(this);
            var data = {};
            data.id = $(this).val();
            data.warehouse = $('#storage').val();
            data.text = $(this).find('option:selected').text();
            data.quantity_type = $('#quantity_type').val();
            elem.parent().find('input[type="hidden"]').val(data.text);
            //console.log(data);
            $.post(root + 'admin/orders/getItemPrice', data, function(d) {
                console.log(d);
                if (d.id !== undefined) {
                    elem.find('option:selected').val(d.id);
                }
                if (d.price) {
                    elem.parent().parent().find('.item_price').val(d.price);
                    calcTotal();
                }
                if (d.name) {
                    elem.parent().parent().find('.item_desc').val(d.name);
                    calcTotal();
                }
                if (d.price) {
                    elem.parent().parent().find('.in_stock').val(d.instock);
                    calcTotal();
                }

            }, 'json');
        });

        function calcTotal() {
            var full_total = 0;
            $(".item_price").each(function(index) {
                var elem = $(this);
                var price = Number(elem.val());
                var unit = Number(elem.parent().parent().find('.item_unit').val());
                var priceF = price * unit;
                elem.parent().parent().find('.full_price').val(priceF);
                full_total = full_total + priceF;
            });
            var before_vat = full_total / 1.17;
            var vat = full_total - before_vat;
            $('.fill_total_before_vat').val(before_vat.toFixed(2));
            $('.fill_vat').val(vat.toFixed(2));
            $('.fill_total').val(full_total.toFixed(2));
        }
        $('.order_page .excel_exporter').on('click', () => {
            $('#selectCustomer').select2({
                placeholder: 'Search customers',
                selectOnClose: false,
                tags: true,
                dropdownParent: $('#ExcelExportModal'),
                ajax: {
                    dataType: "json",
                    url: root + 'admin/invoices/getCustomersAjax',
                    processResults: function(data) {
                        return {
                            results: data.results
                        }
                    }
                },
                "language": {
                    "noResults": function() {
                        return "<?php echo $this->lang->line('Search_Customers'); ?>";
                    }
                },
                theme: "bootstrap-5"
            });

            $('#selectInvoiceFrom').select2({
                placeholder: 'From Invoice',
                selectOnClose: false,
                tags: true,
                dropdownParent: $('#ExcelExportModal'),
                ajax: {
                    dataType: "json",
                    url: root + 'admin/invoices/getInvoicesAjax',
                    processResults: function(data) {
                        return {
                            results: data.results
                        }
                    }
                },
                "language": {
                    "noResults": function() {
                        return "<?php echo $this->lang->line('Search_Customers'); ?>";
                    }
                },
                theme: "bootstrap-5"
            });

            $('#selectInvoiceTo').select2({
                placeholder: 'To Invoice',
                selectOnClose: false,
                tags: true,
                dropdownParent: $('#ExcelExportModal'),
                ajax: {
                    dataType: "json",
                    url: root + 'admin/invoices/getInvoicesAjax',
                    processResults: function(data) {
                        return {
                            results: data.results
                        }
                    }
                },
                "language": {
                    "noResults": function() {
                        return "<?php echo $this->lang->line('Search_Customers'); ?>";
                    }
                },
                theme: "bootstrap-5"
            });

            // ability_ExcelExportTypes();
            // $('#selectCustomer').prop("disabled", false);
            // $('.ExcelFilteration input[rel-exportby="customer"]').prop('checked', true);
        });
        $(document).on('change', '.order_page .ExcelFilteration input[type="checkbox"]', function() {
            var elem = $(this);
            var checkbox = elem.attr("rel-exportby");
            if (checkbox != 'customer') {
                if ($('#exportByCustomer').prop('checked') == true) {
                    if (checkbox == 'invoice') {
                        $('#exportByDate').prop('checked', false);
                    }
                    if (checkbox == 'date') {
                        $('#exportByInvoice').prop('checked', false);
                    }
                } else {
                    if (checkbox == 'invoice') {
                        $('#exportByDate').prop('checked', false);
                    }
                    if (checkbox == 'date') {
                        $('#exportByInvoice').prop('checked', false);
                    }
                }
            }
        });
        $(document).on('click', '.order_page #buildExcel', function() {
            let ajaxData = {};
            if ($('#exportByCustomer').prop("checked")) {
                ajaxData.customerId = $('#selectCustomer').val();
            }
            if ($('#exportByInvoice').prop("checked")) {
                ajaxData.invoiceFrom = $('#selectInvoiceFrom').val();
                ajaxData.invoiceTo = $('#selectInvoiceTo').val();
            }
            if ($('#exportByDate').prop("checked")) {
                ajaxData.invoiceDateFrom = $('#invoiceDateFrom').val();
                ajaxData.invoiceDateTo = $('#invoiceDateTo').val();
            }

            console.log(ajaxData);
            $.post(root + 'admin/ajax/invoiceExcelReport', ajaxData, function(response) {
                window.location.assign(root + 'assets/' + response);
            }, "json");
        });
        $(document).on('click', '.order_page #dt_basic2 tbody tr', function() {
            let elem = $(this);
            $('#deleteInvoiceButton').css('border-color', "#e9200c");
            $('#viewInvoiceButton').css('border-color', "#27b980");
            $('#dt_basic2 tbody tr').removeClass("selected_row");
            $(this).addClass("selected_row");
        });
        $(document).on('click', '.order_page #out_order', function() {
            var selected_row = $('#dt_basic2 tbody tr.selected_row').attr("rel-order");
            var is_close = $('#dt_basic2 tbody tr.selected_row').attr("rel-close");
            var status = $('#dt_basic2 tbody tr.selected_row').attr("rel-status");
            console.log(is_close);
            if (selected_row !== undefined && is_close == 0) {
                if (status > 1 && status < 4) {
                    let elem = $(this);
                    let data = {};
                    let order_id = $('#dt_basic2 tbody tr.selected_row').attr("rel-order");
                    data.order_id = order_id;
                    $.post(root + 'admin/orders/deliveryData', data, function(d) {
                        if (d.result == true) {
                            if (d.order.order_items) {
                                $('#send_order_table tbody').html("");
                                $('#out_customer').html(d.order.customerName);
                                $('#out_order_id').html(order_id);
                                $('#send_order_table tbody').html("");
                                $('#total_items').val(0);
                                $('#invoice_for').val(d.order.customerName);
                                $('#customer_id').val(d.order.customerId);
                                $('#warehouse_name1').val(d.order.warehouse_id);
                                // $('#customer_id').text(d.order.customerName);
                                $('#warehouse_name').val(d.order.WarehouseName);
                                $('#out_customer_credit').val(d.order.customerCredite);
                                $(d.order.order_items).each(function(index, value) {
                                    var $row = '<tr class="items">';
                                    $row += '<td  colspan="3" style=" width: 100px; "><input class="item_code form-control " name="item[' + value.item_id + '][code]" id="" value="' + value.product_code + ' " rel-product="' + value.product_id + ' "></td>';
                                    $row += '<td><input class="form-control productid" name="item[' + value.item_id + '][product]" id="product" value="' + value.item_name + ' " ></td>';
                                    $row += '<td><input class="form-control units" name="item[' + value.item_id + '][unit]" id="unit" value="' + value.unit + '"></td>';
                                    $row += '<td><input class="form-control unitLeft" name="item[' + value.item_id + '][unitLeft]" id="unitLeft" value="' + value.left_unit + '"rel-left="' + value.left_unit + ' " ></td>';
                                    $row += '<td><input class="form-control unitOut" name="item[' + value.item_id + '][unitOut]" id="unitOut" value="0"></td>';
                                    $row += '<td><input class="form-control in_stock" name="item[' + value.item_id + '][in_stock]" id="in_stock" value="' + value.instock + '" rel-stock="' + value.instock + ' " > </td>';
                                    $row += '</tr>';
                                    $('#send_order_table tbody').append($row);
                                });
                                $('#deliveryModal').modal('show');
                            } else {
                                alert("<?php echo $this->lang->line('Error'); ?>");
                            }
                        }
                    }, 'json');
                } else if (status == 4) {
                    bootbox.alert("This order is completed");
                } else {
                    bootbox.alert("This order is not confirmed yet");
                }
            } else {
                bootbox.alert("Please Select an order from the table below to continue");
            }
        });

        $(document).on('change', ' .unitOut', function() {
            var elem = $(this);
            var newValue = elem.val();
            var row = elem.parent().parent();
            var unit_left = row.find('input.unitLeft');
            var in_stock = row.find('input.in_stock');
            // var productId = Number(elem.parent().parent().find('.item_code').attr('rel-product'));
            unit_left.val(Number(unit_left.attr('rel-left')) - Number(newValue));
            in_stock.val(Number(in_stock.attr('rel-stock')) - Number(newValue));

            // var unit = $(this).val();
            // var stock = Number(elem.parent().parent().find('.in_stock').val());
            // basic_in_stock  = Number(elem.parent().parent().find('.in_stock').val());
            // var deliveryUnit = Number(elem.parent().parent().find('.unitLeft').val());
            // basic_unitLeft  = Number(elem.parent().parent().find('.unitLeft').val());
            // var rest_unit = deliveryUnit - unit;
            // var rest_instock = stock - unit;
            // elem.parent().parent().find('.unitLeft').val(rest_unit);
            // elem.parent().parent().find('.in_stock').val(rest_instock);
            calcAll();
        });
        $(document).ready(function() {
            $(".popup").addClass("animation-reveal");
            $(".popup").css("opacity", "0");
            setTimeout(() => {
                $(".popup").removeClass("animation-reveal");
                $(".popup").css("opacity", "1");
            }, 1000);
        });
        $(function() {
            $(document).on("click", '.label', function() {
                $(".popup").each(function(index) {
                    if ($(this).hasClass('open')) {
                        $(this).removeClass("open");
                        $(this).addClass("close");
                    }
                });
                $(this).parent().removeClass("close");
                $(this).parent().addClass("open");
            });

            $(document).on("click", '.menu-item', function() {
                console.log("test");
                let data = {};
                let elem = $(this);
                data.order_id = $('#dt_basic2 tbody tr.selected_row').attr("rel-order");
                let status = $('#dt_basic2 tbody tr.selected_row').attr("rel-status");
                let text = elem.text();
                data.text = text;
                data.status = elem.attr("rel-action");
                console.log($('#dt_basic2 tbody tr.selected_row').attr("rel-order"));
                console.log(elem.text());
                console.log(elem.attr("rel-action"));
                if (elem.attr("rel-action") !== status) {
                    $.post(root + 'admin/orders/confirmOrder', data, function(d) {
                        if (d.result == true) {
                            elem.parent().parent().addClass("close");
                            elem.parent().parent().text(text);
                            setTimeout(() => {
                                $(this).removeClass("open");
                            }, 400);
                        } else {
                            bootbox.alert("<?php echo $this->lang->line('An_error_occurred'); ?>");
                            elem.parent().parent().addClass("close");
                            setTimeout(() => {
                                $(this).removeClass("open");
                            }, 400);
                        }
                    }, 'json');

                }
            });

            $(document).on("keyup", function(event) {
                if ((event.keyCode || event.which) === 27) {
                    $(".popup").addClass("close");
                    setTimeout(() => {
                        $(".popup").removeClass("open");
                    }, 400);
                }
            });

            $(document).on("click", function(event) {
                if ($(event.target).closest(".label").length === 0) {
                    $(".popup").addClass("close");
                    setTimeout(() => {
                        $(".popup").removeClass("open");
                    }, 400);
                }
            });
        });

        function calcAll() {
            var total_items = 0;
            $('.unitOut').each(function() {
                total_items += Number($(this).val()) || 0;
            });
            $('#total_items').val(total_items);
        }

        function checkEmptyDoms(class_name, hide_id) {
            $(`#${hide_id}`).addClass("d-none");
            if ($(`.${class_name}:visible`).length == 0) {
                $(`#${hide_id}`).removeClass('d-none');

            } else {
                console.log($(`.${class_name}:visible`).length == 0)
                $(`#${hide_id}`).addClass("d-none");
            }
        }
    });
</script>

<style>
    .status-style {
        padding: 5px 13px;
        border-radius: 20px;
        color: white;
        font-weight: bold;
    }

    span.select2-dropdown.select2-dropdown--above {
        z-index: 99999;
    }

    .selected_row {
        background-color: #e3edff;
    }

    .product_card {
        box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
        border-radius: 5px;
        width: 8rem;
        height: 200px;
        overflow-y: auto;
        background-position: center;
        background-size: cover;
        background-repeat: no-repeat;
    }

    .out_of_stock {
        background-color: #ffffffde;
        position: relative;
        top: 0%;
        height: 120rem;
        opacity: 0.8;
        z-index: 999;
        user-select: none;
    }

    .popup {
        position: relative;
        background-color: transparent;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1rem;
        font-weight: 700;
        border: none;
        text-decoration: none;
        border-radius: 8px;
    }

    .popup.animation-reveal {
        animation: reveal 1s cubic-bezier(0.77, 0, 0.175, 1);
    }

    .popup .label {
        border-radius: 8px;
        cursor: pointer;
        padding: 0.8rem 1.5rem;
        width: 150px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .popup .label:hover {
        background-color: #fff;
        color: #000;
        border-radius: 8px;
    }

    .popup .menu {
        position: absolute;
        background-color: #e3edff;
        display: none;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        border-radius: 8px;
        font-size: 1rem;
        padding: 0.8rem 1.5rem;
        width: 150px;
    }

    .popup .menu .menu-item {
        cursor: pointer;
        border-radius: 8px;
        padding: 0.5rem 0rem;
        opacity: 0.8;
        transition: 0.2s;
        color: black;
    }

    .popup .menu .menu-item:hover {
        background-color: #fff;
        color: #000;
        border-radius: 8px;
        opacity: 1;
    }

    .popup .menu .menu-separator {
        border-bottom: 1px solid black;
        margin: 5px 0;
    }

    .popup.open .menu {
        display: flex;
        animation: open-menu-anim 0.4s ease-in-out;
        z-index: 99;
        color: white;
    }

    .popup.close .menu {
        animation: close-menu-anim 0.4s ease-in-out;
    }

    @keyframes reveal {
        0% {
            opacity: 0;
        }

        100% {
            opacity: 1;
        }
    }

    @keyframes open-menu-anim {
        0% {
            transform: scale(0, 0);
        }

        33% {
            transform: scale(0.95, 1.05);
        }

        66% {
            transform: scale(1.05, 0.95);
        }

        100% {
            transform: scale(1, 1);
        }
    }

    @keyframes close-menu-anim {
        0% {
            transform: scale(1, 1);
        }

        33% {
            transform: scale(1.05, 0.95);
        }

        66% {
            transform: scale(0.95, 1.05);
        }

        100% {
            transform: scale(0, 0);
        }
    }

    div#invoice_details {
        height: 240px;
        width: auto;
        overflow-x: hidden;
        overflow-y: auto;
    }
</style>
<style>
    table#dt_basic2 {
        width: 100% !important;
    }
</style>