<?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";

               

                $custid = "";

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

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

                }

                

                //  $phone = "0";

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

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

                // }

                $due = "0";

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

                    $due = mysqli_real_escape_string($con,$Row[1]);

                }
                 

                
                //  $lineman = "0";

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

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

                // }


                

                if (!empty($custid)) {







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

                //   $getline=f(q("select * from tbl_login where l_name='$lineman' AND l_type=2"));
                //   //$getpack=f(q("SELECT * FROM tbl_pack WHERE pname=''"))
                //   $lineid=$getline['l_id'];

                //   if($getline['l_name']==''){
                    
                //     $luser=str_replace(' ', '-', strtolower($lineman));
                //     $luid=md5($luser);
                //     $lpass=md5("1234567");

                //     $inscom=mysqli_query($con,"INSERT INTO tbl_login SET l_uid='$luid',l_pass='$lpass',l_name='$lineman',l_read='$luser',l_type=2,l_act=1,user_id=0");
                //     $lineid=mysqli_insert_id($con);
                    
                //   }else{

                //     $lineid=$getline['l_id'];
                //   }


                  



                    $query="UPDATE adduser SET prevdue='$due' 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='subscriber.php'> << back</a>"; } ?></div>

    

            <?php //echo $dupli?>





</body>

</html>