<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes

include "top.php";

require_once('excel/php-excel-reader/excel_reader2.php');

require_once('excel/SpreadsheetReader.php');



if (isset($_POST["import"]))

{

    

   // $users=$_SESSION['bigapple']; 

  $allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];

  

  if(in_array($_FILES["file"]["type"],$allowedFileType)){



        $targetPath = 'image/'.$_FILES['file']['name'];

        move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);

        

        $Reader = new SpreadsheetReader($targetPath);

        

        $sheetCount = count($Reader->sheets());

        echo $sheetCount;

        for($i=0;$i<$sheetCount;$i++)

        {

            

            $Reader->ChangeSheet($i);

            

            foreach ($Reader as $Row)

            {

                //echo "Hello";

                $name = "";

                if(isset($Row[0])) {

                    $name = mysqli_real_escape_string($con,$Row[2]);

                }

                $custid = "";

                if(isset($Row[1])) {

                    $custid = mysqli_real_escape_string($con,$Row[0]);

                }
                 $area = "Panchanantala";

                // if(isset($Row[2])) {

                //     $area = mysqli_real_escape_string($con,$Row[2]);

                // }

                

                 $phone = "";

                if(isset($Row[3])) {

                    $phone = mysqli_real_escape_string($con,$Row[5]);

                }

                 $vcno = "";

                if(isset($Row[1])) {

                    $vcno = mysqli_real_escape_string($con,$Row[6]);

                }

                 $stb = "";

                if(isset($Row[0])) {

                    $stb = mysqli_real_escape_string($con,$Row[0]);

                }

                 $address = "";

                if(isset($Row[3])) {

                    $address = mysqli_real_escape_string($con,$Row[3]);

                }

                 $price = "0.00";

                // if(isset($Row[6])) {

                //     $price = mysqli_real_escape_string($con,$Row[6]);

                // }

                 $pay = "";

                if(isset($Row[4])) {

                    $pay = mysqli_real_escape_string($con,$Row[7]);

                }
                 $enddt = "";

                if(isset($Row[10])) {

                    $enddt = mysqli_real_escape_string($con,$Row[10]);

                }

                // $month = "";

                // if(isset($Row[7])) {

                //     $month = mysqli_real_escape_string($con,$Row[7]);

                // }

                // $year = "";

                // if(isset($Row[8])) {

                //     $year = mysqli_real_escape_string($con,$Row[8]);

                // }

              // echo $name;
                $month="Dec";
                $year='2020';
               // $enddt='2020-10-01';
                

                if (!empty($name)) {







                  $checkc=f(q("SELECT * FROM adduser WHERE custid='$custid'"));

                  $getarea=f(q("select * from tbl_area where arname='$area'"));
                  $getpack=f(q("SELECT * FROM tbl_pack WHERE pname='$pay'"));



                  if($checkc['name']==''){

                    if($getarea[arid]==''){

                      $areaid='0';
                    }


                    $query = "INSERT INTO

                                     `adduser`

                                 SET

                             `name`='$name',

                             `custid`='$custid',

                             `address`='$address',

                             `phone`='$phone',
                             area_id='$areaid',
                             area_name='$area',
                             packid='0',
                             packname='$pay',
                             price='$price',vc='$vcno',stb='$stb',pay_ch='$pay',is_cable=0,mnth='$month',yrs='$year',pexpiry='$enddt'";

                  //echo $query;

                    $result = mysqli_query($con,$query);

                    if($result){

                       mysqli_query($con,"INSERT INTO tbl_login SET l_pass=md5('$phone'),l_uid=md5('$custid'),l_name='$name',l_read='$custid',l_type='3',l_act='1',user_id='0'");
                    }

                  }else{



                    $query="UPDATE adduser SET mnth='$month',yrs='$year',pay_ch='$pay',phone='$phone',price='$price' WHERE custid='$custid'";



                    $result=mysqli_query($con,$query);







                  }

                

                    if (! empty($result)) {

                        $type = "success";

                        $message = "Excel Data Imported into the Database";

                    } else {

                        $type = "error";

                        $message = "Problem in Importing Excel Data".$result;

                    }

                }

             }

        

         }

  }

  else

  { 

        $type = "error";

        $message = "Invalid File Type. Upload Excel File.";

  }

}

?>



<!DOCTYPE html>

<html>    

<head>

<style>    

body {

	font-family: Arial;

	width: 550px;

}



.outer-container {

	background: #F0F0F0;

	border: #e0dfdf 1px solid;

	padding: 40px 20px;

	border-radius: 2px;

}



.btn-submit {

	background: #333;

	border: #1d1d1d 1px solid;

    border-radius: 2px;

	color: #f0f0f0;

	cursor: pointer;

    padding: 5px 20px;

    font-size:0.9em;

}



.tutorial-table {

    margin-top: 40px;

    font-size: 0.8em;

	border-collapse: collapse;

	width: 100%;

}



.tutorial-table th {

    background: #f0f0f0;

    border-bottom: 1px solid #dddddd;

	padding: 8px;

	text-align: left;

}



.tutorial-table td {

    background: #FFF;

	border-bottom: 1px solid #dddddd;

	padding: 8px;

	text-align: left;

}



#response {

    padding: 10px;

    margin-top: 10px;

    border-radius: 2px;

    display:none;

}



.success {

    background: #c7efd9;

    border: #bbe2cd 1px solid;

}



.error {

    background: #fbcfcf;

    border: #f3c6c7 1px solid;

}



div#response.display-block {

    display: block;

}

</style>

</head>



<body>

    <h2>Import Users Via Excel File</h2>

    

    <div class="outer-container">

        <form action="" method="post"

            name="frmExcelImport" id="frmExcelImport" enctype="multipart/form-data">

            <div>

                <label>Choose Excel

                    File</label> <input type="file" name="file"

                    id="file" accept=".xls,.xlsx">

                <button type="submit" id="submit" name="import"

                    class="btn-submit">Import</button>

        

            </div>

        

        </form>

        

    </div>

    <div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; echo "<a href='index.php'> << back</a>"; } ?></div>

    

         





</body>

</html>