Mini Shell

Direktori : /var/www/vhosts/ccp.ac.th/backup_full/httpdocs/fertilizer/
Upload File :
Current File : /var/www/vhosts/ccp.ac.th/backup_full/httpdocs/fertilizer/Fertilizer.php

<?php
// ส่วนนี้สำหรับรับค่า AJAX เพื่อดึงรายการแม่ปุ๋ย/สารเติมของสหกรณ์และปีบัญชีที่เลือก
if(isset($_POST['action']) && $_POST['action'] == 'get_ingredients') {
    include 'db/database.php';
    $sahakornID = mysqli_real_escape_string($link, $_POST['sahakornID']);
    $year = mysqli_real_escape_string($link, $_POST['year']);
    $options = "<option value=''></option>";
    
    // ดึงชื่อแม่ปุ๋ยและสารเติมทั้งหมดที่มีการบันทึกการเบิกผลิต ในปีบัญชีและสหกรณ์นั้นๆ
    $sql_ing = "SELECT DISTINCT S_mother_pui as ing_name 
                FROM sahakorn_process 
                WHERE sahakornID = '$sahakornID' AND year = '$year' 
                ORDER BY S_mother_pui ASC";
    $res_ing = mysqli_query($link, $sql_ing);
    
    if($res_ing){
        while($row = mysqli_fetch_assoc($res_ing)){
            $options .= "<option value='".htmlspecialchars($row['ing_name'])."'>".htmlspecialchars($row['ing_name'])."</option>";
        }
    }
    
    echo $options;
    exit; // จบการทำงาน AJAX แค่ตรงนี้
}
?>
<!doctype html>
<html lang="en">
    <?php include 'head.php'; ?>

    <!-- Select2 CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
    
    <!-- เรียกใช้ไฟล์ SweetAlert CSS ที่แนบมา -->
    <link rel="stylesheet" href="sweetalert.css">

    <style>
        /* ปรับแต่ง Select2 ให้เข้ากับ Material Design */
        .select2-container--default .select2-selection--single {
            background-color: transparent !important;
            border: none !important;
            border-bottom: 1px solid #D2D2D2 !important;
            border-radius: 0 !important;
            height: 36px !important;
        }
        .select2-container--default .select2-selection--single .select2-selection__rendered {
            padding-left: 0 !important;
            line-height: 36px !important;
            color: #555 !important;
        }
        .select2-container--default .select2-selection--single .select2-selection__arrow {
            height: 36px !important;
        }
        .form-group.is-focused .select2-container--default .select2-selection--single {
            border-bottom: 2px solid #9c27b0 !important; 
        }
        .select2-container--default .select2-selection--single:focus {
            outline: none !important;
        }
        
        /* ปรับแต่งหัวตารางให้อยู่กึ่งกลาง */
        th, td { text-align: center; vertical-align: middle !important; }
        td.text-left { text-align: left; }

        /* ---------- แก้ไขปัญหา SweetAlert1 ---------- */
        /* ซ่อนกล่อง Input และแจ้งเตือน Error ที่ถูก Material Design บังคับแสดงผล */
        .sweet-alert fieldset,
        .sweet-alert .form-group,
        .sweet-alert .sa-error-container,
        .sweet-alert .sa-input-error {
            display: none !important;
        }
        /* ------------------------------------------- */
    </style>

    <body>
        <div class="wrapper">
            <?php include 'leftside.php'; ?>

            <div class="main-panel">
                <?php include 'header.php'; ?>
                <?php 
                    include 'db/database.php'; 
                    
                    // ตัวแปรสำหรับแจ้งเตือน SweetAlert
                    $alert_script = "";

                    // ---------- จัดการระบบเพิ่ม / แก้ไข / ลบ ข้อมูล ----------
                    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
                        
                        $sahakornID = mysqli_real_escape_string($link, $_POST['sahakornID']);
                        $year = mysqli_real_escape_string($link, $_POST['year']);

                        if($_POST['action'] == 'delete') {
                            // กรณีลบข้อมูล
                            $del_id = mysqli_real_escape_string($link, $_POST['delete_id']);
                            $sql_del = "DELETE FROM calculate_fertilizer WHERE calculate_fertilizerID = '$del_id'";
                            if(mysqli_query($link, $sql_del)){
                                $alert_script = "swal({ title: 'สำเร็จ', text: 'ลบข้อมูลออกจากระบบแล้ว', type: 'success', timer: 2000, showConfirmButton: false });";
                            } else {
                                $alert_script = "swal({ title: 'ผิดพลาด', text: 'ไม่สามารถลบข้อมูลได้', type: 'error', timer: 2000, showConfirmButton: false });";
                            }
                        } 
                        else if ($_POST['action'] == 'add' || $_POST['action'] == 'edit') {
                            // รับค่าจากฟอร์ม Modal
                            $ingredient = mysqli_real_escape_string($link, $_POST['ingredient']);
                            $remaining = (float)$_POST['remaining'];
                            $buy = (float)$_POST['buy'];
                            $sell = (float)$_POST['sell'];
                            $countable = (float)$_POST['countable'];

                            // ดึงข้อมูลการเบิกวัตถุดิบไปผลิตของแม่ปุ๋ย/สารเติม จากตาราง sahakorn_process
                            $sql_process = "SELECT SUM(sum) as total_pickup 
                                            FROM sahakorn_process 
                                            WHERE sahakornID = '$sahakornID' 
                                              AND year = '$year' 
                                              AND S_mother_pui = '$ingredient'"; 
                            $rs_process = mysqli_query($link, $sql_process);
                            
                            $pick_up = 0;
                            if($row_p = mysqli_fetch_assoc($rs_process)){
                                $pick_up = (float)$row_p['total_pickup'];
                            }

                            // คำนวณตามสูตร
                            $total_sum = $remaining + $buy;
                            $year_balance = $total_sum - $sell - $pick_up;
                            $sack = $year_balance / 50; 
                            $tooshort = $countable - $year_balance; 

                            if($_POST['action'] == 'add') {
                                // เช็คข้อมูลซ้ำเพื่อทำการ Update ทับของเดิม หรือ Insert ใหม่
                                $check_sql = "SELECT calculate_fertilizerID FROM calculate_fertilizer 
                                              WHERE sahakornID = '$sahakornID' AND year = '$year' AND name_pui_C = '$ingredient'"; 
                                $check_res = mysqli_query($link, $check_sql);

                                if(mysqli_num_rows($check_res) > 0) {
                                    $update_sql = "UPDATE calculate_fertilizer SET 
                                                   remaining = '$remaining', buy = '$buy', sell = '$sell', 
                                                   pick_up = '$pick_up', year_balance = '$year_balance', 
                                                   countable_F = '$countable', tooshort_F = '$tooshort', sack_C = '$sack'
                                                   WHERE sahakornID = '$sahakornID' AND year = '$year' AND name_pui_C = '$ingredient'"; 
                                    mysqli_query($link, $update_sql);
                                    $alert_script = "swal({ title: 'สำเร็จ', text: 'อัปเดตข้อมูลวัตถุดิบที่มีอยู่แล้วเรียบร้อย', type: 'success', timer: 2000, showConfirmButton: false });";
                                } else {
                                    $insert_sql = "INSERT INTO calculate_fertilizer 
                                                   (sahakornID, year, name_pui_C, remaining, buy, sell, pick_up, year_balance, countable_F, tooshort_F, sack_C) 
                                                   VALUES 
                                                   ('$sahakornID', '$year', '$ingredient', '$remaining', '$buy', '$sell', '$pick_up', '$year_balance', '$countable', '$tooshort', '$sack')"; 
                                    mysqli_query($link, $insert_sql);
                                    $alert_script = "swal({ title: 'สำเร็จ', text: 'เพิ่มข้อมูลวัตถุดิบใหม่เรียบร้อยแล้ว', type: 'success', timer: 2000, showConfirmButton: false });";
                                }
                            } 
                            else if ($_POST['action'] == 'edit') {
                                $edit_id = mysqli_real_escape_string($link, $_POST['edit_id']);
                                $update_sql = "UPDATE calculate_fertilizer SET 
                                               remaining = '$remaining', buy = '$buy', sell = '$sell', 
                                               pick_up = '$pick_up', year_balance = '$year_balance', 
                                               countable_F = '$countable', tooshort_F = '$tooshort', sack_C = '$sack'
                                               WHERE calculate_fertilizerID = '$edit_id'"; 
                                mysqli_query($link, $update_sql);
                                $alert_script = "swal({ title: 'สำเร็จ', text: 'แก้ไขข้อมูลวัตถุดิบเรียบร้อยแล้ว', type: 'success', timer: 2000, showConfirmButton: false });";
                            }
                        }
                    }

                    // ค่าที่ใช้แสดงผลตารางปัจจุบัน
                    $view_sahakornID = isset($_POST['sahakornID']) ? $_POST['sahakornID'] : '';
                    $view_year = isset($_POST['year']) ? $_POST['year'] : '';
                ?>  

                <div class="content">
                    <div class="container-fluid">            

                        <!-- การ์ดฟอร์มหลัก เลือกสหกรณ์และปีบัญชี -->
                        <div class="card">
                            <div class="card-header" data-background-color="purple">
                               <h4 class="title" style="color: white;">กรุณาเลือกสหกรณ์และปีบัญชี</h4>
                            </div>
                            <div class="card-content">
                                <form method="POST" action="">
                                    <input type="hidden" name="action" value="view">
                                    <div class="row">
                                        <div class="col-md-5">
                                            <div class="form-group label-floating">
                                                <label class="control-label">กรุณาเลือกสหกรณ์</label>
                                                <select name="sahakornID" class="form-control select2" required>
                                                    <option value=""></option>
                                                    <?php
                                                    $sql_sahakorn = "SELECT sahakornID, name FROM sahakorn ORDER BY sahakornID ASC"; 
                                                    $result_sahakorn = mysqli_query($link, $sql_sahakorn);
                                                    while($row_sahakorn = mysqli_fetch_assoc($result_sahakorn)) {
                                                        $selected = ($view_sahakornID == $row_sahakorn['sahakornID']) ? 'selected' : '';
                                                        echo "<option value='".$row_sahakorn['sahakornID']."' {$selected}>".$row_sahakorn['name']."</option>"; 
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="form-group label-floating">
                                                <label class="control-label">กรุณาเลือกปีบัญชี(ปีพ.ศ.)</label>
                                                <select name="year" class="form-control select2" required>
                                                    <option value=""></option>
                                                    <?php
                                                    $current_year_be = (int)date('Y') + 543; 
                                                    for ($y = $current_year_be; $y >= $current_year_be - 4; $y--) {
                                                        $selected_year = ($view_year == $y) ? 'selected' : '';
                                                        echo "<option value='".$y."' {$selected_year}>".$y."</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <button type="submit" class="btn btn-primary pull-right" style="margin-top: 20px;">
                                                <i class="material-icons">search</i> ค้นหา/จัดการข้อมูล
                                            </button>
                                        </div>
                                    </div>
                                    <div class="clearfix"></div>
                                </form>
                            </div>
                        </div>

                        <!-- แสดงตารางข้อมูลก็ต่อเมื่อมีการเลือกสหกรณ์และปีบัญชีแล้ว -->
                        <?php
                        if ($view_sahakornID != '' && $view_year != '') {
                            
                            $sk_name = "";
                            $q_sk = mysqli_query($link, "SELECT name FROM sahakorn WHERE sahakornID = '$view_sahakornID'"); 
                            if($r_sk = mysqli_fetch_assoc($q_sk)){
                                $sk_name = $r_sk['name']; 
                            }
                        ?>
                        
                        <div class="card">
                            <!-- เปลี่ยนแถบสีหัวตารางเป็นสีฟ้า (blue หรือ info) -->
                            <div class="card-header" data-background-color="blue">
                                <h4 class="title" style="color: white; margin: 0;"><?php echo $sk_name . " ปี " . $view_year; ?></h4>
                            </div>
                            <div class="card-content table-responsive">
                                
                                <!-- ย้ายปุ่มเพิ่มข้อมูลมาอยู่ล่างแถบสีฟ้า -->
                                <div style="margin-bottom: 15px;">
                                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addModal">
                                        <i class="material-icons">add</i> เพิ่มข้อมูล
                                    </button>
                                </div>
                                
                                <table class="table table-bordered table-hover">
                                    <thead class="text-info">
                                        <tr>
                                            <th>แม่ปุ๋ย/สารเติม</th>
                                            <th>วัตถุดิบคงเหลือ<br>ต้นปี</th>
                                            <th>ซื้อวัตถุดิบ<br>ระหว่างปี</th>
                                            <th>รวม</th>
                                            <th>ขาย<br>วัตถุดิบ</th>
                                            <th>เบิกวัตถุดิบ<br>ไปผลิต</th>
                                            <th>วัตถุดิบคงเหลือ<br>สิ้นปี</th>
                                            <th>ปริมาณ<br>นับได้(กก.)</th>
                                            <th>ขาด/<br>เกิน</th>
                                            <th>คิดเป็น<br>กระสอบ</th>
                                            <th>เครื่องมือ</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $sql_display = "SELECT * FROM calculate_fertilizer WHERE sahakornID = '$view_sahakornID' AND year = '$view_year'"; 
                                        $rs_display = mysqli_query($link, $sql_display);

                                        $sum_remaining = 0; $sum_buy = 0; $sum_total_sum = 0; $sum_sell = 0;
                                        $sum_pick_up = 0; $sum_year_balance = 0; $sum_countable = 0;
                                        $sum_tooshort = 0; $sum_sack = 0;

                                        if(mysqli_num_rows($rs_display) > 0) {
                                            while($row_calc = mysqli_fetch_assoc($rs_display)){
                                                $total_row_sum = $row_calc['remaining'] + $row_calc['buy']; 

                                                $sum_remaining += $row_calc['remaining']; 
                                                $sum_buy += $row_calc['buy']; 
                                                $sum_total_sum += $total_row_sum;
                                                $sum_sell += $row_calc['sell']; 
                                                $sum_pick_up += $row_calc['pick_up']; 
                                                $sum_year_balance += $row_calc['year_balance']; 
                                                $sum_countable += $row_calc['countable_F']; 
                                                $sum_tooshort += $row_calc['tooshort_F']; 
                                                $sum_sack += $row_calc['sack_C']; 
                                        ?>
                                        <tr>
                                            <td class="text-left"><?php echo htmlspecialchars($row_calc['name_pui_C']); ?></td>
                                            <td><?php echo number_format($row_calc['remaining'], 2); ?></td>
                                            <td><?php echo number_format($row_calc['buy'], 2); ?></td>
                                            <td style="font-weight: bold; color: #00bcd4;"><?php echo number_format($total_row_sum, 2); ?></td>
                                            <td><?php echo number_format($row_calc['sell'], 2); ?></td>
                                            <td><?php echo number_format($row_calc['pick_up'], 2); ?></td>
                                            <td style="font-weight: bold; color: #ff9800;"><?php echo number_format($row_calc['year_balance'], 2); ?></td>
                                            <td><?php echo number_format($row_calc['countable_F'], 2); ?></td>
                                            <td style="<?php echo ($row_calc['tooshort_F'] < 0) ? 'color: red;' : 'color: blue;'; ?>"><?php echo number_format($row_calc['tooshort_F'], 2); ?></td>
                                            <td><?php echo number_format($row_calc['sack_C'], 2); ?></td>
                                            <td class="td-actions text-center">
                                                <!-- ปุ่มแก้ไข (ส่งค่าไปยัง Form Modal) -->
                                                <button type="button" rel="tooltip" title="แก้ไขข้อมูล" class="btn btn-warning btn-simple btn-xs" 
                                                    onclick="openEditModal('<?php echo $row_calc['calculate_fertilizerID']; ?>', '<?php echo htmlspecialchars($row_calc['name_pui_C']); ?>', '<?php echo $row_calc['remaining']; ?>', '<?php echo $row_calc['buy']; ?>', '<?php echo $row_calc['sell']; ?>', '<?php echo $row_calc['countable_F']; ?>')">
                                                    <i class="material-icons">edit</i>
                                                </button>
                                                <!-- ปุ่มลบ (จัดการด้วยคลาส btn-delete และ Form) -->
                                                <form method="POST" action="" style="display: inline-block; margin: 0;">
                                                    <input type="hidden" name="action" value="delete">
                                                    <input type="hidden" name="delete_id" value="<?php echo $row_calc['calculate_fertilizerID']; ?>">
                                                    <input type="hidden" name="sahakornID" value="<?php echo $view_sahakornID; ?>">
                                                    <input type="hidden" name="year" value="<?php echo $view_year; ?>">
                                                    <button type="submit" rel="tooltip" title="ลบข้อมูล" class="btn btn-danger btn-simple btn-xs btn-delete">
                                                        <i class="material-icons">close</i>
                                                    </button>
                                                </form>
                                            </td>
                                        </tr>
                                        <?php 
                                            } 
                                        ?>
                                        <!-- แถวแสดงผลรวม -->
                                        <tr style="font-weight: bold; color: #d32f2f; background-color: #fafafa;">
                                            <td>รวม</td>
                                            <td><?php echo number_format($sum_remaining, 2); ?></td>
                                            <td><?php echo number_format($sum_buy, 2); ?></td>
                                            <td><?php echo number_format($sum_total_sum, 2); ?></td>
                                            <td><?php echo number_format($sum_sell, 2); ?></td>
                                            <td><?php echo number_format($sum_pick_up, 2); ?></td>
                                            <td><?php echo number_format($sum_year_balance, 2); ?></td>
                                            <td><?php echo number_format($sum_countable, 2); ?></td>
                                            <td><?php echo number_format($sum_tooshort, 2); ?></td>
                                            <td><?php echo number_format($sum_sack, 2); ?></td>
                                            <td></td>
                                        </tr>
                                        <?php
                                        } else {
                                            echo "<tr><td colspan='11' class='text-center' style='padding: 20px;'>ยังไม่มีข้อมูล กดปุ่มเพิ่มข้อมูลด้านบน</td></tr>";
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <?php 
                        } // end if show table
                        ?>

                    </div>
                </div>

                <?php include 'footer1.php'; ?> 

            </div>
        </div>
        <?php include 'footer2.php'; ?>

        <!-- ======================= MODAL: เพิ่มข้อมูล ======================= -->
        <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <form method="POST" action="">
                        <input type="hidden" name="action" value="add">
                        <input type="hidden" name="sahakornID" value="<?php echo $view_sahakornID; ?>">
                        <input type="hidden" name="year" value="<?php echo $view_year; ?>">
                        
                        <div class="modal-header" style="background-color: #4caf50; color: white;">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="color:white;"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="addModalLabel">เพิ่มข้อมูลวัตถุดิบ (ปี <?php echo $view_year; ?>)</h4>
                        </div>
                        <div class="modal-body">
                            <div class="form-group label-floating is-empty">
                                <label class="control-label">กรุณาเลือกแม่ปุ๋ย/สารเติม</label>
                                <select name="ingredient" class="form-control" required style="margin-top: 5px;">
                                    <option value=""></option>
                                    <?php
                                    if ($view_sahakornID != '' && $view_year != '') {
                                        // ดึงรายการแม่ปุ๋ย/สารเติมจาก sahakorn_process มาแสดงเป็นตัวเลือก
                                        $sql_ing = "SELECT DISTINCT S_mother_pui as ing_name FROM sahakorn_process WHERE sahakornID = '$view_sahakornID' AND year = '$view_year' ORDER BY S_mother_pui ASC";
                                        $res_ing = mysqli_query($link, $sql_ing);
                                        while($row_ing = mysqli_fetch_assoc($res_ing)){
                                            echo "<option value='".htmlspecialchars($row_ing['ing_name'])."'>".htmlspecialchars($row_ing['ing_name'])."</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="form-group label-floating">
                                <label class="control-label">วัตถุดิบคงเหลือยกมาต้นปี (กก.)</label>
                                <input type="number" step="0.01" name="remaining" class="form-control" required>
                            </div>
                            <div class="form-group label-floating">
                                <label class="control-label">ซื้อวัตถุดิบระหว่างปี (กก.)</label>
                                <input type="number" step="0.01" name="buy" class="form-control" required>
                            </div>
                            <div class="form-group label-floating">
                                <label class="control-label">ขายวัตถุดิบ (กก.)</label>
                                <input type="number" step="0.01" name="sell" class="form-control" required>
                            </div>
                            <div class="form-group label-floating">
                                <label class="control-label">ปริมาณที่นับได้ (กก.)</label>
                                <input type="number" step="0.01" name="countable" class="form-control" required>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">ยกเลิก</button>
                            <button type="submit" class="btn btn-success">บันทึกข้อมูล</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- ======================= MODAL: แก้ไขข้อมูล ======================= -->
        <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <form method="POST" action="">
                        <input type="hidden" name="action" value="edit">
                        <input type="hidden" name="edit_id" id="edit_id" value="">
                        <input type="hidden" name="sahakornID" value="<?php echo $view_sahakornID; ?>">
                        <input type="hidden" name="year" value="<?php echo $view_year; ?>">
                        
                        <div class="modal-header" style="background-color: #ff9800; color: white;">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="color:white;"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="editModalLabel">แก้ไขข้อมูลวัตถุดิบ</h4>
                        </div>
                        <div class="modal-body">
                            <div class="form-group">
                                <label class="control-label" style="font-size:12px;">แม่ปุ๋ย/สารเติม (ไม่สามารถแก้ไขได้)</label>
                                <input type="text" name="ingredient" id="edit_ingredient" class="form-control" readonly style="background-color: #eee; padding-left: 10px;">
                            </div>
                            <div class="form-group label-floating is-empty">
                                <label class="control-label">วัตถุดิบคงเหลือยกมาต้นปี (กก.)</label>
                                <input type="number" step="0.01" name="remaining" id="edit_remaining" class="form-control" required>
                            </div>
                            <div class="form-group label-floating is-empty">
                                <label class="control-label">ซื้อวัตถุดิบระหว่างปี (กก.)</label>
                                <input type="number" step="0.01" name="buy" id="edit_buy" class="form-control" required>
                            </div>
                            <div class="form-group label-floating is-empty">
                                <label class="control-label">ขายวัตถุดิบ (กก.)</label>
                                <input type="number" step="0.01" name="sell" id="edit_sell" class="form-control" required>
                            </div>
                            <div class="form-group label-floating is-empty">
                                <label class="control-label">ปริมาณที่นับได้ (กก.)</label>
                                <input type="number" step="0.01" name="countable" id="edit_countable" class="form-control" required>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">ยกเลิก</button>
                            <button type="submit" class="btn btn-warning">บันทึกการแก้ไข</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Scripts -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
        <!-- ไฟล์ JS ของ SweetAlert 1 -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>

        <script>
        $(document).ready(function (){

            // เรียกการทำงานของ SweetAlert แจ้งเตือนสถานะเมื่อโหลดหน้าเสร็จ
            <?php echo $alert_script; ?>

            // เรียกใช้งาน Select2
            $('.select2').select2({
                width: '100%',
                placeholder: "" 
            });

            // ปรับแต่งคลาส label-floating ให้ Select2 ขยับขึ้นลง
            $('.select2').each(function() {
                if ($(this).val()) {
                    $(this).closest('.form-group').removeClass('is-empty');
                }
            });
            $('.select2').on('select2:open', function() {
                $(this).closest('.form-group').removeClass('is-empty').addClass('is-focused');
            }).on('select2:close', function() {
                $(this).closest('.form-group').removeClass('is-focused');
                if (!$(this).val()) {
                    $(this).closest('.form-group').addClass('is-empty');
                }
            }).on('change', function() {
                if ($(this).val()) {
                    $(this).closest('.form-group').removeClass('is-empty');
                } else {
                    $(this).closest('.form-group').addClass('is-empty');
                }
            });

            // ลบ class is-empty ของ select ทั่วไปใน Modal เพื่อให้ label ไม่บังข้อความ
            $('#addModal select').change(function() {
                if ($(this).val() !== '') {
                    $(this).parent('.form-group').removeClass('is-empty');
                }
            });

        }); 

        // ฟังก์ชันเปิด Modal แก้ไขและยัดข้อมูลเข้า Input
        function openEditModal(id, ingredient, remaining, buy, sell, countable) {
            $('#edit_id').val(id);
            $('#edit_ingredient').val(ingredient);
            $('#edit_remaining').val(remaining);
            $('#edit_buy').val(buy);
            $('#edit_sell').val(sell);
            $('#edit_countable').val(countable);
            
            // ขยับ Label ขึ้น (ลบคลาส is-empty ออก)
            $('#editModal .form-group').removeClass('is-empty');
            
            $('#editModal').modal('show');
        }

        // จัดการปุ่มลบข้อมูล ด้วย SweetAlert (เวอร์ชัน 1)
        // ==========================================
        $('.btn-delete').on('click', function(e) {
            e.preventDefault();
            var form = $(this).closest('form');
            
            swal({
                title: "ยืนยันการลบข้อมูล?",
                text: "หากลบแล้วจะไม่สามารถกู้คืนได้ และข้อมูลที่เชื่อมโยงจะถูกลบด้วย",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#f44336", // สีแดง
                confirmButtonText: "ใช่, ลบเลย!",
                cancelButtonText: "ยกเลิก",
                closeOnConfirm: false
            }, function(isConfirm){
                if (isConfirm) {
                    form.submit();
                }
            });
        });
        </script>  
    </body>
</html>

Zerion Mini Shell 1.0