<?php
include 'inc/connect.php';
$acc_ch = strip_tags(addslashes($_POST['acc_ch']));
$accountup = isset($_POST['accountup']) ? strip_tags(addslashes($_POST['accountup'])) : 0;
//////////////////////////////////
if ($acc_ch == 1) {
    if (empty($accountup)) {
        ?>
            <option value="">برجاء إختر حساب المستوى الاول أولا</option>
        <?php
    } else {
        ?>
        <option value="">إختر الحساب</option>
        <?php
        $sqlname = $con->prepare("SELECT * from level_acc WHERE acc_up='$accountup'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
        ?>
            <option value="<?php echo $name['ID']; ?>"><?php echo $name['name_account']; ?></option>
        <?php
        }
    } 
} elseif ($acc_ch == 2) {
    if (empty($accountup)) {
        ?>
            <option value="">برجاء إختر حساب المستوى الثانى أولا</option>
        <?php
    } else {
        ?>
        <option value="">إختر الحساب</option>
        <?php
        $sqlname = $con->prepare("SELECT * from level_acc WHERE acc_up='$accountup'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
        ?>
            <option value="<?php echo $name['ID']; ?>"><?php echo $name['name_account']; ?></option>
        <?php

        }
    }
} elseif ($acc_ch == 3) {
    $group_types = strip_tags(addslashes($_POST['group_types']));
    if (empty($group_types)) {
        ?>
            <option value="">برجاء إختر القسم أولا</option>
        <?php
    } else {
        ?>
        <option value="">إختر القسم</option>
        <?php
        $sqlname = $con->prepare("SELECT * from types_goods WHERE id_group_types = '$group_types'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
            ?>
            <option value="<?php echo $name['ID']; ?>"><?php echo $name['NameType']; ?></option>
            <?php

        }
    }
} elseif ($acc_ch == 4) {
    $cust = strip_tags(addslashes($_POST['cust']));
    if (empty($cust)) {
        $sqlname = $con->prepare("SELECT * from projects where operation = 0");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
        ?>
            <option value="<?php echo $name['ID']; ?>"><?php echo $name['name_project']; ?></option>
        <?php
        }
    } else {
        ?>
        <option value="">إختر المشروع</option>
        <?php
        $sqlname = $con->prepare("SELECT * from projects WHERE id_cust = '$cust'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
        ?>
            <option value="<?php echo $name['ID']; ?>"><?php echo $name['name_project']; ?></option>
        <?php
        }
    }
} elseif ($acc_ch == 5) {
    $project = strip_tags(addslashes($_POST['project']));
    if (empty($project)) {
        $stmt = $con->prepare("SELECT * FROM  cust_import WHERE TypeID = 0 ORDER BY ID ASC");
        $stmt->execute();
        $rows = $stmt->fetchAll();
        if (!empty($rows)) {
            foreach ($rows as $row) {
                if ($row['vip'] == 1) {
        ?>
                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['Name_CI']; ?></option>
                <?php
                }
            }
            foreach ($rows as $row) {
                if ($row['vip'] == 0) {
                ?>
                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['Name_CI']; ?></option>
            <?php
                }
            }
        } else {
            ?>
            <option value="">لا يوجد عملاء حتى الأن</option>
        <?php
        }
    } else {
        $sqlname = $con->prepare("SELECT * from projects WHERE ID = '$project'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
        $id_cust = $name['id_cust'];
        }
        $sqlname = $con->prepare("SELECT * from cust_import WHERE ID = '$id_cust'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
        ?>
            <option value="<?php echo $name['ID']; ?>"><?php echo $name['Name_CI']; ?></option>
        <?php
        }
    }
} elseif ($acc_ch == 6) {
    $group_types = strip_tags(addslashes($_POST['group_types']));
    if (empty($group_types)) {
        ?>
            <option value="">برجاء إختر القسم أولا</option>
        <?php
    } else {
        ?>
        <option value="">إختر القسم</option>
        <option value="0">إختر الكل</option>
        <?php
        $sqlname = $con->prepare("SELECT * from types_goods WHERE id_group_types = '$group_types'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        foreach ($names as $name) {
            ?>
            <option value="<?php echo $name['ID']; ?>"><?php echo $name['NameType']; ?></option>
            <?php

        }
    }
} elseif ($acc_ch == 7) {
    if (empty($accountup)) {
        ?>
        <label for="account_id">إختر الحساب</label>
        <select id="account_id" name="account" class="form-control" required>
            <option value="">برجاء إختر حساب الاب أولا</option>
        </select>
        <?php
    } else {
        $sqlname = $con->prepare("SELECT * from level_acc WHERE acc_up = '$accountup'");
        $sqlname->execute();
        $names = $sqlname->fetchAll();
        if (!empty($names)) {
            foreach ($names as $name) {
                ?>
                    <option value="<?php echo $name['ID']; ?>"><?php echo $name['name_account']; ?></option>
                <?php
            }
        } else {
            $sqlname = $con->prepare("SELECT * from level_acc WHERE ID = '$accountup'");
            $sqlname->execute();
            $names = $sqlname->fetchAll();
            foreach ($names as $name) {
                ?>
                    <option value="<?php echo $name['ID']; ?>"><?php echo $name['name_account']; ?></option>
                <?php
            }
        }
    }
} elseif ($acc_ch == 8) {

    $currentYear = date("Y");
    $sqlLastRegistrationNumber = "SELECT MAX(registration_number) AS last_ID FROM yomya
    WHERE YEAR(dateinsert) = :currentYear";

    $account = $con->prepare($sqlLastRegistrationNumber);
    $account->bindParam(":currentYear", $currentYear, PDO::PARAM_INT);
    $account->execute();

    $resultLastRegistrationNumber = $account->fetch(PDO::FETCH_ASSOC);
    if (!empty($resultLastRegistrationNumber['last_ID'])) {
        echo $resultLastRegistrationNumber['last_ID'] + 1;
    } else {
        echo 1;
    }
/* 
    $account = $con->prepare("SELECT MAX(registration_number) as last_ID FROM yomya");
    $account->execute(); */
    /* $account1s = $account->fetchAll();
    foreach ($account1s as $key => $account1) {
        $last_ID_yomya = $account1['last_ID'];
        if (!empty($last_ID_yomya)) {
            echo $last_ID_yomya + 1;
        } else {
            echo 1;
        }
    } */
}