<?php
include SYSTEM_PATH . 'sync/sync_constant.php';
//
if (isset($_GET['send_data']) && (($_GET['send_data'] == 1 && $permissions->hasPermission('sync_data_to_server')) || ($_GET['send_data'] == 0 && $permissions->hasPermission('sync_data_from_server')))) {
    echo "<iframe src='" . SITE_LINK . "sync/" . ($_GET['send_data'] == 1 ? 'branch__data_to_server.php' : 'branch__data_from_server.php') . "' style='width:100%;min-height:400px;padding:0;margin:0;border:1px solid #000;'/>";
    return;
}
$check_server = CommonFunctions::check_ping_ip(FTP_SERVER);
/* Check if server is offline */
if (!$check_server) {
    ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong><?php print CommonLanguage::getMessage('danger'); ?> : </strong><?php print CommonLanguage::getMessage('server_is_offline'); ?> ! 
        <br>
    </div>
    <?php
    return;
}
if (smc_session()['default_setting']['sync_type'] != 2) {
    ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong><?php print CommonLanguage::getMessage('danger'); ?> : </strong><?php print CommonLanguage::getMessage('this_option_is_available_only_for_branch'); ?>  ! 
        <br>
    </div>
    <?php
    return;
}
?>
<div class="container-fluid container-fixed-lg bg-white form-group">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div id="usercustomerAlert"></div>
            <div class="panel-title"><?php print CommonLanguage::getMessage('get_server_data'); ?></div>

            <div class="clearfix"></div>
        </div>

        <div class="panel-body">
            <?php if ($permissions->hasPermission('get_customers_from_server')) { ?>
                <div class="row m-b-10">
                    <div class="input-group  col-md-4 center-margin pull-left " >
                        <label><?php print CommonLanguage::getMessage('choose_customer'); ?></label>
                        <input class="search_customer full-width" id="search_customer" name="search_customer[]" type="hidden"  />
                    </div>
                    <div class="input-group  col-md-4" >
                        <button class=" btn btn-primary btn-cons m-b-10 pull-right " id="sync_customers" style="padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="pg-form"></i> <span class="bold"><?php print CommonLanguage::getMessage('get_customers'); ?></span></button>
                    </div>

                </div>
            <?php }if ($permissions->hasPermission('sync_data_to_server')) { ?>
                <div class="row m-b-10">
                    <label><?php print CommonLanguage::getMessage('sync_to_server'); ?></label>

                    <div class="input-group  col-md-4" >
                        <a class=" btn btn-primary btn-cons m-b-10  " href="<?php echo SITE_LINK . 'Inventory/get_data_server?send_data=1' ?>" style="padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="pg-form"></i> <span class="bold"><?php print CommonLanguage::getMessage('sync_to_server'); ?></span></a>
                    </div>

                </div>
            <?php }if ($permissions->hasPermission('sync_data_from_server')) { ?>
                <br>
                <div class="row m-b-10">
                    <label><?php print CommonLanguage::getMessage('sync_from_server'); ?></label>

                    <div class="input-group  col-md-4" >
                        <a class=" btn btn-primary btn-cons m-b-10  " href="<?php echo SITE_LINK . 'Inventory/get_data_server?send_data=0' ?>" style="padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="pg-form"></i> <span class="bold"><?php print CommonLanguage::getMessage('sync_from_server'); ?></span></a>
                    </div>

                </div>
            <?php } ?>

        </div>
        <?php echo Forms::ModalLoader('data_server_loader'); ?>

    </div>
</div>


<script>
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            title = <?php echo json_encode(CommonLanguage::getMessage('user_customer_list')); ?>,
            sync_server_url = '<?php echo SERVER_trusto_URL; ?>';

    $(function () {
        build_select2($(".search_customer"), (sync_server_url + "sync/customerSearch"), 1)

        $("#sync_customers").on("click", function () {
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: sync_server_url + 'sync/get_customer_data',
                data: {customer: $("#search_customer").val()},
                complete: function (data, vv) {
                    if (vv == "success") {
                        insert_customer_details(data.responseJSON)
                    }
                }
            })

        })


    });

    function insert_customer_details(ret_data) {

        $.ajax({
            type: 'POST',
            url: 'InventoryAjax/insert_customer_details',
            data: {data: ret_data},
            complete: function (data, vv) {
                if (vv == "success") {
                }
            }
        })


    }
</script>
