<?php

$umessage = '';
include('./function/function.php');
check_session();
if (isset($_POST['addsubproduct'])) {
    $id = 0;
    extract($_POST);
    $type = 1;
    $pdo = getPDOObject();
    $posted_data = $_POST;
    // $sql = $pdo->query("SELECT * FROM `client4` where  name LIKE '$name'");
    // $num = $sql->rowCount();
    $num = 0;

    $photos = $_FILES['photo']['name'];
    $Filename = '';

    $photos2 = $_FILES['photo2']['name'];
    $Filename2 = '';

    $photos3 = $_FILES['photo3']['name'];
    $Filename3 = '';

    $photos4 = $_FILES['photo4']['name'];
    $Filename4 = '';

    if (!$num) {
        if ($photos) {
            $Filename = date('dmyhis') . basename($_FILES['photo']['name']);
            $posted_data['photo'] = $Filename;
            $target = "../upload/" . $Filename;
            move_uploaded_file($_FILES['photo']['tmp_name'], $target); //Tells you if its all ok	
        }

        if ($photos2) {
            $Filename2 = date('dmyhis') . basename($_FILES['photo2']['name']);
            $posted_data['photo2'] = $Filename2;
            $target = "../upload/" . $Filename2;
            move_uploaded_file($_FILES['photo2']['tmp_name'], $target); //Tells you if its all ok	
        }
        if ($photos3) {
            $Filename3 = date('dmyhis') . basename($_FILES['photo3']['name']);
            $posted_data['photo3'] = $Filename3;
            $target = "../upload/" . $Filename3;
            move_uploaded_file($_FILES['photo3']['tmp_name'], $target); //Tells you if its all ok	
        }

        if ($photos4) {
            $Filename4 = date('dmyhis') . basename($_FILES['photo4']['name']);
            $posted_data['photo4'] = $Filename4;
            $target = "../upload/" . $Filename4;
            move_uploaded_file($_FILES['photo4']['tmp_name'], $target); //Tells you if its all ok	
        }

        $affected_rows = insert('subproduct', $posted_data);
        if ($affected_rows)
            if ($affected_rows)
                $umessage = '<div class="alert alert-success" role="alert">
							<strong></strong>Added Successfully
						   </div>';
    } else {
        $umessage = '<div class="alert alert-danger" role="alert">Duplicate Entry!!! Code Already Exists </div> ';
    }
}

if ($_GET['action'] == 'delete') {
    $sql_del = sqlfetch("delete from subproduct where id='" . $_GET['id'] . "'");
    echo "<script>window.open('product.php','_self')</script>";
}

if (isset($_POST['deleteall'])) {
    $arr = $_POST['ids'];
    if (count($arr)) {
        $str_rest_refs = implode(",", $arr);

        $data = sqlfetch("select * from `subproduct` where id in ($str_rest_refs)");
        foreach ($data as $subproduct) {
            $img_path = '../upload/' . $subproduct['photo'];
            if (file_exists($img_path)) {
                @unlink($img_path);
            }
        }

        $pdo = getPDOObject();
        $q = $pdo->query("DELETE FROM `subproduct` WHERE id in ($str_rest_refs)");
        if ($q)
            $umessage = '<div class="alert alert-success" role="alert">
							<strong></strong>Deleted Successfully
						   </div>';
    } else {
        $umessage = '<div class="alert alert-danger" role="alert">
							<strong></strong>Please Select Items to perform this action
						   </div>';
    }
}

if (isset($_POST['activate'])) {
    $arr = $_POST['ids'];
    if (count($arr)) {
        $str_rest_refs = implode(",", $arr);
        $pdo = getPDOObject();
        $q = $pdo->query("UPDATE `subproduct` SET actstat='1' WHERE id in ($str_rest_refs)");
        if ($q)
            $umessage = '<div class="alert alert-success" role="alert">
							<strong></strong>Activated Successfully
						   </div>';
    } else {
        $umessage = '<div class="alert alert-danger" role="alert">
							<strong></strong>Please Select Items to perform this action
						   </div>';
    }
}

if (isset($_POST['deactivate'])) {
    $arr = $_POST['ids'];
    if (count($arr)) {
        $str_rest_refs = implode(",", $arr);
        $pdo = getPDOObject();
        $q = $pdo->query("UPDATE `subproduct` SET actstat='0' WHERE id in ($str_rest_refs)");
        if ($q)
            $umessage = '<div class="alert alert-success" role="alert">
							<strong></strong>DeActivated Successfully
						   </div>';
    } else {
        $umessage = '<div class="alert alert-danger" role="alert">
							<strong></strong>Please Select Items to perform this action
						   </div>';
    }
}

if (isset($_POST['editdone'])) {
    extract($_POST);
    $posted_data = $_POST;
    $Filename = $prevphoto;
    $photos = $_FILES['photo']['name'];
    if ($photos != '') {
        $Filename = '';

        $Filename = date('dmyhis') . basename($_FILES['photo']['name']);
        $posted_data['photo'] = $Filename;
        $target = "../upload/" . $Filename;
        move_uploaded_file($_FILES['photo']['tmp_name'], $target); //Tells you if its all ok	
        $img_path = '../upload/' . $prevphoto;
        if (file_exists($img_path)) {
            @unlink($img_path);
        }
    }
    $Filename2 = $prevphoto2;
    $photos2 = $_FILES['photo2']['name'];
    if ($photos2 != '') {
        $Filename2 = '';

        $Filename2 = date('dmyhis') . basename($_FILES['photo2']['name']);
        $posted_data['photo2'] = $Filename2;
        $target = "../upload/" . $Filename2;
        move_uploaded_file($_FILES['photo2']['tmp_name'], $target); //Tells you if its all ok	
        $img_path = '../upload/' . $prevphoto2;
        if (file_exists($img_path)) {
            @unlink($img_path);
        }
    }

    $Filename3 = $prevphoto3;
    $photos3 = $_FILES['photo3']['name'];
    if ($photos3 != '') {
        $Filename3 = '';

        $Filename3 = date('dmyhis') . basename($_FILES['photo3']['name']);
        $posted_data['photo3'] = $Filename3;
        $target = "../upload/" . $Filename3;
        move_uploaded_file($_FILES['photo3']['tmp_name'], $target); //Tells you if its all ok	
        $img_path = '../upload/' . $prevphoto3;
        if (file_exists($img_path)) {
            @unlink($img_path);
        }
    }

    $Filename4 = $prevphoto4;
    $photos4 = $_FILES['photo4']['name'];
    if ($photos4 != '') {
        $Filename4 = '';

        $Filename4 = date('dmyhis') . basename($_FILES['photo4']['name']);
        $posted_data['photo4'] = $Filename4;
        $target = "../upload/" . $Filename4;
        move_uploaded_file($_FILES['photo4']['tmp_name'], $target); //Tells you if its all ok	
        $img_path = '../upload/' . $prevphoto4;
        if (file_exists($img_path)) {
            @unlink($img_path);
        }
    }

    $affected_rows = update('subproduct', $posted_data, array('id' => $pid));
    if ($affected_rows)
        $umessage = '<div class="alert alert-success" role="alert">
							<strong></strong>Updated Successfully
						   </div>';
}

function subproduct_form(
    $pid = '0',
    $name = '',
    $photo = '',
    $des = '',
    $photo2 = '',
    $photo3 = '',
    $photo4 = '',
    $pro_lable = '',
    $pro_specification = '',
    $pro_mainprice = '',
    $pro_discountprice = '',
    $pro_shortdes = '',
    $pro_additionalinfo = '',
    $youtube = '',
    $fld_order = '0',
    $link = '',
    $subcat = 0,
    $actstat = '',
    $formname = 'addsubproduct'
) { ?>





    <form action="product.php" method="post" enctype="multipart/form-data">
        <div class="form theme-form">
            <input type="hidden" name="pid" value="<?php echo $pid; ?>" />
            <input type="hidden" name="prevphoto" value="<?php echo $photo; ?>" />
            <input type="hidden" name="prevphoto2" value="<?php echo $photo2; ?>" />

            <input type="hidden" name="prevphoto3" value="<?php echo $photo3; ?>" />
            <input type="hidden" name="prevphoto4" value="<?php echo $photo4; ?>" />
            <div class="row">
                <div class="col-lg-6  mt-3">
                    <div class="row">
                        <div class="col-lg-12  mt-4">
                            <div class="input-group input-group-merge">
                                <span class="input-group-text">@</span>
                                <div class="form-floating form-floating-outline">
                                    <input type="text" class="form-control" name="name" placeholder="Name " aria-label="Name" value="<?php echo $name; ?>" />
                                    <label for="basic-addon11"> Tour Name </label>
                                </div>
                            </div>
                        </div>


                        <div class="col-lg-12  mt-4">
                            <div class="input-group input-group-merge">
                                <span class="input-group-text">@</span>
                                <div class="form-floating form-floating-outline">
                                    <input type="text" class="form-control" name="pro_lable" placeholder="Name " aria-label="Name" value="<?php echo $pro_lable; ?>" />
                                    <label for="basic-addon11"> Tour Date </label>
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-12  mt-4">
                            <div class="input-group input-group-merge">
                                <span class="input-group-text">@</span>
                                <div class="form-floating form-floating-outline">
                                    <input type="text" class="form-control" name="pro_specification" placeholder="Name " aria-label="Name" value="<?php echo $pro_specification; ?>" />
                                    <label for="basic-addon11">Tour Location</label>
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-12  mt-4">
                            <div class="input-group input-group-merge">
                                <span class="input-group-text">@</span>
                                <div class="form-floating form-floating-outline">
                                    <input type="text" class="form-control" name="pro_mainprice" placeholder="Name " aria-label="Name" value="<?php echo $pro_mainprice; ?>" />
                                    <label for="basic-addon11">Tour Person</label>
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-12  mt-4">
                            <div class="input-group input-group-merge">
                                <span class="input-group-text">@</span>
                                <div class="form-floating form-floating-outline">
                                    <input type="number" class="form-control" name="pro_discountprice" placeholder="Name " aria-label="Name" value="<?php echo $pro_discountprice; ?>" />
                                    <label for="basic-addon11"> Tour Price</label>
                                </div>
                            </div>
                        </div>



                        <div class="col-lg-12  mt-4">
                            <div class="form-floating form-floating-outline">
                                <select class="form-select" name="actstat" id="selectError" data-rel="chosen">
                                    <option <?php if (($actstat) == '1')
                                                echo 'selected'; ?> value="1">Active</option>
                                    <option <?php if (($actstat) == '0')
                                                echo 'selected'; ?> value="0">Inactive</option>
                                </select>
                                <label for="floatingSelect">Status</label>
                            </div>
                        </div>





                    </div>
                </div>

                <div class="col-lg-6  mt-3">

                    <div class="col-lg-12  mt-4">
                        <div class="form-floating form-floating-outline">
                            <select id="weightUnits" class="select2 form-select" name="subcat" id="selectError" data-rel="chosen">

                                <option>SELECT Subcategory</option>
                                <?php
                                $categories = sqlfetch("SELECT * FROM `subcategory` order by fld_order");
                                foreach ($categories as $category) {
                                    $select = '';
                                    if (($subcat == ($category['id'])))
                                        $select = 'selected';
                                    echo '<option ' . $select . ' value="' . $category['id'] . '">' . $category['name'] . ' (' . $category['id'] . ')</option>';
                                }
                                ?>
                            </select>
                            <label for="floatingSelect">Select Subategory</label>
                        </div>
                    </div>

                    <div class="col-lg-12 mt-4">
                        <div class="input-group input-group-merge">

                            <span class="input-group-text">@</span>

                            <div class="form-floating form-floating-outline">
                                <input type="file" class="form-control" name="photo" aria-label="Upload" value="<?php echo $photo; ?>">
                                <label for="basic-addon11">Banner Photo (1510 x 376)</label>

                            </div>
                            <img class="grayscale img-responsive" alt="" src="../upload/<?php echo $photo; ?>" style="    width: 50px;margin-right: 16px;border-radius: 16px;">
                        </div>

                    </div>

                    <div class="col-lg-12 mt-4">
                        <div class="input-group input-group-merge">

                            <span class="input-group-text">@</span>

                            <div class="form-floating form-floating-outline">
                                <input type="file" class="form-control" name="photo2" aria-label="Upload" value="<?php echo $photo2; ?>">
                                <label for="basic-addon11"> Package Photo </label>

                            </div>
                            <img class="grayscale img-responsive" alt="" src="../upload/<?php echo $photo2; ?>" style="    width: 50px;margin-right: 16px;border-radius: 16px;">
                        </div>

                    </div>
                    <div class="col-lg-12 mt-4" style="display: none;">
                        <div class="input-group input-group-merge">

                            <span class="input-group-text">@</span>

                            <div class="form-floating form-floating-outline">
                                <input type="file" class="form-control" name="photo3" aria-label="Upload" value="<?php echo $photo3; ?>">
                                <label for="basic-addon11">Photo 3</label>


                            </div>
                            <img class="grayscale img-responsive" alt="" src="../upload/<?php echo $photo3; ?>" style="    width: 50px;margin-right: 16px;border-radius: 16px;">
                        </div>

                    </div>
                    <div class="col-lg-12 mt-4">
                        <div class="input-group input-group-merge">

                            <span class="input-group-text">@</span>

                            <div class="form-floating form-floating-outline">
                                <input type="file" class="form-control" name="photo4" aria-label="Upload" value="<?php echo $photo4; ?>">
                                <label for="basic-addon11">Upload Itnirary Pdf</label>


                            </div>
                            <img class="grayscale img-responsive" alt="" src="../upload/<?php echo $photo4; ?>" style="    width: 50px;
                                      margin-right: 16px;
                                  border-radius: 16px;">
                        </div>

                    </div>
                    <div class="col-lg-12 mt-4">
                        <div class="input-group input-group-merge">
                            <span class="input-group-text">@</span>
                            <div class="form-floating form-floating-outline">
                                <input type="number" class="form-control" placeholder="Sort Order" aria-label="Description" name="fld_order" value="<?php echo $fld_order; ?>" />
                                <label for="basic-addon11">Sort Order</label>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-12  mt-4">

                        <div class="input-group input-group-merge">
                            <span class="input-group-text">@</span>
                            <div class="form-floating form-floating-outline">
                                <input type="text" class="form-control" name="youtube" placeholder="Name " aria-label="Name" value="<?php echo $youtube; ?>" />
                                <label for="basic-addon11"> Youtube Link</label>
                            </div>
                        </div>

                    </div>
                </div>



                <div class="col-lg-12  mt-4">
                    <div class="input-group input-group-merge">

                        <div class="form-floating form-floating-outline">
                            <label for="basic-addon11">Description Text</label>
                            <br><br>
                            <textarea id="mytextarea" name="des" cols="60" rows="10"><?php echo $des; ?></textarea>
                        </div>
                    </div>
                    <div class="col-lg-12  ">
                        <div class="form-floating form-floating-outline">
                            <label for="basic-addon11">Itinerary </label>
                            <br><br>


                            <textarea id="mytextarea" name="pro_shortdes" cols="60" rows="10"><?php echo $pro_shortdes; ?></textarea>
                        </div>
                    </div>
                </div>
                <div class="col-lg-12  mt-4">

                    <div class="input-group input-group-merge">

                        <div class="form-floating form-floating-outline">
                            <label for="basic-addon11">Other Info</label>
                            <br><br>
                            <textarea id="mytextarea" name="pro_additionalinfo" cols="60" rows="10"><?php echo $pro_additionalinfo; ?></textarea>
                        </div>
                    </div>
                </div>


                <div class="col-lg-12  mt-5">
                    <div class="input-group input-group-merge">
                        <button class="btn btn-primary waves-effect  waves-light" type="submit" value="Submit" name="<?php echo $formname; ?>">
                            <span class=" align-middle">Submit</span>
                        </button>
                    </div>
                </div>



            </div>



        </div>
    </form>



<?php
}


?>
















<?php require('include/header.php'); ?>
<div class="content-wrapper">
    <div class="container-xxl flex-grow-1 container-p-y">


        <h4 class="py-3 mb-4"><span class="text-muted fw-light">Home/</span> Categories </h4>


        <?php echo $umessage; ?>


        <?php
        if (isset($_GET['edit']) and ($_GET['edit'] == 'true')) { ?>

            <div class="row">
                <div class="col-md-12">
                    <div class="card mb-4">
                        <h5 class="card-header">Manage Here</h5>
                        <div class="card-body demo-vertical-spacing demo-only-element z4">
                            <div class="row">
                                <?php
                                $pid = $_GET['pid'];
                                $productdata = sqlfetch("SELECT * FROM `subproduct` where id='$pid' ");
                                foreach ($productdata as $product) {
                                    extract($product);
                                    subproduct_form(
                                        $pid,
                                        $name,
                                        $photo,
                                        $des,
                                        $photo2,
                                        $photo3,
                                        $photo4,
                                        $pro_lable,
                                        $pro_specification,
                                        $pro_mainprice,
                                        $pro_discountprice,
                                        $pro_shortdes,
                                        $pro_additionalinfo,
                                        $youtube,
                                        $fld_order,
                                        $link,
                                        $subcat,
                                        $actstat,
                                        $formname = 'editdone'
                                    );
                                } ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


        <?php
        } else {
        ?>

            <div class="row">
                <div class="col-md-12">
                    <div class="card mb-4">
                        <h5 class="card-header">Manage Here</h5>
                        <div class="card-body demo-vertical-spacing demo-only-element z4">
                            <div class="row">
                                <?php subproduct_form(); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>









            <div class="card">
                <div class="card-datatable dataTable_select text-nowrap table-responsive">
                    <table id="tableID" class="display dt-select-table table table-bordered" style="width:100%">
                        <thead>
                            <tr>
                            <tr>
                                <th>S. No.</th>
                                <th>Name</th>
                                <th>Photo</th>
                                <th>Package ID</th>
                                <th>Sort Order</th>
                                <th>Status</th>
                                <th>Edit</th>
                                <th>Delet</th>
                            </tr>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $count = 1;
                            $data = sqlfetch("SELECT * FROM `subproduct`  order by fld_order");
                            foreach ($data as $menu) { ?>
                                <tr>
                                    <td>
                                        <?php echo $count++; ?>
                                    </td>

                                    <td>
                                        <?php echo $menu['name']; ?>
                                    </td>
                                    <td><img style="height: 32px;width: 32px;" src="../upload/<?php echo $menu['photo']; ?>" class="img-responsive"></td>
                                    <td><?php echo $menu['id']; ?></td>

                                    <td>

                                        <?php echo $menu['fld_order']; ?>
                                    </td>

                                    <td>
                                        <?php echo get_active_status_text($menu['actstat']); ?>
                                    </td>
                                    <td>
                                        <!-- <input class="checkhour" name="ids[]" value="<?php echo $menu['id']; ?>" type="checkbox" /> -->

                                        <a class="ajax-link" href="product.php?&pid=<?php echo $menu['id']; ?>&edit=true">
                                            <button type="button" class="btn btn-xs btn-danger pull-right" name="editclient"><i class="fa fa-pencil"></i> </button>
                                        </a>
                                    </td>
                                    <td>


                                        <a href="product.php?id=<?php echo $menu['id']; ?>&action=delete" onclick="return confirm('Are you sure you want to delete this item?');">
                                            <button type="submit" class="btn btn-xs btn-danger pull-right" style="margin:0px 10px;" name="delete"><i class="fa-solid fa-trash"></i></button>
                                        </a>

                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>










        <?php } ?>


        <?php require('include/footer.php'); ?>