<?php session_start();
include("includes/db_config.php"); ?>
<?php if(isset($_POST['bill_addr']))
{
    //var_dump($_POST);
extract($_POST);
$id=$_SESSION['id'];
$sql1 ="UPDATE  user_profile  SET fname='$name',country='$country',state='$state',city='$city',addr='$address',pincode='$pincode',landmark='$landmark' WHERE id='".$id."'"; 
$res=mysqli_query($conn,$sql1) or die(mysqli_error());
}
if(isset($_POST['ship_addr'])){
extract($_POST);
date_default_timezone_set('Asia/Kolkata');
$shdate = date('Y-m-d');
$id=$_SESSION['id'];
    $sql_qry ="UPDATE  user_profile  SET sh_name='$sh_name',sh_phone='$sh_phone',shp_addr='$sh_addr',shp_country='$sh_country',shp_state='$sh_state',shp_city='$sh_city',shp_pincode='$sh_pincode',sh_landmark='$sh_landmark',shipping_date='$shdate' WHERE id='".$id."'"; 
$res1=mysqli_query($conn,$sql_qry) or die(mysqli_error());
}

if(isset($_POST['order_comm'])){
extract($_POST);
date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s');
$user_id=$_SESSION['id'];
$session_id=$_SESSION['prd_session_id'];
$sql_plr="select addto_cart.*  from addto_cart  where (user_id='".$user_id."' OR session_id='".$session_id."') and addto_cart.invoice_id='1'";
$res_pltrr = mysqli_query($conn, $sql_plr);
while($res_ptrr= mysqli_fetch_array($res_pltrr)){ 
     $ord_id[]=$res_ptrr['id'];
}
$names_str = implode(" , ",$ord_id);
$rndno=rand(1000, 9999);
$order="ORG".$rndno;
//$id=$_SESSION['id'];
$id=$_SESSION['id'];
$payment_method="online";
$sql_ttl="select SUM(quantity * price) as ttl from addto_cart  where (user_id='".$user_id."' OR session_id='".$session_id."') and addto_cart.invoice_id='1'";
$resu_ttl = mysqli_query($conn, $sql_ttl);
$res_ttl= mysqli_fetch_assoc($resu_ttl);
$dis_price=$res_ttl['ttl'];
if($dis_price < 1800){
$dis_price=$dis_price+100;    
}else{
$dis_price=$dis_price;    
}
echo $dis_price;
$sql_pf=mysqli_query($conn,"select * from user_profile where id='".$id."'");
$res_pf= mysqli_fetch_assoc($sql_pf);
if($bill_addrs=="on"){
$name=$res_pf['fname'];
$phone=$res_pf['mob_no'];
$country=$res_pf['country'];
$state=$res_pf['state'];
$city=$res_pf['city'];    
$pincode=$res_pf['pincode'];    
$addr=$res_pf['addr'];    
$landmark=$res_pf['landmark'];    
$address="billing";
}else if($ship_addrs=="on"){
$name=$res_pf['sh_name'];
$phone=$res_pf['sh_phone'];
$country=$res_pf['shp_country'];
$state=$res_pf['shp_state'];
$city=$res_pf['shp_city'];    
$pincode=$res_pf['shp_pincode'];    
$addr=$res_pf['shp_addr'];    
$landmark=$res_pf['sh_landmark'];     
$address="shipping";
}else{
if(!empty($res_pf['fname'])){
$name=$res_pf['fname']; }else{
 $name=$res_pf['sh_name'];   
}if(!empty($res_pf['mob_no'])){
$phone=$res_pf['mob_no'];}else{
$phone=$res_pf['sh_phone'];
}if(!empty($res_pf['country'])){
$country=$res_pf['country']; }else{
$country=$res_pf['shp_country'];    
}if(!empty($res_pf['state'])){
$state=$res_pf['state']; }else{
$state=$res_pf['shp_state'];
}if(!empty($res_pf['city'])){
$city=$res_pf['city']; }else{
$city=$res_pf['shp_city'];    
}if(!empty($res_pf['pincode'])){
$pincode=$res_pf['pincode']; }else{
$pincode=$res_pf['shp_pincode'];        
}if(!empty($res_pf['addr'])){
$addr=$res_pf['addr']; }else{
$addr=$res_pf['shp_addr'];    
}if(!empty($res_pf['landmark'])){
$landmark=$res_pf['landmark'];  }else{
 $landmark=$res_pf['sh_landmark'];   
}  
$address="billing";
}
$shop_type="Web";
$sql_rd="INSERT into user_payment(order_id,user_id,cart_id,total_price,name,phone,country,state,city,pincode,address,landmark,address_type,shop_type,created_date) values ('".$order."','".$id."','".$names_str."','".$dis_price."','".$name."','".$phone."','".$country."','".$state."','".$city."','".$pincode."','".$addr."','".$landmark."','".$address."','".$shop_type."','".$date."')";
//echo $sql_rd; exit();
//$res_add=mysqli_query($conn,$sql_rd) or die(mysqli_error());
if(mysqli_query($conn,$sql_rd)){
    $last_id= mysqli_insert_id($conn);
    $session_id=$_SESSION['prd_session_id'];
    $sql_ptm="select * from user_payment where id='".$last_id."'";
    $result_ptm = mysqli_query($conn, $sql_ptm);
    $res_ptm= mysqli_fetch_array($result_ptm);
    $cart_id=$res_ptm['cart_id'];
    header("location:payment.php?id=".$last_id);

}
}

 ?>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content=" ">
    <meta name="author" content="">
    <title>Orange Groceries</title>
    <?php include("includes/css.php");?>
</head>

<body>
    <?php include("includes/header.php");?>

   <section class="page-info section-padding border-bottom topthumb">
        <div class="container-fluid">
            <div class="row"> 
            <div class="col-md-12">  
                    <a href="https://orangegroceries.com/index.php"> <span class="mdi mdi-home"></span> &nbsp; Home</a> <span class="mdi mdi-chevron-right"></span> <a href="#">Checkout</a>
                </div> 
                </div> 
        </div>
    </section>
    <section class="checkout-page section-padding">
        <div class="container-fluid">
            <div class="row"><div class="col-md-12"><a href="cart.php"><button class="btn btn-success btn-md"><i class="mdi mdi-chevron-double-left"></i> Back</button></a></div></div>
            <div class="row">
                <div class="col-md-7">
                    <center>
                        <h3 style="color:red;" id="err-msg1"></h3>
                    </center>
                    <div class="checkout-step">
                        <div class="accordion" id="accordionExample">
                            <div class="card checkout-step-two">
                                <div class="card-header" id="headingTwo">
                                    <h5 class="mb-0">
                                        <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" style="padding:0px;">
                                            <span class="number">1</span> Delivery Address
                                        </button>
                                    </h5>
                                </div>
                                  <?php  $id=$_SESSION['id'];
                           $sql_user="select * from user_profile where id='".$id."'";
                            $result_user = mysqli_query($conn, $sql_user);
                            $res_user= mysqli_fetch_array($result_user);
                            $country=$res_user['country'];
                            $state=$res_user['state'];
                            $city=$res_user['city'];
                            $sql_country = mysqli_query($conn,"select * from countries where id = '$country'");
                            $row_country = mysqli_fetch_array($sql_country);
                            $sql_state = mysqli_query($conn,"select * from states where id = '$state'");
                            $row_state = mysqli_fetch_array($sql_state);
                            $sql_city = mysqli_query($conn,"select * from cities where id = '$city'");
                            $row_city = mysqli_fetch_array($sql_city);
                            $shp_country=$res_user['shp_country'];
                            $shp_state=$res_user['shp_state'];
                            $shp_city=$res_user['shp_city'];
                            $sql_shcountry = mysqli_query($conn,"select * from countries where id = '$shp_country'");
                            $row_shcountry = mysqli_fetch_array($sql_shcountry);
                            $sql_shstate = mysqli_query($conn,"select * from states where id = '$shp_state'");
                            $row_shstate = mysqli_fetch_array($sql_shstate);
                            $sql_shcity = mysqli_query($conn,"select * from cities where id = '$shp_city'");
                            $row_shcity = mysqli_fetch_array($sql_shcity);
                             ?> 
                            
                                <div id="collapseTwo" class="collapse show" aria-labelledby="headingTwo" data-parent="#accordionExample" >
                                    <div class="card-body" style="padding-top:0px;">
                                       
                                        <form  method="post" action="" class="form-my-account add_new_address">
                                             <div class="card-header pl-0" id="headingTwo" style="border-bottom:0px solid #ccc">
                                    <h5 class="mb-0"> 
                                    <div class="row m-0 p-0">
                                    <div class="col-md-12">
                                    <div class="p-2" style="font-size:18px;font-weight:800;"> <input name="bill_addrs" type="checkbox" > Billing Address:</div>
                                    <div class="text-black" style="padding:10px; text-transform:capitalize;font-size:14px;">
                                       <strong> <?php echo $res_user['fname']; ?> </strong>, <?php echo $res_user['addr']; ?><?php echo ', '.$res_user['landmark']; ?> <?php echo ', '.$row_city['name']; ?> <?php echo ', '.$row_state['name']; ?> <?php echo ', '.$row_country['name']; ?> <?php echo ', '.$res_user['pincode']; ?>  </div>
                                     </div>

                                 <div class="col-md-12"><hr/></div>
                                 <div class="col-md-12">
                                    <div class="p-2" style="font-size:18px;font-weight:800;"> <input name="ship_addrs" type="checkbox" > Shipping Address:</div>
                                    <div class="text-black" style="padding:10px; text-transform:capitalize;font-size:14px;">
                                      <strong> <?php echo $res_user['sh_name']; ?> </strong>. <?php echo $res_user['shp_addr']; ?>,&nbsp; <?php echo $res_user['sh_landmark']; ?> <?php echo $row_shcity['name']; ?>,&nbsp;  <?php echo $row_shstate['name']; ?>,&nbsp; <?php echo $res_user['pincode']; ?>  </div>
                                     </div>

                                </div> 
                                    </h5>
                                </div>   <?php if (isset($_POST['showbtn'])) {
                                           echo $_POST['message']; }?>    
             
                                            <div style="display:flex;"> 
                                                <button class="btn btn-secondary mb-2 btn-lg" type="button" id="add_addresss" onclick="initMap()" style="    background: transparent !important;    color: #e96125;    border: solid 1px #e96125;"> + Add New Shipping Address</button> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                             <?php $user_id=$_SESSION['id'];
                                             $session_id=$_SESSION['prd_session_id'];
                                             $sql_ttls="select SUM(quantity * price) as ttl from addto_cart  where (user_id='".$user_id."' OR session_id='".$session_id."')  and addto_cart.invoice_id='1'";
                                                $resu_ttls = mysqli_query($conn, $sql_ttls);
                                                $res_ttls= mysqli_fetch_assoc($resu_ttls); 
                                                $dis_price=$res_ttls['ttl'];
                                                 if($dis_price >= 800){ ?>
                                               <button type="submit" name="order_comm" class="btn btn-secondary mb-2 btn-lg">Continue Payment </button><?php } else{ $rpr=1800-$dis_price; ?>
                                                 <input type="hidden" name="message" value="<?= "Add Items Worth ₹$rpr More To Place Order" ?>"><button type="submit" name="showbtn" class="btn btn-secondary mb-2 btn-lg">Continue Payment </button>
             
                                              <?php  } ?>
                                            </div>
                                        </form>
                                            <form method="post" action="" class="form-my-account add_new_address">  
                                            <div id="show_address" style="display:none;">
												<hr/>
                                                <h5 class="heading-design-h5 text-green">Add New Shipping Address</h5>
                                                <div id="err_msg" style="color: red"></div>

                                                <div class="row">
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
                                                            <label class="control-label">Full Name <span class="required">*</span></label>
                                                            <input name="sh_name" id="sh_name" value=" <?php echo $res_user['sh_name']; ?>" placeholder="Name" type="text" class="form-control border-form-control" />
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
                                                            <label class="control-label">Phone <span class="required">*</span></label>
                                                            <input name="sh_phone" id="phone" value="<?php echo $res_user['sh_phone']; ?>" type="number" placeholder="Mobile No" class="form-control border-form-control" />
                                                        </div>
                                                    </div>
                                                </div> 
                                                <div class="row">
                                                    <div class="col-sm-12">
                                                        <div class="form-group">
                                                            <label class="control-label" for="shipping_address">Shipping Address</label>
                                                            <textarea name="sh_addr" id="shipping_address" cols="" rows="" class="form-control" placeholder="Address"><?php echo $res_user['shp_addr']; ?></textarea>
                                                            <small class="text-danger">Please provide the number and street.</small>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-sm-12">
                                                        <div class="form-group">
                                                            <label class="control-label" for="landmark">Landmark</label>
                                                            <input name="sh_landmark" value="<?php echo $res_user['sh_landmark']; ?>"  type="text" placeholder="House no.,Flat,Landmark" class="form-control" />
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="row">
                                                    <div class="col-sm-6" style="display:none;">
                                                        <div class="form-group">
                                                             <label class="control-label">Country </label>
                                                        <select class="form-control select" name="sh_country" id="shcountry_id" onchange="countries_change()">
                                                           <option value="<?php echo $row_shcountry['id']; ?>"><?php echo $row_shcountry['name']; ?></option>
                                              <?php  $sql_country = mysqli_query($conn,"select * from countries where id='101'")or die(mysqli_error($con));
                                                 while($row_country = mysqli_fetch_array($sql_country)){ ?>
                                                  <option value="<?php echo $row_country["id"];?>"><?php echo $row_country["name"];?></option>
                                                   <?php } ?>
                                                        </select>
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
                                                            <label class="control-label" for="country">Select State</label>
                                                             <div id="shstate_id">
                                                            <select class="form-control select" name="sh_state">
                                                               <option value="<?php echo $row_shstate['id']; ?>"><?php echo $row_shstate['name']; ?></option>
                                             <?php  $sql_states = mysqli_query($conn,"select * from states where id='36'")or die(mysqli_error($con));
                                                 while($row_states = mysqli_fetch_array($sql_states)){ ?>
                                                  <option value="<?php echo $row_states["id"];?>"><?php echo $row_states["name"];?></option>
                                                   <?php } ?>
                                                            </select>
                                                        </div>
                                                        </div>
                                                    </div>
                                                    
                                                
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
                                                            <label class="control-label" for="city">City</label>
                                                            <div id="shcity_id">
                                                            <select class="form-control select" name="sh_city">
                                                            <option value="<?php echo $row_shcity['id']; ?>"><?php echo $row_shcity['name']; ?></option>
                                               <?php  $sql_citi = mysqli_query($conn,"select * from cities where id='4460'")or die(mysqli_error($con));
                                                 while($row_citi = mysqli_fetch_array($sql_citi)){ ?>
                                                  <option value="<?php echo $row_citi["id"];?>"><?php echo $row_citi["name"];?></option>
                                                   <?php } ?>
                                                        </select>
                                                        </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-6">
                                                        <div id="err_msg1" style="color: red"></div>
                                                        <label class="control-label" for="pincode">ZIP Code</label>
                                                        <input name="sh_pincode" pattern="[1-9]{1}[0-9]{5}" value="<?php echo $res_user['shp_pincode']; ?>" type="text" placeholder="ZIP Code" class="form-control" />
                                                    </div>
                                               <div class="col-sm-6 mt-4">
                                                    <input type="submit" value="Add Address" name="ship_addr" class="btn btn-secondary mb-2 btn-lg" class="btn btn-primary">
                                                </div>
                                            </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-5">
                    <div class="card mt-10">
					<div class="card card-body cart-table anajcart">
                        <div class="table-responsive">
                            <table class="table cart_summary table-striped">
                                <thead>
                                    <tr>
                                        <th class="cart_product" colspan="2">Item Details</th>
                                        <th>Qty</th> 
                                        <th>Price</th> 
                                        <th>Total</th> 
                                        <th class="action"> </th>
                                    </tr>
                                </thead>
                                <tbody>
                                      <?php 
                            //$user_id=$_SESSION['id'];
                            $session_id=$_SESSION['prd_session_id'];
                            $sql_qry="select addto_cart.*,product.prod_img1 from addto_cart join product on addto_cart.prod_id=product.id where (user_id='".$user_id."' OR session_id='".$session_id."') and invoice_id='1'";
                            $resu = mysqli_query($conn, $sql_qry);
                                while($res_data= mysqli_fetch_assoc($resu)){
                                $ttl=$res_data['price'] * $res_data['quantity']; ?>
                                    <tr>
                                        <td class="cart_product">
                                            <a><img class="img-fluid" src="admin/product/<?php echo $res_data['prod_img1'];?>" alt="<?php echo $res_data['prod_name'];?>"></a>
                                        </td>
                                        <td class="cart_description">
                                            <h5 class="product-name"><a><?php echo $res_data['prod_name'];?></a></h5>
                                        </td>
                                        <td class="availability in-stock"><?php echo $res_data['quantity'];?> </td>
                                        <td class="availability in-stock"> ₹ <?php echo $res_data['price'];?></td>
                                        <td class="price"><span class="currency-sign" id="price_576">₹ <?php echo $ttl; ?></span></td>
                                         <td class="action" width="5%">
                                            <a class="btn btn-sm" data-original-title="Remove" href="javascript:delete_cart_by_ID('<?php echo $res_data['id'] ?>');" title="" data-placement="top"><img src="img/delete-icons.png" style="width:20px; height:27px"></a>
                                        </td>
                                    </tr>
                                <?php } ?>
                                 <?php $session_id=$_SESSION['prd_session_id'];
                                 $sql_ttl="select SUM(quantity * price) as ttl from addto_cart  where (user_id='".$user_id."' OR session_id='".$session_id."')  and addto_cart.invoice_id='1'";
                                $resu_ttl = mysqli_query($conn, $sql_ttl);
                                $res_ttl= mysqli_fetch_assoc($resu_ttl); 
                                $totalp=$res_ttl['ttl'];
                                if($totalp < 1800){
                                $totalp=$totalp+100; ?> 
								<tr>
								<td></td>
								<th colspan="3" class="text-black">Delivery Charge : </th>
								<th colspan="2">₹<?php echo "100"; ?></th>
								</tr> <?php } ?>
								<tr>
                                <td ></td>
                                <th colspan="3" class="text-black">Total : </th>
                                <th colspan="2"><?php echo $totalp; ?></th>
                                </tr>
                                
                                </tbody> 
                            </table>
                        </div>
						</div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <?php include("includes/footer.php");?>
    <?php include("includes/js.php");?>

    <script>
        $(document).ready(function() {
            $("#add_addresss").click(function() {
                $("#show_address").toggle();
            });
        });
        function countries_change() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "ajax_city.php?shcountry_id=" + document.getElementById("country_id").value, false);
            xmlhttp.send(null);
            document.getElementById("shstate_id").innerHTML = xmlhttp.responseText;
        }

        function state_change() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "ajax_city.php?shstate_id=" + document.getElementById("state_idd").value, false);
            xmlhttp.send(null);
            document.getElementById("shcity_id").innerHTML = xmlhttp.responseText;
        }
         $(document).ready(function(){
            $('#country_id').on('change', function(){
                var cat_id = $(this).val();
               //alert(cat_id);
                if(cat_id){
                    $.ajax({
                        type:'POST',
                        url:'ajax_city.php',
                        data:'cat_id='+cat_id,
                        success:function(html){
                            $('#state_id').html(html);
                            console.log(html);
                           // $('#city').html('<option value="">Select Division</option>'); 
                        }
                    }); 
                }
            });

                $('#state_id').on('change', function(){
                var scat_id = $(this).val();
                if(scat_id){
                    $.ajax({
                        type:'POST',
                        url:'ajax_city.php',
                        data:'scat_id='+scat_id,
                        success:function(html){
                            $('#city_id').html(html);
                        }
                    }); 
                }
            });
        });
    </script>
    <script type="text/javascript">

function delete_cart_by_ID(id)
{
    if (confirm('Do You Want to Deleting This \nContinue anyway?')) {
        window.location.href = 'delete_chcart.php?id=' + id;
    }
}
</script>
</body>
</html>