<?php
//-----------------------Do not edit the below ----------------------//
require_once('DB.php');
class App_DB extends DB
{
    public function __construct($db_details_ary = array())
    {
        try {
            if (isset($db_details_ary) && !empty($db_details_ary)) {
                parent::__construct($db_details_ary);
            } else {
                echo ("Invalid User Attempt To Database!");
                exit();
            }
        } catch (Exception $exc) {
            $tempVar = $exc->getTraceAsString();
        }
    }
    //---------------------Do not edit the above --------------------//

    //function autofill / auto complete
    public function autoFill($sql, $fld)
    {
        $value = '';
        $result = $this->getRecordSet($sql);
        if (count($result) > 0) {
            foreach ($result as $row) {
                $value .= '"' . $row[$fld] . '",';
            }
            $len = strlen($value);
            $value = substr($value, 0, ($len - 1));
        }
        return $value;
    }

    //function autofill / auto complete for two fileds return, grouping fields
    public function autoFillGroup($sql, $fld1, $fld2)
    {
        $value = '';
        $result = $this->getRecordSet($sql);
        if (count($result) > 0) {
            foreach ($result as $row) {
                $value .= '"' . $row[$fld1] . ' V/S ' . $row[$fld2] . '",';
            }
        }
        return $value;
    }


    //function to create shoba combo
    public function shobaCmb($cmb, $classCss = "frmSelect", $scripts = "")
    {
        $sql = "SELECT sno,shoba FROM shobajaat WHERE is_active = 1 ORDER BY sno ASC";
        $options = '<select name="' . $cmb . '" id="' . $cmb . '" class="' . $classCss . '" ' . $scripts . '>';
        $options .= '<option value=""></option>';
        $result = $this->getRecordSet($sql);
        if (count($result) > 0) {
            foreach ($result as $row) {
                $options .= '<option value="' . $row['sno'] . '"> ' . $row['shoba'] . ' </option>' . PHP_EOL;
            }
        } else {
            $options .= "<option value=''>شعبہ جات خالی ہیں</option>";
        }
        $options .= "</select>";
        echo ($options);
    }

    //functon to create darajaat combo
    // public function darjaatCmb($cmbName, $otherOptions = "", $classCss = "form-select")
    // {
    //     $sql = "SELECT sno,shoba FROM shobajaat WHERE is_active = 1 ORDER BY sno ASC";
    //     echo ('<select name="' . $cmbName . '" id="' . $cmbName . '" class="' . $classCss . '" ' . $otherOptions . '>');
    //     echo ('<option value="">درجہ منتخب کریں</option>');
    //     $result = $this->getRecordSet($sql);
    //     foreach ($result as $row1) {
    //         echo ('<optgroup label="' . $row1['shoba'] . '">');
    //         $sqlDarja = "SELECT derjaCode,shoba_sno,darja FROM darjaat WHERE shoba_sno = " . $row1['sno'];
    //         $resultDarja = $this->getRecordSet($sqlDarja);
    //         foreach ($resultDarja as $row2) {
    //             echo ('<option value="' . $row2['derjaCode'] . '"> ' . $row2['darja'] . ' </option>') . PHP_EOL;
    //         }
    //         echo ('</optgroup>');
    //     }
    //     echo ("</select>");
    // }

    public function darjaatCmb($cmbName, $otherOptions = "", $classCss = "form-select text-end urdu-regular w-100", $selectedValue = "")
    {
        $sql = "SELECT sno, shoba FROM shobajaat WHERE is_active = 1 ORDER BY sno ASC";
        echo '<select name="' . htmlspecialchars($cmbName) . '" 
                 id="' . htmlspecialchars($cmbName) . '" 
                 class="' . htmlspecialchars($classCss) . '" 
                 dir="rtl" ' . $otherOptions . '>';
        echo '<option value="">درجہ منتخب کریں</option>';
        $result = $this->getRecordSetFilled($sql);
        foreach ($result as $row1) {
            echo '<optgroup label="' . htmlspecialchars($row1['shoba']) . '">';
            $sqlDarja = "SELECT derjaCode, shoba_sno, darja 
                     FROM darjaat 
                     WHERE shoba_sno = " . (int) $row1['sno'] . " 
                     ORDER BY derjaCode ASC";
            $resultDarja = $this->getRecordSetFilled($sqlDarja);
            foreach ($resultDarja as $row2) {
                $selected = ($row2['derjaCode'] == $selectedValue) ? 'selected' : '';
                echo '<option value="' . htmlspecialchars($row2['derjaCode']) . '" ' . $selected . '>'
                    . htmlspecialchars($row2['darja']) .
                    '</option>' . PHP_EOL;
            }
            echo '</optgroup>';
        }
        echo '</select>';
    }
}//DB()
