<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('pump_list'); ?></div>
            <?php
            if ($permissions->hasPermission('add_pump')) {
                echo Forms::FloatingAddButton('4c0870', '#add-edit-pump-modal', CommonLanguage::getMessage('add'));
            }
            ?>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('pump_name'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('product_name'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('status'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-md " data-backdrop="static" id="add-edit-pump-modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm"  >
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('add_pump'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_pump'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('pump_alert'); ?></div>
                <form id="pumpForm">
                    <input type="hidden" name="pump_id" id="pump_id" />
                    <div class="row m-t-10">
                        <div class=" col-md-12  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('pump_name'); ?>
                                <span class="red-star">*</span>
                            </label>
                            <input type="text" class="form-control" autocomplete="off" id="pump_name" name="pump_name" required/>
                        </div>
                    </div>
                    <div class="row m-t-10">
                        <div class=" col-md-12  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('product_name'); ?>
                                <span class="red-star">*</span>
                            </label>
                            <input type="hidden" class="full-width" id="Product_ID" name="Product_ID" required/>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn btn-primary white" id="submit_pump"><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
            <?php echo Forms::ModalLoader('pumpLoader'); ?>
        </div>
    </div>
</div>
<div class="modal fade" id="enable-disable-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('enable_pump'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('disable_pump'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('enable_disable_alert'); ?></div>
                <div class="row">
                    <input  type="hidden" id="pump_status_id"/>
                    <input  type="hidden" id="enabled_disabled_pump_id"/>
                    <div class="col-md-12 m-t-10 form-group">
                        <p class="about_enable_pump"><?php echo CommonLanguage::getMessage('about_enable_pump'); ?></p>
                        <p class="about_disable_pump"><?php echo CommonLanguage::getMessage('about_disable_pump'); ?></p>
                        <p><?php echo CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                    </div>
                </div>
                
                <?php echo Forms::ModalLoader('enableDisableLoader'); ?>
            </div>
            <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                    <button class="btn btn-primary text-white" id="submit_enable_disable"><?php print CommonLanguage::getMessage('Save'); ?></button>
                </div>
        </div>
    </div>
</div>
<script>
    $(function(){
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], "InventoryAjax/pump_ajax");
        $(".floatingContainer").on("click", function () {
            $('#pump_id').val(0);
            build_select2($("#Product_ID"), "xhr/ProductSearch?Status_ID=1");
            $('#add-edit-pump-modal .add-label').removeClass('hidden');
        });
        $('#tableWithSearch').on('click', '.delete_pump', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id');
            IncludeDeleteModal(common_delete_modal, {pump_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], "InventoryAjax/pump_ajax")
            }, "InventoryAjax/deletePump");
        });
        $('#tableWithSearch').on('click', '.edit_pump', function () {
            $('#add-edit-pump-modal .edit-label').removeClass('hidden');
            $('#pump_id').val($(this).parents('div.Action').attr('data-id'));
            $('#Product_ID').val($(this).attr('data-id'))
            $('#Product_ID').attr('data-select-name',$('td:eq(1)',$(this).parents('tr')).html())
            build_select2($("#Product_ID"), "xhr/ProductSearch?Status_ID=1&is_edited="+$(this).attr('data-id'));
            $('#pump_name').val($('td:eq(0)',$(this).parents('tr')).html());
        });
        $("#tableWithSearch").on('click', '.enable_disable_pump', function () {
            $('#enable-disable-modal .about_enable_pump,#enable-disable-modal .about_disable_pump').addClass('hidden');
            var status_id = $(this).attr('data-id'), pump_id = $(this).parents('div.Action').attr('data-id');
            $('#pump_status_id').val(status_id);
            $('#enabled_disabled_pump_id').val(pump_id);
            status_id == 0 ? $('#enable-disable-modal .add-label,#enable-disable-modal .about_enable_pump').removeClass('hidden') : $('#enable-disable-modal .edit-label,#enable-disable-modal .about_disable_pump').removeClass('hidden');
        });
        $("#submit_pump").on("click", function () {
            if (!$("#pumpForm").valid())
                return;
            var Data = new FormData(document.getElementById("pumpForm"));
            $("#pumpLoader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitPump",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2], "InventoryAjax/pump_ajax");
                            hideModalAlert('add-edit-pump-modal', 'pump_alert');
                        }
                        AddAlert("#pump_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#pumpLoader").addClass('hidden');
                }
            });
        });
        $("#submit_enable_disable").on("click", function () {
            $("#enableDisableLoader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/enableDisablePump",
                method: "POST",
                data: {status_id: $('#pump_status_id').val(), pump_id: $('#enabled_disabled_pump_id').val()},
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2], "InventoryAjax/pump_ajax");
                            hideModalAlert('enable-disable-modal', 'enable_disable_alert');
                        }
                        AddAlert("#enable_disable_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#enableDisableLoader").addClass('hidden');
                }
            });
        });
    });
</script>