<?php
include 'Views/Stock/add_damage_items.php';
?>
<div class="container-fluid container-fixed-lg bg-white" >
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"> <?php print CommonLanguage::getMessage('damaged_items'); ?></div>
        </div>
        <div class="panel-body form-group">
            <div class="row m-t-10">
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('Depot') ?></label>

                    <select required id="Depot_ID" name="Depot_Loc" class="full-width " data-init-plugin="select2">
                        <option value="All"><?php print CommonLanguage::getMessage('All') ?></option>
                        <?php foreach ($all_depots as $Depot): ?>
                            <option value="<?php echo $Depot['Depot_ID']; ?>"><?php echo $Depot['Depot_Name']; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3 ">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>

                    <?php echo Forms::snippet_rangepicker(); ?>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('customer') ?></label>

                    <input id="current_customer_id" type="hidden" class="full-width" name="current_customer_id" >
                </div> 
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Category') ?></label>
                    <input type="hidden" name="category_id" class="full-width " id="category_id" />
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('brand') ?></label>
                    <input type="hidden" name="brand_id" class="full-width " id="brand_id" />
                </div>
                <div class="col-md-3 m-t-25">
                    <button style="color:white !important;" id="Generate_Report" type="button" class="btn bg-complete-darker white" data-toggle="dropdown" aria-expanded="false">
                        <?php print CommonLanguage::getMessage('generate_report'); ?>
                    </button>
                </div>
                <?php
               /* 
                we don't use it.  all related functions for this page are disabled
               if ($permissions->hasPermission('add_damage_in_report')) {
                    echo Forms::FloatingAddButton('4c0870', '#add_Damage', CommonLanguage::getMessage('add'));
                }*/
                ?> 
            </div>

            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered stock" id="tableWithSearch" >
                            <thead>
                                <tr>
                                    <th class="text-center"><?php print CommonLanguage::getMessage('Item'); ?></th>
                                    <th class="text-center"  ><?php print CommonLanguage::getMessage('Depot'); ?></th>
                                    <th class="text-center" ><?php print CommonLanguage::getMessage('reason'); ?></th>
                                    <th class="text-center no-print" ><?php print CommonLanguage::getMessage('Date'); ?></th>
                                    <th class="text-center no-print" ><?php print CommonLanguage::getMessage('customer'); ?></th>
                                    <th class="text-center"  ><?php print CommonLanguage::getMessage('quantity'); ?></th>
                                    <?php if ($permissions->hasPermission('show_cost_column_damaged')) { ?>
                                        <th class="text-center"  ><?php print CommonLanguage::getMessage('total_costs'); ?></th>
                                    <?php } ?>
                                    <!--<th class="text-center"  ><?php print CommonLanguage::getMessage('Action'); ?></th>-->

                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                            <tfoot>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <?php if ($permissions->hasPermission('show_cost_column_damaged')) { ?>
                                <th><div class="text-center"><span id="total"></span>&nbsp;<span id="currency_symbol"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                                    <?php } ?>
                            <!--<th></th>-->
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('damaged_items')); ?>,
            today = <?php echo json_encode($today); ?>;
    $(function () {
        build_select2($("#category_id"), "xhr/catSearch");
        build_select2($("#brand_id"), "xhr/CommonSearch?col_id=brand_id&col_name=brand_name&table=tbl_brand_products")

        build_select2($('#current_customer_id'), 'xhr/customerSearch', false, '', {placeholder: '<?php echo CommonLanguage::getMessage('choose_customer'); ?>'});
        build_damage_table()
        rangepicker_js();
        $("#Generate_Report").on("click", function () {
            build_damage_table()
        })

        $("#tableWithSearch").on("click", ".delete", function () {
            var entry_value = $(this).attr('data-id');
            var data = {
                id: entry_value
            };
            IncludeDeleteModal(common_delete_modal, data, function () {
                build_damage_table();
            }, "StockAjax/delete_damage_ajax");
        })
    })
    function build_damage_table() {
        var currentData = {
            From_Date: window.start ? window.start : today,
            To_Date: window.end ? window.end : today,
            cat_id: $("#Material_Category_ID").val(),
            depot_id: $("#Depot_ID").val(),
            customer_id: $('#current_customer_id').val(),
            brand_id: $("#brand_id").val(),
            category_id: $("#category_id").val(),
        }
        $.ajax({
            url: "StockAjax/get_total_damaged_cost",
            data: currentData,
            dataType: "json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var response = v1.responseJSON;
                    RebuildDataTable(title, [0, 1, 2, 3, 4], 'StockAjax/DamagedItemsRequest', currentData);
                    $("#total").html(response);
                }
            }
        })
    }
</script>
