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_sum.php

<!doctype html>
<html lang="en">

    <?php include 'head.php'; ?>
    
    <!-- CSS สำหรับ Select2 และ SweetAlert -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
    <link href="sweetalert.css" rel="stylesheet">

    <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;
            padding: 0 !important;
            box-shadow: none !important;
        }
        .select2-container--default .select2-selection--single .select2-selection__rendered {
            padding-left: 0 !important;
            line-height: 36px !important;
            color: #555555 !important;
            font-size: 14px;
        }
        .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; 
        }
        .form-group.label-floating label.control-label {
            z-index: 10;
            pointer-events: none;
        }
        .select2-container {
            margin-top: 5px;
        }
        
        /* สไตล์เสริมสำหรับปุ่มในตาราง */
        .td-actions .btn {
            margin: 0 2px;
            padding: 5px 10px;
        }

        /* กำหนดฟอนต์ให้ SweetAlert ให้เหมือนในรูปภาพ */
        .sweet-alert h2, .sweet-alert p, .sweet-alert button {
            font-family: 'Kanit', sans-serif !important;
        }
        
        /* --- แก้ไข: ซ่อนช่อง Input และ Error แจ้งเตือนของ SweetAlert ที่โผล่มาผิดปกติ --- */
        .sweet-alert fieldset,
        .sweet-alert .form-group,
        .sweet-alert .sa-error-container,
        .sweet-alert .sa-input-error,
        .sweet-alert input[type="text"] {
            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';  
                    
                    $is_searched = false;
                    $swal_status = "";
                    $swal_msg = "";

                    // รับค่าจากการค้นหา
                    $search_sahakornID = isset($_POST['sahakornID']) ? $_POST['sahakornID'] : '';
                    $search_year = isset($_POST['year']) ? $_POST['year'] : '';
                    $action = isset($_POST['action']) ? $_POST['action'] : '';

                    // หากมีการระบุสหกรณ์และปีบัญชี
                    if (!empty($search_sahakornID) && !empty($search_year)) {
                        $is_searched = true;

                        // -------- จัดการเพิ่ม / แก้ไข / ลบ ข้อมูล --------
                        if ($action == 'add' || $action == 'edit') {
                            $formulaID = $_POST['formulaID'];
                            $remaining_pui = $_POST['remaining_pui'];
                            $sell_pui = $_POST['sell_pui'];
                            $use_pui = $_POST['use_pui'];
                            $countable_R = $_POST['countable_R'];

                            // หาผลรวมรับจากการผลิต
                            $sql_process = "SELECT SUM(sum) AS total_sum FROM sahakorn_process WHERE sahakornID = '$search_sahakornID' AND year = '$search_year' AND formulaID = '$formulaID'";
                            $res_process = mysqli_query($link, $sql_process);
                            $row_process = mysqli_fetch_assoc($res_process);
                            $from_production = $row_process['total_sum'] ? $row_process['total_sum'] : 0;

                            // คำนวณค่าต่างๆ
                            $total_pui = $remaining_pui + $from_production; 
                            $pui_year_balance = $total_pui - ($sell_pui + $use_pui); 
                            $sack_R = $pui_year_balance / 50; 
                            $tooshort_R = $countable_R - $pui_year_balance; 

                            if ($action == 'add') {
                                // ตรวจสอบว่ามีข้อมูลสูตรนี้ซ้ำในปีเดียวกันหรือไม่
                                $sql_check = "SELECT calculate_remainingID FROM calculate_remaining WHERE sahakornID = '$search_sahakornID' AND year = '$search_year' AND formulaID = '$formulaID'";
                                $res_check = mysqli_query($link, $sql_check);

                                if(mysqli_num_rows($res_check) > 0) {
                                    $sql_action = "UPDATE calculate_remaining SET 
                                                    remaining_pui = '$remaining_pui', from_production = '$from_production', 
                                                    sell_pui = '$sell_pui', `use` = '$use_pui', pui_year_balance = '$pui_year_balance', 
                                                    sack_R = '$sack_R', countable_R = '$countable_R', tooshort_R = '$tooshort_R' 
                                                   WHERE sahakornID = '$search_sahakornID' AND year = '$search_year' AND formulaID = '$formulaID'";
                                } else {
                                    $sql_action = "INSERT INTO calculate_remaining (sahakornID, year, formulaID, remaining_pui, from_production, sell_pui, `use`, pui_year_balance, sack_R, countable_R, tooshort_R) 
                                                   VALUES ('$search_sahakornID', '$search_year', '$formulaID', '$remaining_pui', '$from_production', '$sell_pui', '$use_pui', '$pui_year_balance', '$sack_R', '$countable_R', '$tooshort_R')";
                                }

                                if(mysqli_query($link, $sql_action)){
                                    $swal_status = "success";
                                    $swal_msg = "บันทึกข้อมูลเรียบร้อยแล้ว";
                                }

                            } elseif ($action == 'edit') {
                                $edit_id = $_POST['edit_id'];
                                $sql_action = "UPDATE calculate_remaining SET 
                                                formulaID = '$formulaID', remaining_pui = '$remaining_pui', from_production = '$from_production', 
                                                sell_pui = '$sell_pui', `use` = '$use_pui', pui_year_balance = '$pui_year_balance', 
                                                sack_R = '$sack_R', countable_R = '$countable_R', tooshort_R = '$tooshort_R' 
                                               WHERE calculate_remainingID = '$edit_id'";
                                               
                                if(mysqli_query($link, $sql_action)){
                                    $swal_status = "success";
                                    $swal_msg = "แก้ไขข้อมูลเรียบร้อยแล้ว";
                                }
                            }
                        } elseif ($action == 'delete') {
                            $delete_id = $_POST['delete_id'];
                            $sql_action = "DELETE FROM calculate_remaining WHERE calculate_remainingID = '$delete_id'";
                            if(mysqli_query($link, $sql_action)){
                                $swal_status = "success";
                                $swal_msg = "ลบข้อมูลเรียบร้อยแล้ว";
                            }
                        }

                        // ดึงชื่อสหกรณ์สำหรับแสดงผล
                        $res_sahakorn_name = mysqli_query($link, "SELECT name FROM sahakorn WHERE sahakornID = '$search_sahakornID'");
                        $row_sk_name = mysqli_fetch_assoc($res_sahakorn_name);
                        $sahakorn_name_display = $row_sk_name ? $row_sk_name['name'] : "";
                    }
                ?>  
  
                <div class="content">
                    <div class="container-fluid">            
  
                        <!-- ฟอร์มค้นหาข้อมูล -->
                        <div class="card">
                            <div class="card-header" data-background-color="purple">
                                <h4 class="title" style="color: white; font-family: 'Kanit', sans-serif;">กรุณาเลือกสหกรณ์และปีบัญชี</h4>
                            </div>
                            <div class="card-content">
                                <form action="" method="POST">
                                    <input type="hidden" name="action" value="search">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group label-floating">
                                                <label class="control-label">กรุณาเลือกสหกรณ์</label>
                                                <select class="form-control select2-search" name="sahakornID" required>
                                                    <option value=""></option>
                                                    <?php
                                                    $sql_sk = "SELECT * FROM sahakorn";
                                                    $res_sk = mysqli_query($link, $sql_sk);
                                                    while ($row_sk = mysqli_fetch_assoc($res_sk)) {
                                                        $selected = ($search_sahakornID == $row_sk['sahakornID']) ? "selected" : "";
                                                        echo "<option value='{$row_sk['sahakornID']}' $selected>{$row_sk['name']}</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="form-group label-floating">
                                                <label class="control-label">กรุณาเลือกปีบัญชี (ปีพ.ศ.)</label>
                                                <select class="form-control select2-search" name="year" required>
                                                    <option value=""></option>
                                                    <?php
                                                    $current_year_be = date('Y') + 543;
                                                    for ($i = 0; $i <= 4; $i++) {
                                                        $year_option = $current_year_be - $i;
                                                        $selected_year = ($search_year == $year_option) ? "selected" : "";
                                                        echo "<option value='$year_option' $selected_year>$year_option</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <button type="submit" class="btn btn-primary pull-right"><i class="material-icons">search</i> ดูข้อมูล</button>
                                    <div class="clearfix"></div>
                                </form>
                            </div>
                        </div>

                        <!-- ตารางประวัติ (แสดงเมื่อมีการค้นหา) -->
                        <?php if($is_searched): ?>
                        <div class="card">
                            <div class="card-header" data-background-color="blue">
                                <h4 class="title" style="color: white; margin: 0; font-family: 'Kanit', sans-serif;">ประวัติการบันทึกข้อมูล : <?php echo $sahakorn_name_display; ?> ปีบัญชี <?php echo $search_year; ?></h4>
                            </div>
                            <div class="card-content table-responsive">
                                <!-- ย้ายปุ่มมาด้านล่างแถบสีฟ้า ชิดซ้าย -->
                                <div style="text-align: left; margin-bottom: 10px;">
                                    <button class="btn btn-success btn-round" data-toggle="modal" data-target="#modalAdd">
                                        <i class="material-icons">add</i> เพิ่มข้อมูล
                                    </button>
                                </div>

                                <table class="table table-hover" style="text-align: center;">
                                    <thead class="text-info">
                                        <tr>
                                            <th style="text-align: center;">ลำดับ</th>
                                            <th style="text-align: center;">สูตรปุ๋ยที่ผสม</th>
                                            <th style="text-align: center;">ยกมาต้นปี</th>
                                            <th style="text-align: center;">รับจากการผลิต</th>
                                            <th style="text-align: center;">รวม</th>
                                            <th style="text-align: center;">ขาย</th>
                                            <th style="text-align: center;">เบิกใช้</th>
                                            <th colspan="2" style="text-align: center;">คงเหลือสิ้นปี</th>
                                            <th colspan="2" style="text-align: center;">นับได้</th>
                                            <th colspan="2" style="text-align: center;">ขาด/เกิน</th>
                                            <th style="text-align: center; min-width: 100px;">เครื่องมือ</th>
                                        </tr>
                                        <tr>
                                            <th colspan="7" style="text-align: center;"></th>
                                            <th style="text-align: center; font-size: 15px;">กิโลกรัม</th>
                                            <th style="text-align: center; font-size: 15px;">กระสอบ</th>
                                            <th style="text-align: center; font-size: 15px;">กิโลกรัม</th>
                                            <th style="text-align: center; font-size: 15px;">กระสอบ</th>
                                            <th style="text-align: center; font-size: 15px;">กิโลกรัม</th>
                                            <th style="text-align: center; font-size: 15px;">กระสอบ</th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $no = 1;
                                        $sum_remaining = 0; $sum_production = 0; $sum_total = 0;
                                        $sum_sell = 0; $sum_use = 0; $sum_balance = 0;
                                        $sum_sack = 0; $sum_countable = 0; $sum_tooshort = 0;

                                        $sql_history = "SELECT c.*, f.name_formula 
                                                        FROM calculate_remaining c 
                                                        LEFT JOIN formula f ON c.formulaID = f.formulaID 
                                                        WHERE c.sahakornID = '$search_sahakornID' AND c.year = '$search_year'
                                                        ORDER BY f.name_formula ASC";
                                        $res_history = mysqli_query($link, $sql_history);
                                        
                                        if(mysqli_num_rows($res_history) > 0) {
                                            while($row_hist = mysqli_fetch_assoc($res_history)) {
                                                $row_total = $row_hist['remaining_pui'] + $row_hist['from_production'];
                                                
                                                $sum_remaining += $row_hist['remaining_pui'];
                                                $sum_production += $row_hist['from_production'];
                                                $sum_total += $row_total;
                                                $sum_sell += $row_hist['sell_pui'];
                                                $sum_use += $row_hist['use'];
                                                $sum_balance += $row_hist['pui_year_balance'];
                                                $sum_sack += $row_hist['sack_R'];
                                                $sum_countable += $row_hist['countable_R'];
                                                $sum_tooshort += $row_hist['tooshort_R'];
                                        ?>
                                        <tr>
                                            <td><?php echo $no++; ?></td>
                                            <td style="text-align: left;"><?php echo $row_hist['name_formula']; ?></td>
                                            <td><?php echo number_format($row_hist['remaining_pui'], 2); ?></td>
                                            <td><?php echo number_format($row_hist['from_production'], 2); ?></td>
                                            <td><?php echo number_format($row_total, 2); ?></td>
                                            <td><?php echo number_format($row_hist['sell_pui'], 2); ?></td>
                                            <td><?php echo number_format($row_hist['use'], 2); ?></td>
                                            <td><?php echo number_format($row_hist['pui_year_balance'], 2); ?></td>
                                            <td><?php echo number_format($row_hist['sack_R'], 2); ?></td>
                                            <td><?php echo number_format($row_hist['countable_R'], 2); ?></td>
                                            <td><?php $countable_sack = $row_hist['countable_R'] / 50; echo number_format($countable_sack, 2); ?></td>
                                            <td><?php echo number_format($row_hist['tooshort_R'], 2); ?></td>
                                            <td><?php $tooshort_sack = $row_hist['tooshort_R'] / 50; echo number_format($tooshort_sack, 2); ?></td>
                                            <td class="td-actions text-center">
                                                <button type="button" class="btn btn-warning btn-edit" 
                                                    data-id="<?php echo $row_hist['calculate_remainingID']; ?>"
                                                    data-formula="<?php echo $row_hist['formulaID']; ?>"
                                                    data-remain="<?php echo $row_hist['remaining_pui']; ?>"
                                                    data-sell="<?php echo $row_hist['sell_pui']; ?>"
                                                    data-use="<?php echo $row_hist['use']; ?>"
                                                    data-count="<?php echo $row_hist['countable_R']; ?>"
                                                    title="แก้ไขข้อมูล">
                                                    <i class="material-icons">edit</i>
                                                </button>
                                                <button type="button" class="btn btn-danger btn-delete" 
                                                    data-id="<?php echo $row_hist['calculate_remainingID']; ?>"
                                                    title="ลบข้อมูล">
                                                    <i class="material-icons">close</i>
                                                </button>
                                            </td>
                                        </tr>
                                        <?php 
                                            } 
                                        } else {
                                            echo "<tr><td colspan='12'>ไม่มีข้อมูลในปีบัญชี $search_year</td></tr>";
                                        }
                                        ?>
                                    </tbody>
                                    <tfoot style="background-color: #f5f5f5; font-weight: bold; color: #333;">
                                        <tr>
                                            <td colspan="2" style="text-align: right;">ผลรวมทั้งหมด:</td>
                                            <td><?php echo number_format($sum_remaining, 2); ?></td>
                                            <td><?php echo number_format($sum_production, 2); ?></td>
                                            <td><?php echo number_format($sum_total, 2); ?></td>
                                            <td><?php echo number_format($sum_sell, 2); ?></td>
                                            <td><?php echo number_format($sum_use, 2); ?></td>
                                            <td><?php echo number_format($sum_balance, 2); ?></td>
                                            <td><?php $sum_sack_calc = $sum_balance / 50; echo number_format($sum_sack_calc, 2); ?></td>
                                            <td><?php echo number_format($sum_countable, 2); ?></td>
                                            <td><?php $sum_countable_sack = $sum_countable / 50; echo number_format($sum_countable_sack, 2); ?></td>
                                            <td><?php echo number_format($sum_tooshort, 2); ?></td>
                                            <td><?php $sum_tooshort_sack = $sum_tooshort / 50; echo number_format($sum_tooshort_sack, 2); ?></td>
                                            <td></td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                        <?php endif; ?>

                    </div>
                </div>

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

            </div>
        </div>
        
        <?php include 'footer2.php'; ?>
        
        <!-- ฟอร์มซ่อนสำหรับการลบข้อมูล -->
        <form id="formDelete" method="POST" action="">
            <input type="hidden" name="sahakornID" value="<?php echo $search_sahakornID; ?>">
            <input type="hidden" name="year" value="<?php echo $search_year; ?>">
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="delete_id" id="delete_id" value="">
        </form>

        <!-- ===================== Modal เพิ่มข้อมูล ===================== -->
        <div class="modal fade" id="modalAdd" role="dialog" aria-labelledby="modalAddLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header" style="background-color: #4caf50; color: white;">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" style="font-family: 'Kanit', sans-serif;">เพิ่มข้อมูลปุ๋ยผสม</h4>
                    </div>
                    <form action="" method="POST">
                        <input type="hidden" name="sahakornID" value="<?php echo $search_sahakornID; ?>">
                        <input type="hidden" name="year" value="<?php echo $search_year; ?>">
                        <input type="hidden" name="action" value="add">
                        
                        <div class="modal-body">
                            <div class="form-group label-floating">
                                <label class="control-label">กรุณาเลือกสูตรปุ๋ยผสม</label>
                                <select class="form-control select2-modal" name="formulaID" required>
                                    <option value=""></option>
                                    <?php
                                    $sql_fm = "SELECT * FROM formula";
                                    $res_fm = mysqli_query($link, $sql_fm);
                                    while ($row_fm = mysqli_fetch_assoc($res_fm)) {
                                        echo "<option value='{$row_fm['formulaID']}'>{$row_fm['name_formula']}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">ปุ๋ยผสมคงเหลือยกมาต้นปี (กก.)</label>
                                        <input type="number" step="0.01" class="form-control" name="remaining_pui" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">ขายปุ๋ยผสม (กก.)</label>
                                        <input type="number" step="0.01" class="form-control" name="sell_pui" required>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">เบิกใช้ (กก.)</label>
                                        <input type="number" step="0.01" class="form-control" name="use_pui" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">ปริมาณนับได้ (กิโลกรัม)</label>
                                        <input type="number" step="0.01" class="form-control" name="countable_R" required>
                                    </div>
                                </div>
                            </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="modalEdit" role="dialog" aria-labelledby="modalEditLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header" style="background-color: #ff9800; color: white;">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" style="font-family: 'Kanit', sans-serif;">แก้ไขข้อมูลปุ๋ยผสม</h4>
                    </div>
                    <form action="" method="POST">
                        <input type="hidden" name="sahakornID" value="<?php echo $search_sahakornID; ?>">
                        <input type="hidden" name="year" value="<?php echo $search_year; ?>">
                        <input type="hidden" name="action" value="edit">
                        <input type="hidden" name="edit_id" id="edit_id" value="">

                        <div class="modal-body">
                            <div class="form-group label-floating">
                                <label class="control-label">กรุณาเลือกสูตรปุ๋ยผสม</label>
                                <select class="form-control select2-modal" name="formulaID" id="edit_formulaID" required>
                                    <option value=""></option>
                                    <?php
                                    $res_fm = mysqli_query($link, "SELECT * FROM formula");
                                    while ($row_fm = mysqli_fetch_assoc($res_fm)) {
                                        echo "<option value='{$row_fm['formulaID']}'>{$row_fm['name_formula']}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">ปุ๋ยผสมคงเหลือยกมาต้นปี (กก.)</label>
                                        <input type="number" step="0.01" class="form-control" name="remaining_pui" id="edit_remain" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">ขายปุ๋ยผสม (กก.)</label>
                                        <input type="number" step="0.01" class="form-control" name="sell_pui" id="edit_sell" required>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">เบิกใช้ (กก.)</label>
                                        <input type="number" step="0.01" class="form-control" name="use_pui" id="edit_use" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group label-floating">
                                        <label class="control-label">ปริมาณนับได้ (กิโลกรัม)</label>
                                        <input type="number" step="0.01" class="form-control" name="countable_R" id="edit_count" required>
                                    </div>
                                </div>
                            </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>
        
        <!-- JS ของ Select2 -->
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
        <!-- JS ของ SweetAlert 1 (ให้ตรงกับ css ที่ใช้งาน) -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>

        <script>
            $(document).ready(function() {
                // 1. เรียกใช้งาน Select2 สำหรับฟอร์มค้นหา
                $('.select2-search').select2({
                    width: '100%',
                    placeholder: "", 
                    allowClear: true 
                });

                // ควบคุม Label Floating สำหรับ Select2 ทั้งหมด
                function updateLabelFloating(selectElement) {
                    var val = $(selectElement).val();
                    var parentFormGroup = $(selectElement).closest('.form-group');
                    if (val) {
                        parentFormGroup.removeClass('is-empty');
                    } else {
                        parentFormGroup.addClass('is-empty');
                    }
                }

                // การทำงาน Label ของ Select2 บนหน้าจอหลัก
                $('.select2-search').each(function() { updateLabelFloating(this); });
                $('.select2-search')
                    .on('select2:open', function() { $(this).closest('.form-group').addClass('is-focused').removeClass('is-empty'); })
                    .on('select2:close', function() { $(this).closest('.form-group').removeClass('is-focused'); updateLabelFloating(this); })
                    .on('change', function() { updateLabelFloating(this); });

                // 2. เรียกใช้งาน Select2 ภายในฟอร์ม Modal
                $('.select2-modal').each(function() {
                    $(this).select2({
                        width: '100%',
                        placeholder: "", 
                        allowClear: true,
                        dropdownParent: $(this).closest('.modal-content')
                    });
                });
                
                $('.select2-modal').each(function() { updateLabelFloating(this); });
                $('.select2-modal')
                    .on('select2:open', function() { $(this).closest('.form-group').addClass('is-focused').removeClass('is-empty'); })
                    .on('select2:close', function() { $(this).closest('.form-group').removeClass('is-focused'); updateLabelFloating(this); })
                    .on('change', function() { updateLabelFloating(this); });


                // 3. จัดการเปิด Modal แก้ไขข้อมูล และส่งค่าลงไปใน Form
                $('.btn-edit').click(function() {
                    var id = $(this).data('id');
                    var formula = $(this).data('formula');
                    var remain = $(this).data('remain');
                    var sell = $(this).data('sell');
                    var use_val = $(this).data('use');
                    var count = $(this).data('count');

                    $('#edit_id').val(id);
                    // ใช้ trigger('change') เพื่อให้ Select2 อัปเดตค่าและ Label ลอยขึ้น
                    $('#edit_formulaID').val(formula).trigger('change'); 
                    
                    $('#edit_remain').val(remain).closest('.form-group').removeClass('is-empty');
                    $('#edit_sell').val(sell).closest('.form-group').removeClass('is-empty');
                    $('#edit_use').val(use_val).closest('.form-group').removeClass('is-empty');
                    $('#edit_count').val(count).closest('.form-group').removeClass('is-empty');

                    $('#modalEdit').modal('show');
                });

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

                // 5. แสดงผล SweetAlert ตาม Status จาก PHP 
                <?php if($swal_status != ""): ?>
                    swal({
                        type: '<?php echo $swal_status; ?>',
                        title: '<?php echo $swal_status == "success" ? "สำเร็จ" : "เกิดข้อผิดพลาด"; ?>',
                        text: '<?php echo $swal_msg; ?>',
                        timer: 2000,
                        showConfirmButton: false
                    });
                <?php endif; ?>
            });
        </script>

    </body>
</html>

Zerion Mini Shell 1.0