<?php include "inc/app-top-member.php"; ?>
<?php
$member_ibest_check = false;
$login = false;

if(isset($_SESSION["member_id"])){
    $login = true;
    if ($_SESSION["member_ibest"]) {
        $member_ibest_check = true;
        $member_ibest_id = $_SESSION["member_ibest"];
    }
} else {
    header('Location: https://eagro.com.my/login_register.php');
}

$result_product_detail = $eagro2->getPackageDetail($_GET["id"]);
$array_product_detail = json_decode($result_product_detail, true);

$result_review = $eagro2->getReviewDetail($_GET["id"]);
$array_review = json_decode($result_review, true);
// print_r($result_review);

$result_review_count = $eagro2->getReviewCount($_GET["id"]);
$array_review_count = json_decode($result_review_count, true);

$result_d2d_area = $eagro2->getd2dAreaList($_GET["id"]);
$array_d2d_area = json_decode($result_d2d_area, true);
?>

<!DOCTYPE html>
<html>
<head>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.9/sweetalert2.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.9/sweetalert2.min.js"></script>

    <title>E-Agro Product</title>
    <?php include "inc/head.php"; ?>

    <style>
        .swal2-icon.swal2-info{
            font-size: unset !important;
        }
        .swal2-icon.swal2-warning{
            font-size: unset !important;
        }
    </style>
</head>
<body>
<header>
    <?php include "inc/header.php"; ?>

</header>
<script>
    
    $(document).ready(function(){

        //check if member is in ibest
        var member_ibest_check = $('#member_ibest_check').val();
        var login = $('#login').val();
        var language = $("#language").val();
        // alert(member_ibest_check)
        if (login == 1) {

            if (member_ibest_check == "") {
                if(language == 0) {

                    swal({
                        type: 'info',
                        title: 'Not Authorised!',
                        text: "You are not allowed to access this page. Please browse in the products page"
                    }).then(function(){
                        window.location = 'products.php';
                    })


                } else {

                    swal({
                        type: 'info',
                        title: 'Tidak Dibenarkan!',
                        text: "Anda tidak dibenarkan akses di laman ini"
                    }).then(function(){
                        window.location = 'products.php';
                    })

                }
            }
            
        } else {

            if(language == 0) {

                swal({
                    type: 'info',
                    title: 'Please Login',
                    text: "Please login to browse package"
                }).then(function(){
                    window.location = 'login_register.php';
                })


            } else {

                swal({
                    type: 'info',
                    title: 'Sila Log Masuk',
                    text: "Sila log masuk untuk semak produk"
                }).then(function(){
                    window.location = 'login_register.php';
                })

            }

        }
    })


    function check() {
        location.href = "https://eagro.com.my/login_register.php";
    }

    function addToCart() {
        var language = $("#language").val();
        // alert(language);
        var cart_quantity = $("#cart-quantity").val();
        var product_id = $("#product-id").val();
        var member_id = $("#member-id").val();
        // alert(member_id);
        var FormData = {
            "action": "addToCart",
            "cart_quantity": cart_quantity,
            "product_id": product_id,
            "member_id": member_id
        };

        if (language == 1) {
            $.ajax({
                type: 'post',
                url: 'https://eagro.com.my/action.php',
                data: FormData,
                success: function (result) {
                    // console.log(result);
                    var result_obj = JSON.parse(result);
                    console.log(result_obj);

                    if (result_obj.valid) {
                        swal({
                            type: 'success',
                            title: 'Berjaya!',
                            text: 'Berjaya tambah item ke dalam troli '
                        }).then(function () {
                            location.reload();
                        })
                    } else {
                        swal({
                            type: 'error',
                            title: "Maaf..",
                            text: 'Sila cuba lagi'
                        })
                    }
                }
            });
        } else {
            $.ajax({
                type: 'post',
                url: 'https://eagro.com.my/action.php',
                data: FormData,
                success: function (result) {
                    // console.log(result);
                    var result_obj = JSON.parse(result);
                    console.log(result_obj);


                    if (result_obj.valid) {
                        swal({
                            type: 'success',
                            title: 'Success!',
                            html: result_obj.msg
                        }).then(function () {
                            location.reload();
                        })
                    } else {
                        swal({
                            type: 'error',
                            title: "Oops..",
                            html: result_obj.msg
                        })
                    }
                }
            });
        }

    }

    function takePackage(){
        var package_id = $("#package_id").val();

        swal({
        title: "Are you sure?",
        text: "Are you sure you want to take this package",
        type: "warning",
        showCancelButton: true,
        confirmButtonClass: "btn-danger",
        dangerMode: true,
        confirmButtonText: "Yes, I want this package",
        closeOnConfirm: false
        }).then((result) => {
            if (result.value) {
                // swal({
                //     title: "Success",
                //     text: "Successfully purchase package",
                //     type: 'success'
                // })
                window.location.replace('https://eagro.com.my/checkout_bulk.php?package_id='+package_id)
            } else {
                swal({
                    title: "Cancel",
                    text: "Cancelled purchase package",
                    type: 'info'
                })
            }
        });
    }
</script>

<input type="hidden" id="member_ibest_check" value="<?php echo $member_ibest_check; ?>">
<input type="hidden" id="login" value="<?php echo $login; ?>">
<input type="hidden" id="language" value="<?php echo $_SESSION["language"];?>">
<input type="hidden" id="package_id" value="<?php echo $_GET["id"];?>">

<div class="breadcrumb-area mb-50">

</div>
<div class="single-product-content ">
    <div class="container">
        <!--=======  single product content container  =======-->
        <div class="single-product-content-container mb-35">
            <div class="row">
                <div class="col-lg-6 col-md-12 col-xs-12">


                    <!-- product image gallery -->
                    <div class="product-image-slider d-flex flex-custom-xs-wrap flex-sm-nowrap align-items-center mb-sm-35">
                        <!--Modal Tab Content Start-->
                        <div class="tab-content product-large-image-list">
                            <div class="tab-pane fade show active" id="single-slide1" role="tabpanel"
                                 aria-labelledby="single-slide-tab-1">
                                <!--Single Product Image Start-->
                                <div class="single-product-img easyzoom img-full" align="center">
                                    <img src="/merchant/image/package/<?php echo $array_product_detail["products"][0]["package_image"]; ?>"
                                         class="img-fluid" alt="">
                                    <a href="/merchant/image/package/<?php echo $array_product_detail["products"][0]["package_image"]; ?>"
                                       class="big-image-popup"><i class="fa fa-search-plus"></i></a>
                                </div>
                                <!--Single Product Image End-->
                            </div>
                        </div>
                        <!--Modal Content End-->

                    </div>
                    <!-- end of product image gallery -->
                </div>
                <input type="hidden" id="language" value="<?php echo $_SESSION["language"]; ?>">
                <input type="hidden" id="product-id" value="<?php echo $_GET["id"]; ?>">
                <input type="hidden" id="member-id" value="<?php echo $_SESSION["member_id"]; ?>">

                <div class="col-lg-6 col-md-12 col-xs-12">
                    <!-- product quick view description -->
                    <div class="product-feature-details">
                        <h2 class="product-title mb-15"><?php echo $array_product_detail["products"][0]["package_name"]; ?></h2>

                        <p class="product-rating">
                            <!-- <i class="fa fa-star active"></i>
                            <i class="fa fa-star active"></i>
                            <i class="fa fa-star active"></i>
                            <i class="fa fa-star active"></i>
                            <i class="fa fa-star"></i>

                            <a href="#">(1 customer review)</a> -->
                            <?php if ($array_product_detail["products"][0]["product_brand"] != 0){ ?>
                        <h5 class="product-title mb-15"
                            style="color:#80bb01;">Package</h5>
                        <?php } else { ?>
                            <h5 class="product-title mb-15"
                                style="color:#80bb01;">Package</h5>

                        <?php } ?>
                        <h5 class="product-title mb-15"><?php echo change("pb"); ?> <a
                                    href="/merchant-profile.php?merchant_id=<?php echo $array_product_detail["products"][0]["merchant_id"]; ?>"
                                    style="color:#80bb01;"><?php echo $array_product_detail["products"][0]["merchant_company_name"]; ?></a>
                        </h5>


                        </p>

                        <h2 class="product-price mb-15">
                            <span class="discounted-price">RM <?php echo $array_product_detail["products"][0]["package_amount"] ?></span>
                            <!-- <span class="discounted-price"> $10.00</span> -->
                        </h2>

                        <!-- <p class="product-description mb-20"><?php echo $array_product_detail["product_description"]; ?></p> -->

                        <!-- <input type="hidden" value="<?php echo $array_product_detail["product_stock"]; ?>"
                               id="maxQuantity"> -->
                        <div class="cart-buttons mb-20">
                            <!-- <div class="pro-qty mr-20 mb-xs-20">
                                <input type="text" readonly value="1" max="10" id="cart-quantity">
                            </div> -->
                            <div class="add-to-cart-btn">
                                <?php if ($array_product_detail["products"][0]["product_stock"] > 0) { ?>

                                    <a onclick="<?php echo !isset($_SESSION["member_id"]) || $_SESSION["member_login"] != true ? 'check()' : 'takePackage()'; ?>"><i
                                                class="fa fa-shopping-cart"></i> <?php echo change("take_package"); ?></a>
                                <?php } else { ?>
                                    <a href="#" style="background-color:red;border-color:red;"><i
                                                class="fa fa-warning"></i> <?php echo change("ofs"); ?></a>
                                <?php } ?>
                            </div>
                            <!-- <p class="mt-10"><?php echo $array_product_detail["products"][0]["product_stock"]; ?> <?php echo change("piece"); ?></p> -->
                            <?php if ($array_product_detail["products"][0]["product_payment"] == 2) { ?>
                                <p class="product-rating">
                                    <!--<h5 class="product-title mb-15" style="font-size:14px;color:#80bb01;"><?php echo change("prode"); ?></h5>-->
                                </p>
                            <?php } ?>
                        </div>

                        <div class="single-product-action-btn mb-20">
                            <!-- <a href="#" data-tooltip="Add to wishlist"> <span class="icon_heart_alt"></span> Add to wishlist</a> -->
                            <!-- <a href="#" data-tooltip="Add to compare"> <span class="arrow_left-right_alt"></span> Add to compare</a> -->
                        </div>


                        <div class="single-product-category mb-20">
                            <h3><?php echo change("category2"); ?>: <span><a
                                            href="#"><?php echo $array_product_detail["products"][0]["product_category"]; ?></a></h3>
                            <?php if ($array_product_detail["products"][0]["product_d2d_service"] == 1) { ?>
                                <h3><?php echo change("d2d"); ?>:
                                    <?php if ($array_d2d_area["valid"]) { ?>
                                        <?php for ($i = 0; $i < count($array_d2d_area["data"]); $i++) { ?>
                                            <span>[ <a><?php echo $array_d2d_area["data"][$i]["malaysia_cities"]; ?></a> ]</span>
                                        <?php }
                                    } ?>
                                </h3>
                            <?php } ?>
                        </div>

                        <?php
                        if (empty($array_product_detail["products"][0]['merchant_company_address']) === false
                            && empty($array_product_detail["products"][0]['merchant_company_postcode']) === false
                            && empty($array_product_detail["products"][0]['merchant_company_country']) === false
                            && empty($array_product_detail["products"][0]['state_name']) === false
                            && empty($array_product_detail["products"][0]['city_name']) === false
                        ) {
                            ?>
                            <div class="single-product-category mb-20">
                                <h3>
                                    Pick up Location: <br>
                                    <span>
                                        <a>
                                              <?php
                                              echo $array_product_detail["products"][0]['merchant_company_address'] . " ";

                                              if (empty($array_product_detail["products"][0]['merchant_company_address_2']) === true) {
                                                  echo $array_product_detail["products"][0]['merchant_company_address_2'] . ", <br>";
                                              }

                                              echo $array_product_detail["products"][0]['merchant_company_postcode'] . ', <br>';
                                              echo $array_product_detail["products"][0]['city_name'] . ', <br>';
                                              echo $array_product_detail["products"][0]['state_name'];

                                              ?>
                                        </a>
                                    </span>
                                </h3>
                            </div>
                            <?php
                        }
                        ?>

                        <!-- <div class="social-share-buttons">
                            <h3>share this product</h3>
                            <ul>
                                <li><a class="twitter" href="#"><i class="fa fa-twitter"></i></a></li>
                                <li><a class="facebook" href="#"><i class="fa fa-facebook"></i></a></li>
                                <li><a class="google-plus" href="#"><i class="fa fa-google-plus"></i></a></li>
                                <li><a class="pinterest" href="#"><i class="fa fa-pinterest"></i></a></li>
                            </ul>
                        </div> -->
                    </div>
                    <!-- end of product quick view description -->
                </div>
            </div>
        </div>

        <!--=======  End of single product content container  =======-->

    </div>

</div>


<div class="single-product-tab-section mb-35">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="tab-slider-wrapper">
                    <nav>
                        <div class="nav nav-tabs" id="nav-tab" role="tablist">
                            <!-- <a class="nav-item nav-link active" id="products-tab" data-toggle="tab"
                               href="#products" role="tab"
                               aria-selected="true"><?php echo change("products"); ?></a> -->
                            <a class="nav-item nav-link" id="description-tab" data-toggle="tab"
                               href="#description" role="tab"
                               aria-selected="true"><?php echo change("desc"); ?></a>
                            <!-- <a class="nav-item nav-link" id="features-tab" data-toggle="tab" href="#Ingredient"
                               role="tab"
                               aria-selected="false"><?php echo change("ingredient"); ?></a>
                            <a class="nav-item nav-link" id="review-tab" data-toggle="tab" href="#review" role="tab"
                               aria-selected="false"><?php echo change("review"); ?>
                                (<?php echo $array_review_count["countReview"]; ?>)</a> -->
                        </div>
                    </nav>
                     <div class="tab-content" id="nav-tabContent">
                       <!-- <div class="tab-pane fade show active" id="products" role="tabpanel"
                             aria-labelledby="products-tab">
                             <?php for($i = 0; $i < count($array_product_detail["products"]); $i++) {?>
                                <div class="single-block d-flex">
                                    <div class="image">
                                        <img src="merchant/image/product/<?php echo $array_product_detail["products"][$i]["product_path"];?>" class="img-fluid" alt="">
                                    </div>
                                    <div class="content">
                                        <p>
                                            <?php echo $array_product_detail["products"][$i]["product_name"];?>
                                            <span>
                                                RM <?php echo $array_product_detail["products"][$i]["product_price"];?>
                                            </span>
                                            <span>
                                                Quantity <?php echo $array_product_detail["products"][$i]["quantity"]; ?>
                                            </span>
                                        </p>
                                    </div>
                                </div>
                            <?php } ?>
                        </div> -->
                        <div class="tab-pane fade show active" id="description" role="tabpanel"
                             aria-labelledby="description-tab">
                            <p class="product-desc"><?php echo $array_product_detail["products"][0]["package_description"]; ?></p>
                        </div>
                        <!--<div class="tab-pane fade" id="Ingredient" role="tabpanel" aria-labelledby="features-tab">
                            <p class="product-desc"><?php echo $array_product_detail["product_ingredient"]; ?></p>

                        </div>
                        <div class="tab-pane fade" id="review" role="tabpanel" aria-labelledby="review-tab">
                            <div class="product-ratting-wrap">
                                <div class="rattings-wrapper">
                                    <?php if ($array_review["valid"]) { ?>
                                        <?php for ($i = 0; $i < count($array_review["data"]); $i++) { ?>
                                            <div class="sin-rattings">
                                                <div class="ratting-author">
                                                    <h3><?php echo $array_review["data"][$i]["member_name"]; ?></h3>
                                                    <div class="ratting-star">
                                                        <?php for ($j = 0; $j < $array_review["data"][$i]["product_review_star"]; $j++) { ?>
                                                            <span class="fa fa-star" style="color:#FDE16D"></span>
                                                        <?php }
                                                        $check = $array_review["data"][$i]["product_review_star"];
                                                        $uncheck = 5 - $check;
                                                        for ($j = 0; $j < $uncheck; $j++) {
                                                            ?>
                                                            <span class="fa fa-star" style="color:grey"></span>

                                                        <?php } ?>
                                                        <span>(<?php echo $array_review["data"][$i]["product_review_star"]; ?>)</span>
                                                    </div>
                                                </div>
                                                <p><?php echo $array_review["data"][$i]["product_review_comment"]; ?></p>
                                            </div>
                                        <?php }
                                    } ?>
                                </div>

                            </div>
                        </div> -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--=====  End of My account page section  ======-->


<!--=============================================
=            Footer         =
=============================================-->

</body>
<?php include "inc/inc-js.php"; ?>
<footer>
    <?php include "inc/footer.php"; ?>
</footer>
</html>
