<?php
include "Connection.php";
require_once dirname(__FILE__) . "/_api/global.php";
$conn = new Connection();

if (isset($_REQUEST["funName"])) {
    $db = $conn->getConnection();
    $fun = $_REQUEST["funName"];

    switch ($fun) {
        case "readexcel":

            $fun();
            break;

    }
}

//USERS DSE
function readexcel()
{
    global $conn;
    $lang = (isset($_REQUEST["lang"])) ? $_REQUEST["lang"] : 'en_us';
    try {

        require 'excelreader/excel_reader2.php';
        require 'excelreader/SpreadsheetReader.php';
        $Spreadsheet = new SpreadsheetReader('../hypermupload/import/sample.xlsx');
        $Spreadsheet->ChangeSheet(0);

        $dealer_code_arr = array();
        foreach ($Spreadsheet as $Key => $Row) {

            if ($Key < 1) {
                //print_r($Row);
            } else {
                $dealer_code_arr[] = "'" . $Row[0] . "'";
                echo "UPDATE tbl__dms_dealer  SET  `dealer_name`='" . escape_str(trim($Row[1])) . "', `email`='" . escape_str(trim($Row[3])) . "', `city`='" . trim($Row[6]) . "', `state`='" . trim($Row[4]) . "', `zone`='" . trim($Row[7]) . "', `phone`='" . trim($Row[2]) . "', `zm_name`='" . trim($Row[8]) . "', `rm_name`='" . trim($Row[9]) . "', `am_name`='" . trim($Row[10]) . "', `zm_email`='" . trim($Row[11]) . "', `rm_email`='" . trim($Row[12]) . "', `am_email`='" . trim($Row[13]) . "', `zm_mobile`='" . trim($Row[14]) . "', `rm_mobile`='" . trim($Row[15]) . "', `am_mobile`='" . trim($Row[16]) . "', `modified_on`='" . NOW . "' WHERE dealer_code='" . $Row[0] . "';<br>";

                echo "UPDATE tbl__dms_houser  SET  `fname`='" . escape_str(trim($Row[1])) . "',lname='', `emailid`='" . trim($Row[3]) . "', `mobile`='" . trim($Row[2]) . "'  WHERE dealer_code='" . trim($Row[0]) . "' AND role='Dealer';<br>";

                echo "UPDATE tbl__user  SET  `fname`='" . escape_str(trim($Row[1])) . "',lname='', `emailid`='" . trim($Row[3]) . "', `mobileno`='" . trim($Row[2]) . "'  WHERE dealer_code='" . $Row[0] . "' AND role='14';<br>";

                //echo "UPDATE tbl__dms_houser  SET  `fname`='" . escape_str(trim($Row[1])) . "',lname='', `emailid`='" . trim($Row[3]) . "', `mobile`='" . trim($Row[2]) . "'  WHERE dealer_code='" . $Row[0] . "' AND role='Customer Experience';<br>";
                //echo "UPDATE tbl__user  SET  `fname`='" . escape_str(trim($Row[1])) . "',lname='', `emailid`='" . trim($Row[3]) . "', `mobileno`='" . trim($Row[2]) . "'  WHERE dealer_code='" . $Row[0] . "' AND role='16';<br>";

            }
        }
        // echo count($dealer_code_arr);
        //echo "<br>";
        // echo "dealer_code IN(".@implode(',',$dealer_code_arr).")";

        exit;
        //$conn->message(true,$conn->getMessage($lang,'excelupload','success1'),$arr );

    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'excelupload', 'error4'));
    }
}
