<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">

    <div class="modal-dialog" role="document">

        <div class="modal-content">

            <div class="modal-header">

                <h5 class="modal-title" id="editModalLabel">Chỉnh Sửa</h5>

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                    <span aria-hidden="true">&times;</span>

                </button>

            </div>

            <div class="modal-body">

                <form id="editForm1">

                    <input type="hidden" name="id" id="edit-id">

                    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>"

                        value="<?php echo $this->security->get_csrf_hash(); ?>" />

                    <div class="form-row">

                        <div class="form-group" app-field-wrapper="amount">

                            <label for="MA_KH" class="control-label">Khách Hàng</label>

                            <select name="MA_KH" id="edit-Ten_KH" class="selectpicker form-control" data-width="100%"

                                data-none-selected-text="Chọn" data-live-search="true" tabindex="-98"

                                aria-invalid="false">

                                <option value=""></option>

                                <?php foreach ($clients as $client): ?>

                                        <option value="<?php echo $client['userid']; ?>" <?php echo (isset($congno) && $client['userid'] == $congno->MA_KH) ? 'selected' : ''; ?>>

                                            <?php echo $client['company']; ?></option>

                                        <?php endforeach; ?>

                            </select>

                        </div>

                        <div class="">

                            <label for="edit-So_cont">Số cont</label>

                            <input type="text" class="form-control" id="edit-So_cont" name="So_cont">

                        </div>

                        <span>

                                    <label for="Ngay_ghi_so">Ngày ghi sổ:</label>

                                    <input type="date" name="Ngay_ghi_so" id="edit-Ngay_ghi_so" require

                                        value="<?php echo date('Y-m-d'); ?>">

                        </span>

                                

                        <div class="">

                            <label for="edit-Diem_lay_rong" class="control-label">Điểm Lấy Rỗng</label>

                            <input type="text" class="form-control" id="edit-Diem_lay_rong" name="Diem_lay_rong">

                        </div>

                        <div class="">

                            <label for="edit-Diem_di" class="control-label">Điểm đi</label>

                            <input type="text" class="form-control" id="edit-Diem_di" name="Diem_di">

                        </div>

                        <div class="">

                            <label for="edit-Diem_den" class="control-label">Điểm Đến</label>

                            <input type="text" class="form-control" id="edit-Diem_den" name="Diem_den">

                        </div>

                        <div class="form-group" app-field-wrapper="amount">

                            <label for="edit-Loai_cont" class="control-label">Loại cont</label>

                            <select name="Loai_cont" id="edit-Loai_cont" class="selectpicker form-control"

                                data-width="100%" data-none-selected-text="Chọn" data-live-search="true"

                                tabindex="-98" aria-invalid="false">

                                <option value=""></option>

                                <option value="20">20</option>

                                <option value="40">40</option>

                            </select>

                        </div>

                        <div class="form-group" app-field-wrapper="amount">

                            <label for="edit-Xuat_Nhap" class="control-label">Xuất Nhập</label>

                            <select name="Xuat_Nhap" id="edit-Xuat_Nhap" class="selectpicker form-control"

                                data-width="100%" data-none-selected-text="Chọn" data-live-search="true"

                                tabindex="-98" aria-invalid="false">

                                <option value=""></option>

                                <option value="X">X</option>

                                <option value="N">N</option>

                            </select>

                        </div>

                        <div class=""  style="<?php echo isset($hide_shipping_cost) && $hide_shipping_cost ? 'display: none;' : ''; ?>">

                            <label for="edit-Gia_van_chuyen" class="control-label">Giá Vận Chuyển</label>

                            <input type="text" class="form-control form-control-format" id="edit-Gia_van_chuyen" name="Gia_van_chuyen">

                        </div>

                        <div class="form-group" app-field-wrapper="amount">

                            <label for="edit-VAT" class="control-label">VAT</label>

                            <select name="VAT" id="edit-VAT" class="selectpicker form-control" data-width="100%"

                                data-none-selected-text="Chọn" data-live-search="true" tabindex="-98"

                                aria-invalid="false">

                                <option value=""></option>

                                <option value="0.06">6%</option>

                                <option value="0.08">8%</option>

                                <option value="0.1">10%</option>

                                <option value="0.12">12%</option>

                            </select>

                        </div>

                        <div class="">

                            <label for="edit-Gia_cuoc" class="control-label">Phí Bốc Xếp</label>

                            <input type="text" class="form-control form-control-format" id="edit-Gia_cuoc" name="Gia_cuoc">

                        </div>

                        <div class="">

                            <label for="edit-Phi_nang" class="control-label">Phí Nâng</label>

                            <input type="text" class="form-control form-control-format" id="edit-Phi_nang" name="Phi_nang">

                        </div>



                        <div class="">

                            <label for="edit-Phi_ha" class="control-label">Phí Hạ</label>

                            <input type="text" class="form-control form-control-format" id="edit-Phi_ha" name="Phi_ha">

                        </div>



                        <div class="">

                            <label for="edit-Ve_sinh" class="control-label">Vệ Sinh</label>

                            <input type="text" class="form-control form-control-format" id="edit-Ve_sinh" name="Ve_sinh_can">

                        </div>
                        <div class="">

                            <label for="edit-Can" class="control-label">Cân</label>

                            <input type="text" class="form-control form-control-format" id="edit-Can" name="can">

                        </div>

                        <div class="">

                            <label for="edit-Cuoc_cont" class="control-label">Cược Cont</label>

                            <input type="text" class="form-control form-control-format" id="edit-Cuoc_cont" name="Cuoc_cont">

                        </div>

                        <div class="">

                            <label for="edit-Neo" class="control-label">Neo</label>

                            <input type="text" class="form-control form-control-format" id="edit-Neo" name="Neo">

                        </div>

                        <div class="">

                            <label for="edit-Phi_khac" class="control-label">Phí Khác</label>

                            <input type="text" class="form-control form-control-format" id="edit-Phi_khac" name="Phi_khac">

                        </div>

                        <div class="">

                            <label for="edit-Phai_thu" class="control-label">Phải Thu</label>

                            <input type="text" class="form-control form-control-format" id="edit-Phai_thu" name="Phai_thu">

                        </div>

                        <div class="">

                            <label for="edit-Da_thu" class="control-label">Đã Thu</label>

                            <input type="text" class="form-control form-control-format" id="edit-Da_thu" name="Da_thu">

                        </div>

                        <div class="form-group form-group-select-input-status input-group-select">

                                    <label for="status" class="control-label">

                                        Xe Ngoài

                                    </label>

                                    <div class="input-group input-group-select select-status"

                                        app-field-wrapper="status">

                                        <div class="dropdown bootstrap-select input-group-btn _select_input_group bs3"

                                            style="width: 100%;">

                                            <select id="status" name="Nha_xe" class="selectpicker Nha_xe_id form-control" data-width="100%" data-none-selected-text="Chọn" data-live-search="true" tabindex="-98">

                                                <option value=""></option>

                                                    <?php foreach ($status as $xengoai): ?>

                                                        <option value="<?php echo $xengoai['id']; ?>">

                                                            <?php echo $xengoai['name']; ?>

                                                        </option>

                                                    <?php endforeach; ?>

                                            </select>

                                           

                                        </div>

                                        <!-- <div class="input-group-btn">

                                            <a href="#" class="btn btn-default" onclick="new_lead_status_inline();return false;">

                                                <i class="fa fa-plus"></i>

                                            </a>

                                        </div> -->

                                    </div>

                                </div>

                        <div class="">

                            <label for="edit-Gia" class="control-label">Giá</label>

                            <input type="text" class="form-control form-control-format" id="edit-Gia" name="Gia">

                        </div>

                        <div class="form-group" app-field-wrapper="amount">

                            <label for="edit-So_xe" class="control-label">Tên Tài Xế (Xe Nhà)</label>

                            <select name="So_xe" id="edit-So_xe" class="selectpicker form-control" data-width="100%"

                                data-none-selected-text="Chọn" data-live-search="true" tabindex="-98"

                                aria-invalid="false">

                                <option value=""></option>

                                <?php foreach ($staff as $staffs): 

                                    if($staffs['role']==2 && $staffs['active']==1):

                                ?>

                                <option value="<?php echo $staffs['staffid']; ?>" <?php echo (isset($congno) && $staffs['staffid'] == $congno->So_xe) ? 'selected' : ''; ?>>

                                    <?php echo $staffs['staff_identifi'] . ' - ' . $staffs['firstname']; ?></option>

                                <?php endif; 

                            endforeach;?>

                            </select>

                        </div>

                        <div class="form-group" app-field-wrapper="amount">

                            <label for="edit-Bien_so_xe" class="control-label">Số Xe (Xe Nhà)</label>

                            <select name="bien_so_xe" id="edit-Bien_so_xe" class="selectpicker form-control" data-width="100%"

                                data-none-selected-text="Chọn" data-live-search="true" tabindex="-98"

                                aria-invalid="false">

                                <option value=""></option>

                                <?php foreach ($xenha as $xenha1): 

                                ?>

                                <option value="<?php echo $xenha1['id']; ?>" <?php echo (isset($congno) && $xenha1['id'] == $congno->bien_so_xe) ? 'selected' : ''; ?>>

                                    <?php echo $xenha1['name']; ?></option>

                                <?php

                            endforeach;?>

                            </select>

                        </div>

                        <div class="">

                            <label for="edit-Chi_phi_phat_sinh" class="control-label">Chi Phí Phát Sinh</label>

                            <input type="text" class="form-control form-control-format" id="edit-Chi_phi_phat_sinh"

                                name="Chi_phi_phat_sinh">

                        </div>

                        <div class="">

                            <label for="edit-Hoa_Hong" class="control-label">Hoa Hồng</label>

                            <input type="text" class="form-control form-control-format" id="edit-Hoa_Hong" name="Hoa_Hong">

                        </div>

                        <!-- <div class="">

                            <label for="edit-Tong_Chi_Phi" class="control-label">Tổng Chi Phí</label>

                            <input type="number" class="form-control" id="edit-Tong_Chi_Phi" name="Tong_Chi_Phi">

                        </div> -->

                        <div class="">

                            <label for="edit-Ghi_Chu_Them" class="control-label">Ghi Chú Thêm</label>

                            <textarea type="text" class="form-control" id="edit-Ghi_Chu_Them"

                                name="Ghi_Chu_Them"></textarea>

                        </div>

                    </div>

                    <button type="submit">Lưu</button>

                </form>

            </div>

        </div>

    </div>

</div>



<script>

$(document).ready(function() {

    $('.edit-button').click(function() {

        var id = $(this).data('id');

        $.ajax({

            url: '<?php echo site_url('congno/get_congno'); ?>/' +

                id,

            type: 'GET',

            dataType: 'json',

            success: function(data) {

                $('#edit-id').val(data.id);

                $('#edit-Ten_KH').val(data.MA_KH).change();

                $('#edit-Ngay_ghi_so').val(data.Ngay_ghi_so);

                $('#edit-So_cont').val(data.So_cont);

                $('#edit-Diem_lay_rong').val(data.Diem_lay_rong);

                $('#edit-Diem_di').val(data.Diem_di);

                $('#edit-Diem_den').val(data.Diem_den);

                $('#edit-Gia_van_chuyen').val(data.Gia_van_chuyen);

                $('#edit-Xuat_Nhap').val(data.xuat_nhap).change();

                $('#edit-Loai_cont').val(data.Loai_cont).change();

                $('#edit-Gia_cuoc').val(data.gia_cuoc);

                $('#edit-VAT').val(data.VAT).change();

                $('#edit-Phai_thu').val(data.Phai_thu);

                $('#edit-Da_thu').val(data.Da_thu);

                $('#edit-Ghi_Chu_Them').val(data.Ghi_chu_them);

                $('#edit-Chi_phi_phat_sinh').val(data.Chi_phi_phat_sinh);

                $('#edit-Hoa_Hong').val(data.Hoa_hong);

                $('#edit-Tong_Chi_Phi').val(data.Tong_chi_phi);

                $('#edit-Phi_nang').val(data.Phi_nang);

                $('#edit-Phi_ha').val(data.Phi_ha);

                $('#edit-Ve_sinh').val(data.Ve_sinh_can);

                $('#edit-Can').val(data.can);

                $('#edit-Cuoc_cont').val(data.Cuoc_cont);

                $('#edit-Neo').val(data.Neo);

                $('#edit-Phi_khac').val(data.Phi_khac);

                $('.Nha_xe_id').val(data.Nha_xe).change();

                $('#edit-Gia').val(data.Gia);

                $('#edit-So_xe').val(data.So_xe).change();

                $('#edit-Bien_so_xe').val(data.bien_so_xe).change();

                $('#edit-Luong_chuyen').val(data.Luong_chuyen);

                $('#editModal').modal('show');

            },

            error: function(jqXHR, textStatus,

                errorThrown) {

                console.error(

                    'Error fetching data: ',

                    textStatus, errorThrown);

            }

        });

    });



    var formChanged = false;

    $('#editForm1').on('input', function() {

        formChanged = true;

    });

    $('#editModal').on('hide.bs.modal', function() {

        if (formChanged) {

            var confirmationMessage =

                'Bạn có chắc chắn rằng bạn muốn thoát không? Nếu không, hãy nhấp vào "Thoát" trong hộp thoại sau.';

            var confirmationBox =

                '<div class="modal-footer"><button type="button" class="btn btn-danger" data-dismiss="modal">Thoát</button><button type="button" class="btn btn-primary save-changes">Lưu</button></div>';



            if (!confirm(confirmationMessage)) {

                return false;

            } else {

                $(confirmationBox).insertAfter(

                    '#editModal .modal-content');

                $('#editModal .save-changes').click(function() {

                    var formData = $(

                            '#editForm1').serialize();

                    $.ajax({

                        url: '<?php echo site_url('congno/update_congno'); ?>',

                        type: 'POST',

                        data: formData,

                        success: function(response) {

                            var jsonResponse = JSON.parse(

                                response);

                            if (jsonResponse.status ==

                                'success') {

                                $('#editModal').modal(

                                    'hide');

                                location.reload();

                            } else {

                                alert('Lỗi khi cập nhật dữ liệu');

                            }

                        },

                        error: function(jqXHR,

                            textStatus,

                            errorThrown) {

                            console.error(

                                'Error updating data: ',

                                textStatus, errorThrown);

                        }

                    });

                });

            }

        }

    });



    $('#editForm1').submit(function(e) {

        e.preventDefault();

        formChanged = false;

        var formData = $(this).serialize();



        $.ajax({

            url: '<?php echo site_url('congno/update_congno'); ?>',

            type: 'POST',

            data: formData,

            success: function(response) {

                var jsonResponse = JSON.parse(

                    response);

                if (jsonResponse.status ==

                    'success') {

                    $('#editModal').modal('hide');

                    location.reload();

                } else {

                    alert('Lỗi khi cập nhật dữ liệu');

                }

            },

            error: function(jqXHR, textStatus,

                errorThrown) {

                console.error(

                    'Error updating data: ',

                    textStatus, errorThrown);

            }

        });

    });

});

</script>

