<?php
if ($permissions->hasPermission('newVendor')) {
    include("Views/Inventory/vendor_modal.php");
}
include("Views/Inventory/disable_enable_modal.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('vendor_list'); ?></div>
            <?php if (smc_session()['default_setting']['sync_type'] != 2) {

                echo Forms::FloatingAddButton('4c0870', '#supplier_modal', CommonLanguage::getMessage('add'), 'new_vendor');
            } ?>
            <div class="row">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('status'); ?></label>
                    <select id="vendor_status" name="vendor_status" class="full-width" data-init-plugin="select2">
                        <option value=""><?php echo CommonLanguage::getMessage('All'); ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('enabled'); ?></option>
                        <option value="0"><?php echo CommonLanguage::getMessage('disabled'); ?></option>
                    </select>
                </div>
                <div class="col-md-3 <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id" value="<?php echo $permissions->hasPermission('Branches') ? '':smc_session()['user_accounts']['branch_id'] ?>" data-select-name="<?php echo $permissions->hasPermission('Branches') ? '':smc_session()['user_accounts']['branch_name']; ?>">
                </div>

            </div>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered inventory " id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th class="text-center" style="width:5%"><?php print CommonLanguage::getMessage('Code'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('Name'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('branch_name'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('company'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('total_invoices'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('balance'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('mobile_phone'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('address'); ?></th>
                                    <th class="text-center" style="width:15%"><?php print CommonLanguage::getMessage('status'); ?></th>
                                    <th class="text-center no-print" style="width:10%"><?php print CommonLanguage::getMessage('Action'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-md" id="DeleteFromList">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header 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 bold"><?php echo CommonLanguage::getMessage('Confirm_Delete'); ?></h5>
                <div id="Delete_modalAlert"><span></span></div>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12 ">
                        <p><?php print CommonLanguage::getMessage('About_Delete'); ?>: <strong class="Name"></strong></p>
                        <p><?php print CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                    </div>
                </div>
            </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-danger" id="Customer_Delete" style="width:auto"><?php print CommonLanguage::getMessage('delete'); ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
        is_modal_shown = <?php echo json_encode($is_modal_shown); ?>,
        title = <?php echo json_encode(CommonLanguage::getMessage('vendor_list')); ?>;
    /*To delete a customer or vendor*/
    var Current_ToDelete;
    $(function() {
        if (is_modal_shown == 1) {
            $("#supplier_modal").modal("show")
        }
        RebuildDataTable(title, [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {
            IsVendor: true,
            status_id: $('#vendor_status').val(),
            search_branch_id: $('#search_branch_id').val()
        });

        build_select2($("#search_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");

        $("#DeleteCustomer_modalAlert").hide()
        $("#tableWithSearch ").on("click", ".Action .Delete", function() {
            //alert("OK")
            var me = $(this).parents("tr");
            var name = me.find("td:eq(1)").text();
            $("#DeleteFromList").find(".Name").text(name).end().modal("show")
            Current_ToDelete = $(this).parents("tr").find('.Action').attr('data-id')
        });
        $('#tableWithSearch').on('click', '.disable_enable_item', function() {
            var status_id = $(this).attr('data-id'),
                target_item_value = $(this).parents('div.Action').attr('data-id');
            $('#current_status_id').val(status_id == 1 ? 0 : 1);
            $('#target_item_value').val(target_item_value);
            $('#target_item').val('stock_suppliers');
            $('#target_item_id').val('Supplier_ID');
            status_id == 0 ? $('#enable-disable-modal .add-label,#enable-disable-modal .about_enable_item').removeClass('hidden') : $('#enable-disable-modal .edit-label,#enable-disable-modal .about_disable_item').removeClass('hidden');

        })
        $("#Customer_Delete").on("click", function() {
            $.ajax({
                url: 'InventoryAjax/DeleteVendor',
                data: {
                    Supplier_ID: Current_ToDelete
                },
                complete: function(data, vv) {
                    if (vv == "success") {
                        if (vv == "success") {
                            if (data.responseText == 'OK') {
                                AddAlert("#Delete_modalAlert", Submitted_Successfully, 'success');
                                RebuildDataTable(title, [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {
                                    IsVendor: true,
                                    status_id: $('#vendor_status').val()
                                });
                            } else {
                                AddAlert("#Delete_modalAlert", data.responseText, 'danger');
                            }
                            hideModalAlert('DeleteFromList', 'Delete_modalAlert');
                        }
                    }
                }
            });
        });
        $('#vendor_status,#search_branch_id').on('change', function() {
            RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {
                IsVendor: true,
                status_id: $('#vendor_status').val(),
                search_branch_id: $('#search_branch_id').val()
            });
        });
    });
</script>
