<hr/>
<div class="row-fluid">
    <?php
    $data_13 = $this->db->query("SELECT COALESCE(SUM(CASE WHEN status_sync = 0 THEN 1 ELSE 0 END), 0) as belum, COALESCE(SUM(CASE WHEN status_sync = 1 THEN 1 ELSE 0 END), 0) as sudah FROM tabel_sync WHERE id_sync = 13")->row();

    $total_13 = $data_13->sudah + $data_13->belum;
    $persen_13 = round(($data_13->sudah) / ($total_13 != 0 ? $total_13 : 1) * 100);
    ?>
    <div class="span1 text-center">
        <button class="btn btn-danger" id="synct_13" style="padding: 10px; padding-top: 20px"><i class="icon icon-download icon-4x"></i></button>
    </div>
    <div class="span10">
        <p>Data 13: <code id="keterangan_13"><?php echo $data_13->sudah . ' dari ' . $total_13; ?></code></p>
        <div class="progress">
            <div class="bar" style="width: <?php echo $persen_13; ?>%;" id="bar-sukses-13"></div>
            <div class="bar bar-danger" style="width: 0%;" id="bar-gagal-13"></div>
        </div>
        <div id="persen-13" style="font-weight: bold"><?php echo $persen_13; ?>% Selesai</div>
    </div>
    <div class="span1"><div class="pull-right" id="anim_progress_13"></div></div>
    <script>
        $(document).ready(function () {
<?php
if ($persen_13 < 100) {
    ?>
                $('#synct_13').click(function (e) {
                    disabledButton();
    <?php
    $belum1 = $this->db->from('tabel_sync')->where('status_sync', 0)->where('id_sync', 13)->order_by('data_ke')->get()->result();

    $download_13 = '';
    foreach ($belum1 as $v) {
        $download_13 .= $v->data_ke . ',';
    }
    ?>
                    var data_to_download = [<?php echo $download_13; ?>];
                    var no = <?php echo $data_13->sudah; ?>;
                    var jumlah = 0;
                    var gagal = 0;
                    var promises = [];
                    for (var i = 0; i < data_to_download.length; i++) {
                        $('#anim_progress_13').html('<i class="icon icon-spinner icon-spin icon-3x"></i>');
                        var request = $.ajax({
                            type: "POST",
                            url: "<?php echo site_url('admin/download_13'); ?>/" + data_to_download[i],
                            async: true,
                            success: function (result) {
                                no++;
                                if (result === 'TIDAK ADA DATA') {
                                    gagal++;
                                } else {
                                    persen = parseInt(result);
                                    if (persen > jumlah) {
                                        jumlah = persen;
                                        $('#bar-sukses-13').attr('style', 'width: ' + persen + '%;');
                                        $('#persen-13').html(persen + '% Selesai');
                                    }
                                }

                                $('#keterangan_13').html(no + " dari " + <?php echo $total_13 ?> + " (Berhasil: " + (no - gagal) + " Data, Gagal " + gagal + " Data)");
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                gagal++;
                                no++;
                                $('#keterangan_13').html(no + " dari " + <?php echo $total_13 ?> + " (Berhasil: " + (no - gagal) + " Data, Gagal " + gagal + " Data)");
                            }
                        });
                        promises.push(request);
                    }

                    $.when.apply(null, promises).done(function () {
                        if (no === <?php echo $total_13 ?>) {
                            if (gagal === 0) {
                                $('#anim_progress_13').html('<i class="icon icon-check icon-3x text-green"></i>');
                            } else {
                                $('#anim_progress_13').html('<i class="icon icon-warning icon-3x text-red"></i>');
                                $('#keterangan_13').html($('#keterangan_13').html() + " [Refresh Halaman ini untuk Mengulangi (F5)]");
                            }
                        }

                        data_13 = true;
                        if (!data_1) {
                            $('#synct1').click();
                        }
                    });
                });
    <?php
} else {
    ?>
                $('#synct_13').prop("disabled", true);
                $('#anim_progress_13').html('<i class="icon icon-check icon-3x text-green"></i>');
    <?php
}
?>
        });
    </script>
</div>
