<?php $this->load->view('meta') ?>
<div class="wrapper">
    <?php $this->load->view('sidebar') ?>
    <?php $this->load->view('navbar') ?>
    <style>
        table th {
            white-space: nowrap;
        }
    </style>

    <div class="container-fluid">

        <!-- DataTales Example -->
        <div class="card shadow mb-4">
            <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                <h6 class="m-0 font-weight-bold text-primary"><?php echo $title ?></h6>
                <div class="dropdown no-arrow">
                    <button class="btn btn-sm btn-success" id="btnAdd" onclick="tambah()"><span class="fa fa-plus"></span> Tambah Data</button>
                    <!--<button class="btn btn-sm btn-primary" id="btnReport" onclick="report()"><span class="fa fa-file"></span> Laporan</button> -->
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered datatable" cellspacing="0">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th style="text-align:center;">Tanggal</th>
                                <th style="text-align:center;">Shift</th>
                                <th style="text-align:center;">Stockpile</th>
                                <th style="text-align:center;">jumlah</th>
                                <th style="text-align:center;">Remark</th>
                                <th style="text-align:center;">Keterangan</th>
                                <th style="text-align:center;">Foto</th>
                                <th style="text-align:center;">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $no = 1;
                            foreach ($form_batubara_produksi as $form_batubara_produksi) {
                            ?>
                                <tr>
                                    <td><?php echo $no; ?></td>
                                    <td><?php echo tgl_indo($form_batubara_produksi->tanggal_batubara_produksi); ?></td>
                                    <td align="center"><?php echo $form_batubara_produksi->shift_batubara_produksi; ?></td>
                                    <td align="center"><?php echo $form_batubara_produksi->nama_stockpile_batubara_masuk; ?></td>
                                    <td align="right"><?php echo round($form_batubara_produksi->jumlah_batubara_produksi, 3) ?></td>
                                    <td align="center"><?php echo $form_batubara_produksi->nama_checker; ?></td>
                                    <td><?php echo $form_batubara_produksi->keterangan_batubara_produksi; ?></td>
                                    <td style="text-align:center;">
                                        <?php if (!($form_batubara_produksi->file_upload == "")) {
                                        ?>
                                            <a href="<?php echo base_url('assets/images/batubara_produksi/') . $form_batubara_produksi->file_upload ?>" target="_blank"><i class="fa fa-fw fa-search-plus"></i></a>
                                        <?php
                                        }
                                        ?>
                                    </td>
                                    <td align="center">

                                        <button type="button" id="edit<?= encrypt($form_batubara_produksi->id_batubara_produksi) ?>" class="btn btn-warning btn-sm" data-toggle="tooltip" title="Ubah" onclick="ubah('<?= encrypt($form_batubara_produksi->id_batubara_produksi) ?>')"><i class="fas fa-fw fa-pencil-alt"></i></button>
                                        <button type="button" class="btn btn-danger btn-sm" data-toggle="tooltip" title="Hapus" onclick="hapus('<?= encrypt($form_batubara_produksi->id_batubara_produksi) ?>')"><i class="fas fa-fw fa-trash-alt"></i></button>
                                    </td>
                                </tr>
                            <?php
                                $no++;
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>


    <!-- Modal -->
    <div id="modalAdd" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Tambah <?php echo $title ?></h6>
                    <div class="dropdown no-arrow">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                </div>
                <form id="formAdd" class="form-horizontal" method="post" enctype="multipart/form-data">
                    <div class="modal-body">
                        <div class="container-fluid">

                            <div class="row">
                                <div class="col-md-3">Tanggal / Jam</div>
                                <div class="col-md-4">
                                    <input type="text" name="tanggal_batubara_produksi" class="form-control datepicker" value="<?php echo date('d-m-Y') ?>" placeholder="Tanggal produksi" required />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="text" name="jam_batubara_produksi" class="form-control timepicker" value="<?php echo date('H:i') ?>" placeholder="Jam produksi" required />
                                    <br />
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-3">Shift / Checker</div>
                                <div class="col-md-4">
                                    <select name="shift_batubara_produksi" class="form-control" required>
                                        <option value="">--Pilih--</option>
                                        <option>Siang</option>
                                        <option>Malam</option>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_checker" class="form-control" required>
                                        <option value="">--Nama Checker--</option>
                                        <?php
                                        foreach ($checker as $index => $checker_add) {
                                            echo "<option value='" . $checker_add->id_checker . "'>$checker_add->nama_checker</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Asal / Jumlah</div>
                                <div class="col-md-4">
                                    <select name="id_stockpile_batubara_masuk" class="form-control" required>
                                        <option value="">--Asal Batubara--</option>
                                        <?php
                                        foreach ($stockpile_batubara_masuk as $index => $stockpile_batubara_masuk_add) {
                                            echo "<option value='" . $stockpile_batubara_masuk_add->id_stockpile_batubara_masuk . "'>$stockpile_batubara_masuk_add->name_site | $stockpile_batubara_masuk_add->nama_stockpile_batubara_masuk</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="number" name="jumlah_batubara_produksi" class="form-control" placeholder="Jumlah Tonase" step=".001" required />
                                    <br />
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-3">Keterangan </div>
                                <div class="col-md-8">
                                    <input type="text" name="keterangan_batubara_produksi" class="form-control" placeholder="Keterangan" />
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Foto</div>
                                <div class="col-md-8">
                                    <input type="file" name="file_upload" id="file_upload_add" class="form-control" onchange="return validasiFile()" />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3"></div>
                                <div class="col-md-8">
                                    <div id="pratinjauGambar"></div>
                                    <br />
                                </div>
                                <br />
                            </div>



                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitAdd" type="submit" class="btn btn-success">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <div id="modalEdit" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Edit <?php echo $title ?></h6>
                    <div class="dropdown no-arrow">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                </div>
                <form id="formEdit" class="form-horizontal" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="id_batubara_produksi" id="id_batubara_produksi" class="form-control" required />
                    <div class="modal-body">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-3">Tanggal / Jam</div>
                                <div class="col-md-4">
                                    <input type="text" name="tanggal_batubara_produksi" class="form-control datepicker" value="<?php echo date('d-m-Y') ?>" placeholder="Tanggal produksi" required />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="text" name="jam_batubara_produksi" class="form-control timepicker" value="<?php echo date('H:i') ?>" placeholder="Jam produksi" required />
                                    <br />
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-3">Shift / Checker</div>
                                <div class="col-md-4">
                                    <select name="shift_batubara_produksi" class="form-control" required>
                                        <option value="">--Pilih--</option>
                                        <option>Siang</option>
                                        <option>Malam</option>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_checker" class="form-control" required>
                                        <option value="">--Nama Checker--</option>
                                        <?php
                                        foreach ($checker as $index => $checker_add) {
                                            echo "<option value='" . $checker_add->id_checker . "'>$checker_add->nama_checker</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Asal / Jumlah</div>
                                <div class="col-md-4">
                                    <select name="id_stockpile_batubara_masuk" class="form-control" required>
                                        <option value="">--Asal Batubara--</option>
                                        <?php
                                        foreach ($stockpile_batubara_masuk as $index => $stockpile_batubara_masuk_add) {
                                            echo "<option value='" . $stockpile_batubara_masuk_add->id_stockpile_batubara_masuk . "'>$stockpile_batubara_masuk_add->name_site | $stockpile_batubara_masuk_add->nama_stockpile_batubara_masuk</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="number" name="jumlah_batubara_produksi" class="form-control" placeholder="Jumlah Tonase" step=".001" required />
                                    <br />
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-3">Keterangan </div>
                                <div class="col-md-8">
                                    <input type="text" name="keterangan_batubara_produksi" class="form-control" placeholder="Keterangan" />
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Foto</div>
                                <div class="col-md-8">
                                    <input type="file" name="file_upload" id="file_upload_edit" class="form-control" onchange="return validasiFileEdit()" />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3"></div>
                                <div class="col-md-8">
                                    <div id="pratinjauGambarEdit"></div>
                                    <br />
                                </div>
                                <br />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitEdit" type="submit" class="btn btn-success">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div id="modalReport" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Laporan Batubara produksi</h6>
                    <div class="dropdown no-arrow">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                </div>
                <form id="formReport" class="form-horizontal" method="post" target="_blank" action="<?php echo base_url('form_batubara_produksi/report') ?>" enctype="multipart/form-data">
                    <div class="modal-body">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-3">Tanggal</div>
                                <div class="col-md-4">
                                    <input type="text" name="tanggal_report_awal" class="form-control datepicker" value="<?php echo date('d-m-Y') ?>" placeholder="Tanggal Awal" required />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="text" name="tanggal_report_akhir" class="form-control datepicker" value="<?php echo date('d-m-Y') ?>" placeholder="Tanggal Akhir" required />
                                    <br />
                                </div>
                            </div>

                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitReport" type="submit" class="btn btn-success">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <?php $this->load->view('footer') ?>
</div><!-- ./wrapper -->
<?php $this->load->view('js') ?>
<!-- DATA TABLES-->
<script>
    function tambah() {
        $('#btnAdd').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
        $('#formAdd')[0].reset();

        $('#btnAdd').html('<span class="fa fa-plus"></span> Tambah Data');
        $('#modalAdd').modal('show');
    }

    function report() {
        $('#btnReport').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
        $('#formReport')[0].reset();

        $('#btnReport').html('<span class="fa fa-file"></span> Laporan');
        $('#modalReport').modal('show');
    }

    $('#formAdd').submit(function(e) {
        e.preventDefault();
        $('#btnSubmitAdd').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
        $('#btnSubmitAdd').attr('disabled', true);
        var formData = new FormData($(this)[0]);
        $.ajax({
            type: 'post',
            enctype: 'multipart/form-data',
            url: '<?php echo base_url(); ?>form_batubara_produksi/add',
            data: formData,
            async: true,
            cache: false,
            contentType: false,
            processData: false
        }).done(function(res) {
            $('#btnSubmitAdd').html('Submit');
            $('#btnSubmitAdd').removeAttr('disabled');
            if (res == '1') {
                $("#modalAdd").modal('toggle');
                swal({
                    title: 'Data Behasil Ditambah',
                    text: 'Data berhasil disimpan ke database.',
                    type: 'success',
                    closeOnConfirm: false
                }, function(isConfirm) {
                    if (isConfirm) {
                        location.reload();
                    }
                })
            } else {
                swal("Kesalahan!", "produksikan data dengan lengkap!", 'error');
            }
        }).fail(function(res) {
            console.log(res);
        });
    });

    function ubah(id) {
        $('#edit' + id).html('<i class="fas fa-spinner fa-spin"></i>');
        $('#formEdit')[0].reset();
        $.ajax({
            type: 'post',
            url: '<?php echo base_url(); ?>form_batubara_produksi/get_data',
            data: 'id=' + id
        }).done(function(res) {
            var data = $.parseJSON(res);
            $('#tanggal_batubara_produksi').datepicker("setDate", convert_date_indo(data.tanggal_batubara_produksi));
            $('#jam_batubara_produksi').val(data.jam_batubara_produksi.substr(0, 5));
            $('#shift_batubara_produksi').val(data.shift_batubara_produksi);
            $('#id_asal_batubara_produksi').val(data.id_asal_batubara_produksi);
            $('#jumlah_batubara_produksi').val(Math.round(data.jumlah_batubara_produksi * 1000) / 1000);
            $('#id_checker_batubara_kirim').val(id);
            $('#keterangan_batubara_produksi').val(data.keterangan_batubara_produksi);
            $('#id_batubara_produksi').val(id);

            document.getElementById('pratinjauGambarEdit').innerHTML = '<img src="assets/images/batubara_produksi/' + data.file_upload + '" width="100%"/>';

            $('#edit' + id).html('<i class="fas fa-fw fa-pencil-alt"></i>');
            $('#modalEdit').modal('show');

        }).fail(function(res) {
            console.log(res);
        });
    }


    $('#formEdit').submit(function(e) {
        e.preventDefault();
        var formData = new FormData($(this)[0]);
        $('#btnSubmitEdit').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
        $('#btnSubmitEdit').attr('disabled', true);
        $.ajax({
            type: 'post',
            url: '<?php echo base_url(); ?>form_batubara_produksi/update',
            data: formData,
            async: true,
            cache: false,
            contentType: false,
            processData: false
        }).done(function(res) {
            $('#btnSubmitEdit').html('Submit');
            $('#btnSubmitEdit').removeAttr('disabled');
            if (res == '1') {
                $("#modalEdit").modal('toggle');
                swal({
                    title: 'Data Behasil Diubah',
                    text: 'Data berhasil disimpan ke database.',
                    type: 'success',
                    closeOnConfirm: false
                }, function(isConfirm) {
                    if (isConfirm) {
                        location.reload();
                    }
                })
            } else {
                swal("Kesalahan!", "produksikan data dengan lengkap", 'error');
            }
        }).fail(function(res) {
            console.log(res);
        });
    });


    function hapus(code) {
        swal({
                title: "Apakah anda yakin?",
                text: "Data akan dihapus permanen dari Database!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#EF5350",
                confirmButtonText: "Hapus",
                cancelButtonText: "Batal",
                closeOnConfirm: false,
                showLoaderOnConfirm: true
            },
            function() {
                $.ajax({
                    type: 'post',
                    url: '<?php echo base_url(); ?>form_batubara_produksi/delete',
                    data: {
                        submit: true,
                        id: code
                    }
                }).done(function() {
                    swal({
                            title: "Berhasil",
                            text: "Data Berhasil Dihapus",
                            type: "success",
                            showCancelButton: false,
                            closeOnConfirm: true
                        },
                        function(isConfirm) {
                            if (isConfirm) {
                                location.reload();
                            }
                        });
                });
            }
        );
    }
</script>
<script type="text/javascript">
    function validasiFile() {
        var inputFile = document.getElementById('file_upload_add');
        var pathFile = inputFile.value;
        var ekstensiOk = /(\.jpg|\.jpeg|\.png|\.gif)$/i;
        if (!ekstensiOk.exec(pathFile)) {
            alert('Silakan upload file yang memiliki ekstensi .jpeg/.jpg/.png/.gif');
            inputFile.value = '';
            return false;
        } else {
            //Pratinjau gambar
            if (inputFile.files && inputFile.files[0]) {
                var reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('pratinjauGambar').innerHTML = '<img src="' + e.target.result + '" width="100%"/>';
                };
                reader.readAsDataURL(inputFile.files[0]);
            }
        }
    }


    function validasiFileEdit() {
        var inputFile = document.getElementById('file_upload_edit');
        var pathFile = inputFile.value;
        var ekstensiOk = /(\.jpg|\.jpeg|\.png|\.gif)$/i;
        if (!ekstensiOk.exec(pathFile)) {
            alert('Silakan upload file yang memiliki ekstensi .jpeg/.jpg/.png/.gif');
            inputFile.value = '';
            return false;
        } else {
            //Pratinjau gambar
            if (inputFile.files && inputFile.files[0]) {
                var reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('pratinjauGambarEdit').innerHTML = '<img src="' + e.target.result + '" width="100%"/>';
                };
                reader.readAsDataURL(inputFile.files[0]);
            }
        }
    }
</script>
</body>

</html>