<?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>
                    <button class="btn btn-sm btn-primary" id="btnReport_kode" onclick="report_kode()"><span class="fa fa-file"></span> Kode Kirim</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>Approve</th>
                                <th>Tanggal</th>
                                <th>No DT</th>
                                <th>No SK</th>
                                <th>Kode Kirim</th>
                                <th>Asal</th>
                                <th>Tujuan</th>
                                <th>Jam</th>
                                <th>EDP SP</th>
                                <th>Jam Terima</th>
                                <th>EDP Jetty</th>
                                <th>Bruto</th>
                                <th>Tarra</th>
                                <th>Netto</th>
                                <th>Keterangan</th>
                                <th>Foto</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $no = 1;
                            foreach ($form_batubara_kirim as $form_batubara_kirim) {
                            ?>
                                <tr>
                                    <td><?php echo $no; ?></td>
                                    <td class="text-center">
                                        <?php if ($form_batubara_kirim->approve == 1) { ?>
                                            Diterima
                                        <?php } else { ?>
                                            <button type="button" class="btn btn-warning btn-sm" data-toggle="tooltip" title="DIterima" onclick="diterima('<?= encrypt($form_batubara_kirim->id_batubara_kirim) ?>')"><i class="fas fa-fw fa-check"></i></button>
                                            Belum Diterima
                                        <?php } ?>
                                    </td>
                                    <td><?php echo tgl_indo($form_batubara_kirim->tanggal_batubara_kirim); ?></td>
                                    <td align="center"><?php echo $form_batubara_kirim->unit_batubara_kirim; ?></td>
                                    <td align="center"><?php echo $form_batubara_kirim->nomor_sj_batubara_kirim; ?></td>
                                    <td><?php echo $form_batubara_kirim->nama_kode_batubara_kirim; ?></td>
                                    <td align="center"><?php echo $form_batubara_kirim->nama_stockpile_batubara_masuk; ?></td>
                                    <td align="center"><?php echo $form_batubara_kirim->nama_tujuan_batubara_kirim; ?></td>
                                    <td><?php echo substr($form_batubara_kirim->jam_batubara_kirim, 0, 5); ?></td>
                                    <td><?php echo $form_batubara_kirim->nama_checker; ?></td>
                                    <td><?php echo substr($form_batubara_kirim->jam_terima, 0, 5); ?></td>
                                    <td><?php echo $form_batubara_kirim->nama_checker_batubara_kirim; ?></td>
                                    <td align="right"><?php echo round($form_batubara_kirim->bruto_batubara_kirim, 3); ?></td>
                                    <td align="right"><?php echo round($form_batubara_kirim->tarra_batubara_kirim, 3); ?></td>
                                    <td align="right"><?php echo round($form_batubara_kirim->bruto_batubara_kirim - $form_batubara_kirim->tarra_batubara_kirim, 3); ?></td>
                                    <td><?php echo $form_batubara_kirim->keterangan_batubara_kirim; ?></td>
                                    <td style="text-align:center;">
                                        <?php if (!($form_batubara_kirim->file_upload == "")) {
                                        ?>
                                            <a href="<?php echo base_url('assets/images/batubara_kirim/') . $form_batubara_kirim->file_upload ?>" target="_blank"><i class="fa fa-fw fa-search-plus"></i></a>
                                        <?php
                                        }
                                        ?>
                                    </td>
                                    <td class="text-center">

                                        <a href="<?php echo base_url('form_batubara_kirim/cetak/') . encrypt($form_batubara_kirim->id_batubara_kirim); ?>" target="_blank"><button type="button" class="btn btn-sm btn-success" title="Download"><i class="fa fa-fw fa-print"></i></button></a>

                                        <!--<button type="button" id="edit<?= encrypt($form_batubara_kirim->id_batubara_kirim) ?>" class="btn btn-warning btn-sm" data-toggle="tooltip" title="Ubah" onclick="ubah('<?= encrypt($form_batubara_kirim->id_batubara_kirim) ?>')"><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_kirim->id_batubara_kirim) ?>')"><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">No SJ </div>
                                <div class="col-md-8">
                                    <input type="text" name="nomor_sj_batubara_kirim" class="form-control" placeholder="No SJ" />
                                    <br />
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-3">Shift / No Polisi</div>
                                <div class="col-md-4">
                                    <select name="shift_batubara_kirim" class="form-control" required>
                                        <option value="">--Pilih--</option>
                                        <option>Siang</option>
                                        <option>Malam</option>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="text" name="nomor_polisi_batubara_kirim" class="form-control" placeholder="Nomor Polisi" />
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Nomor DT / Kode Kirim</div>
                                <div class="col-md-4">
                                    <input type="text" name="unit_batubara_kirim" class="form-control" placeholder="No Unit DT" />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_kode_batubara_kirim" class="form-control" required>
                                        <option value="">--Pilih Kode Kirim--</option>
                                        <?php
                                        foreach ($kode_batubara_kirim as $index => $kode_batubara_kirim_add) {
                                            echo "<option value='" . $kode_batubara_kirim_add->id_kode_batubara_kirim . "'>$kode_batubara_kirim_add->nama_kode_batubara_kirim</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Dari / Tujuan</div>
                                <div class="col-md-4">
                                    <select name="id_asal_batubara_kirim" class="form-control" required>
                                        <option value="">--Asal Batubara--</option>
                                        <?php
                                        foreach ($stockpile_batubara_masuk as $index => $asal_batubara_kirim_add) {
                                            echo "<option value='" . $asal_batubara_kirim_add->id_stockpile_batubara_masuk . "'>$asal_batubara_kirim_add->name_site | $asal_batubara_kirim_add->nama_stockpile_batubara_masuk</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_tujuan_batubara_kirim" class="form-control" required>
                                        <option value="">--Pilih Tujuan--</option>
                                        <?php
                                        foreach ($tujuan_batubara_kirim as $index => $tujuan_batubara_kirim_add) {
                                            echo "<option value='" . $tujuan_batubara_kirim_add->id_tujuan_batubara_kirim . "'>$tujuan_batubara_kirim_add->nama_tujuan_batubara_kirim</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Bruto / Tarra</div>
                                <div class="col-md-4">
                                    <input type="number" name="bruto_batubara_kirim" class="form-control" placeholder="Bruto" step=".001" required />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="number" name="tarra_batubara_kirim" class="form-control" placeholder="Berat Kosong" step=".001" required />
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Tanggal / Jam Kirim</div>
                                <div class="col-md-4">
                                    <input type="text" name="tanggal_batubara_kirim" class="form-control datepicker" value="<?php echo date('d-m-Y') ?>" placeholder="Tanggal Kirim" required />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="text" name="jam_batubara_kirim" class="form-control timepicker" value="<?php echo date('H:i') ?>" placeholder="Jam Kirim" required />
                                    <br />
                                </div>
                            </div>

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

                            <div class="row">
                                <div class="col-md-3">EDP Kirim / Terima </div>
                                <div class="col-md-4">
                                    <select name="id_checker" class="form-control" required>
                                        <option value="">--Nama EDP Kirim--</option>
                                        <?php
                                        foreach ($checker as $index => $checker_add) {
                                            echo "<option value='" . $checker_add->id_checker . "'>$checker_add->nama_checker</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_checker_batubara_kirim" class="form-control" required>
                                        <option value="">--Nama EDP Terima--</option>
                                        <?php
                                        foreach ($checker_batubara_kirim as $index => $checker_batubara_kirim_add) {
                                            echo "<option value='" . $checker_batubara_kirim_add->id_checker_batubara_kirim . "'>$checker_batubara_kirim_add->nama_checker_batubara_kirim</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Keterangan </div>
                                <div class="col-md-8">
                                    <input type="text" name="keterangan_batubara_kirim" 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_kirim" id="id_batubara_kirim" class="form-control" required />
                    <div class="modal-body">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-3">No SJ </div>
                                <div class="col-md-8">
                                    <input type="text" name="nomor_sj_batubara_kirim" class="form-control" placeholder="No SJ" />
                                    <br />
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-3">Shift / No Polisi</div>
                                <div class="col-md-4">
                                    <select name="shift_batubara_kirim" class="form-control" required>
                                        <option value="">--Pilih--</option>
                                        <option>Siang</option>
                                        <option>Malam</option>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="text" name="nomor_polisi_batubara_kirim" class="form-control" placeholder="Nomor Polisi" />
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Nomor DT / Kode Kirim</div>
                                <div class="col-md-4">
                                    <input type="text" name="unit_batubara_kirim" class="form-control" placeholder="No Unit DT" />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_kode_batubara_kirim" class="form-control" required>
                                        <option value="">--Pilih Kode Kirim--</option>
                                        <?php
                                        foreach ($kode_batubara_kirim as $index => $kode_batubara_kirim_add) {
                                            echo "<option value='" . $kode_batubara_kirim_add->id_kode_batubara_kirim . "'>$kode_batubara_kirim_add->nama_kode_batubara_kirim</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Dari / Tujuan</div>
                                <div class="col-md-4">
                                    <select name="id_asal_batubara_kirim" class="form-control" required>
                                        <option value="">--Asal Batubara--</option>
                                        <?php
                                        foreach ($stockpile_batubara_masuk as $index => $asal_batubara_kirim_add) {
                                            echo "<option value='" . $asal_batubara_kirim_add->id_stockpile_batubara_masuk . "'>$asal_batubara_kirim_add->nama_stockpile_batubara_masuk</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_tujuan_batubara_kirim" class="form-control" required>
                                        <option value="">--Pilih Tujuan--</option>
                                        <?php
                                        foreach ($tujuan_batubara_kirim as $index => $tujuan_batubara_kirim_add) {
                                            echo "<option value='" . $tujuan_batubara_kirim_add->id_tujuan_batubara_kirim . "'>$tujuan_batubara_kirim_add->nama_tujuan_batubara_kirim</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Bruto / Tarra</div>
                                <div class="col-md-4">
                                    <input type="number" name="bruto_batubara_kirim" class="form-control" placeholder="Bruto" step=".001" required />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="number" name="tarra_batubara_kirim" class="form-control" placeholder="Berat Kosong" step=".001" required />
                                    <br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">Tanggal / Jam Kirim</div>
                                <div class="col-md-4">
                                    <input type="text" name="tanggal_batubara_kirim" class="form-control datepicker" value="<?php echo date('d-m-Y') ?>" placeholder="Tanggal Kirim" required />
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <input type="text" name="jam_batubara_kirim" class="form-control timepicker" value="<?php echo date('H:i') ?>" placeholder="Jam Kirim" required />
                                    <br />
                                </div>
                            </div>

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

                            <div class="row">
                                <div class="col-md-3">EDP Kirim / Terima </div>
                                <div class="col-md-4">
                                    <select name="id_checker" class="form-control" required>
                                        <option value="">--Nama EDP Kirim--</option>
                                        <?php
                                        foreach ($checker as $index => $checker_add) {
                                            echo "<option value='" . $checker_add->id_checker . "'>$checker_add->nama_checker</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                                <div class="col-md-4">
                                    <select name="id_checker" class="form-control" required>
                                        <option value="">--Nama EDP Terima--</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">Keterangan </div>
                                <div class="col-md-8">
                                    <input type="text" name="keterangan_batubara_kirim" 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 Kirim</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_kirim/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>
    
    <div id="modalReport_kode" 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">Kode Kirim</h6>
                    <div class="dropdown no-arrow">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                </div>
                <form id="formReport_kode" class="form-horizontal" method="post" target="_blank" action="<?php echo base_url('form_batubara_kirim/report_kode') ?>" 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 class="row">
                                <div class="col-md-3">Kode Kirim</div>
                                <div class="col-md-8">
                                    <select name="id_kode_batubara_kirim" class="form-control" required>
                                        <option value="">--Pilih--</option>
                                        <?php
                                        foreach ($kode_batubara_kirim as $index => $kode_batubara_kirim_report) {
                                            echo "<option value='" . $kode_batubara_kirim_report->id_kode_batubara_kirim . "'>$kode_batubara_kirim_report->nama_kode_batubara_kirim</option>";
                                        }
                                        ?>
                                    </select>
                                    <br />
                                </div>
                            </div>

                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitReport_kode" 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');
    }
    
    function report_kode() {
        $('#btnReport_kode').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
        $('#formReport_kode')[0].reset();

        $('#btnReport_kode').html('<span class="fa fa-file"></span> Kode Kirim');
        $('#modalReport_kode').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_kirim/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!", "Kirimkan 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_kirim/get_data',
            data: 'id=' + id
        }).done(function(res) {
            var data = $.parseJSON(res);
            $('#tanggal_batubara_kirim').datepicker("setDate", convert_date_indo(data.tanggal_batubara_kirim));
            $('#tanggal_terima').datepicker("setDate", convert_date_indo(data.tanggal_terima));
            $('#shift_batubara_kirim').val(data.shift_batubara_kirim);
            $('#jam_batubara_kirim').val(data.jam_batubara_kirim.substr(0, 5));
            $('#jam_terima').val(data.jam_terima.substr(0, 5));
            //edit
            $('#id_unit_batubara_kirim').val(data.id_unit_batubara_kirim);
            $('#bruto_batubara_kirim').val(Math.round(data.bruto_batubara_kirim * 1000) / 1000);
            $('#tarra_batubara_kirim').val(Math.round(data.tarra_batubara_kirim * 1000) / 1000);
            $('#nomor_polisi_batubara_kirim').val(data.nomor_polisi_batubara_kirim);
            $('#id_asal_batubara_kirim').val(data.id_stockpile_batubara_masuk);
            $('#id_tujuan_batubara_kirim').val(data.id_tujuan_batubara_kirim);
            $('#nomor_sj_batubara_kirim').val(data.nomor_sj_batubara_kirim);
            $('#id_kode_batubara_kirim').val(data.id_kode_batubara_kirim);
            $('#keterangan_batubara_kirim').val(data.keterangan_batubara_kirim);
            $('#id_batubara_kirim').val(id);
            $('#id_checker').val(id);
            //edit
            //$('#id_checker_batubara_kirim_jetty').val(id);
            document.getElementById('pratinjauGambarEdit').innerHTML = '<img src="assets/images/batubara_kirim/' + 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_kirim/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!", "Kirimkan 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_kirim/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();
                            }
                        });
                });
            }
        );
    };

    function diterima(code) {
        swal({
                title: "Apakah anda yakin?",
                text: "Data sudah diterima dipelabuhan",
                type: "success",
                showCancelButton: true,
                confirmButtonColor: "#EF5350",
                confirmButtonText: "Diterima",
                cancelButtonText: "Batal",
                closeOnConfirm: false,
                showLoaderOnConfirm: true
            },
            function() {
                $.ajax({
                    type: 'post',
                    url: '<?php echo base_url(); ?>form_batubara_kirim/batubara_diterima',
                    data: {
                        submit: true,
                        id: code
                    }
                }).done(function() {
                    swal({
                            title: "Berhasil",
                            text: "Data Berhasil Diterima",
                            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>