<?php
// required headers
header("Access-Control-Allow-Origin: *");
// header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/ont.php';
include_once 'SimpleXLSX.php';
    
date_default_timezone_set('Asia/Rangoon'); 
session_start();

$database = new Database();
$db = $database->getConnection();
 
$ont = new ONT($db);

if (isset($_FILES['file'])) {
    
    if ( $xlsx = SimpleXLSX::parse( $_FILES['file']['tmp_name'] ) ) {

        $dim = $xlsx->dimension();
        $cols = $dim[0];

        foreach ( $xlsx->rows() as $k => $r ) {
            // if ($k == 0) continue; // skip first row
            if($k==0){
                // $city = (trim($r[0])=="Invoice ID")?$r[0]:"";
                // $id_no = (trim($r[1])=="Invoice Date")?$r[1]:"";
                // $ont_snno = (trim($r[2])=="Payment Date")?$r[2]:"";
                // $mme_id = (trim($r[3])=="Payment Amount")?$r[3]:"";
                // $pppoe_name = (trim($r[4])=="Payment Type")?$r[4]:"";
                // $ont_password = (trim($r[5])=="Received By")?$r[5]:"";
                $city = $r[0];
                $id_no = $r[1];
                $ont_snno = $r[2];
                $mme_id = $r[3];
                $pppoe_name = $r[4];
                $ont_password = $r[5];

                // if($invoice_id=="" || $invoice_date=="" || $payment_date=="" || $payment_amount==""){
                //     echo "incorrect";
                //     die();
                // }
            }else{
                $ont->city = $r[0];
                $ont->id_no = $r[1];
                $ont->ont_snno = $r[2];
                $ont->mme_id = $r[3];
                $ont->pppoe_name = $r[4];
                $ont->ont_password = $r[5];
                $ont->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
                $ont->entry_date_time = date("Y-m-d H:i:s");

                if($ont->exist()){
                   continue;
                }else{
                    $ont->create();
                }

                // if($car_list->vin_no!=""){
                //     if($car_list->exist()){
                //         continue;
                //     }else{
                        // $ont->createUp();
                //     }
                // }
            }
        }
    } else {
        echo "error";
    }
}
echo "created";
?>