<?php


// array for JSON response
$response = array();

//check for required fields

    
    $NetCode              = $_POST['NetCode'];
    $SysCode              = $_POST['SysCode'];
    $CompanyName          = $_POST['CompanyName'];
    $CompanyAddress       = $_POST['CompanyAddress'];
    $CompanyNumber        = $_POST['CompanyNumber'];
    $NameOfPerson         = $_POST['NameOfPerson'];
    $LoginMobileNo        = $_POST['LoginMobileNo'];
    $Email                = $_POST['Email'];
    $Country              = $_POST['Country'];
    $Password             = $_POST['Password'];
    $City                 = $_POST['City'];
    $SubCity              = $_POST['SubCity'];
    $Website              = $_POST['Website'];
    $CapacityOfPersons    = $_POST['CapacityOfPersons'];
    $CompanyLogo          = $_POST['CompanyLogo'];
    $DisplayImage         = $_POST['DisplayImage'];
    $Lat                  = $_POST['Lat'];
    $Lng                  = $_POST['Lng'];
    $ProjectID            = $_POST['ProjectID'];
    $BusinessDescriptions = $_POST['BusinessDescriptions'];
    $FinancialYear        = $_POST['FinancialYear'];
    $CountryUserID        = $_POST['CountryUserID'];
    $CountryClientID      = $_POST['CountryClientID'];
    
    // include db connect class
    //require_once __DIR__ . '/DBConnect.php';
    require_once __DIR__ . '/../DBConnect.php';
    
    $UpdatedDate = date("Y-m-d H:i:s");
    //////////////////////////////////////////////////////temTry
    $parm        = array();
    $query       = "select max(ClientID) as maxid from Client";
    try {
        // $result=$conn->query($sql);
        $stmt = $conn->prepare($query);
        
        // execute the query
        $stmt->execute();
        $users = $stmt->fetchAll();
        $cliid = 0;
        foreach ($users as $user) {
            
            $cliid = $user['maxid'] + 1;
        }
        // echo a message to say the UPDATE succeeded
        // $result = $stmt->rowCount(); // . " records UPDATED successfully";
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
    
    $selectQuery = "select CBEntryCharges,CBEditCharges,BillEditCharges,BillEntryCharges from Project Where ProjectID = '$ProjectID'";
    
    try {
        // $result=$conn->query($sql);
        $stmt = $conn->prepare($selectQuery);
        
        // execute the query
        $stmt->execute();
        $Projects = $stmt->fetchAll();
        foreach ($Projects as $Project) {
            $CBEntryCharges = $Project['CBEntryCharges'];
            $CBEditCharges = $Project['CBEditCharges'];
            $BillEditCharges = $Project['BillEditCharges'];
            $BillEntryCharges = $Project['BillEntryCharges'];
        }
        // echo a message to say the UPDATE succeeded
        // $result = $stmt->rowCount(); // . " records UPDATED successfully";
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
    
    //Client creating in client table
    ///////////////////////////////////////////////////////////
    $sql = "INSERT INTO `Client` (`ClientID`,`LoginMobileNo`,`CountryUserID`, `CountryClientID`,`CompanyName`, `CompanyAddress`,`CBEntryCharges`,`CBEditCharges`,`BillEditCharges`,`BillEntryCharges`, `CompanyNumber`, `NameOfPerson`, `Email`, `WebSite`, `Password`, `Country`, `City`, `SubCity`, `SysCode`, `NetCode`, `UpdatedDate`, `Lat`, `Lng`, `ProjectID`, `BusinessDescriptions`, `FinancialYear`, `ActiveClient`) 
            VALUES ( '$cliid','$LoginMobileNo','$CountryUserID', '$CountryClientID','$CompanyName', '$CompanyAddress', '$CBEntryCharges', '$CBEditCharges', '$BillEditCharges', '$BillEntryCharges', '$CompanyNumber', '$NameOfPerson', '$Email', '$Website', '$Password', '$Country', '$City', '$SubCity', '$SysCode', '$NetCode', '$UpdatedDate', '$Lat', '$Lng', '$ProjectID', '$BusinessDescriptions', '$FinancialYear', '1');";
    
    //    $sql2="INSERT INTO `Client` (`ClientID`, `ClientParentID`, `EntryType`, `LoginMobileNo`, `CompanyName`, //`CompanyAddress`, `CompanyNumber`, `NameOfPerson`, `Email`, `WebSite`, `CompanyLo;`, `Password`, `ActiveClient`, //`Country`, `City`, `SubCity`, `CapacityOfPersons`, `ClientUserID`, `SysCode`, `NetCode`, `UpdatedDate`, `Theme`, //`Background`, `Lat`, `Lng`, `BookingTermsAndCondition`, `ProjectID`, `BusinessDescriptions`, `FinancialYear`) VALUES //('909', '90', '90', '9', '0', '90', '9', '0', '90', '9', '0', NULL, '989', '7', '8989', '89', '8', '98', '98', '9', //'89', '2020-01-16 00:00:00', '9', '8', '89', '8', '9', '89', '8', '9');";
    try {
        // $result=$conn->query($sql);
        $result = $conn->exec($sql);
        
        if ($result === false) {
            /////////////not inserted
            
            
            
            $response["message"] = "Client Not Register data not inserted";
            echo json_encode($response);
        }
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
    $ClientID = $cliid;
    
    //////////////////////Getting Maximun AcNameID from DataBase
    
    ////////Admin User Creating here/////////////////
    $sql     = "INSERT INTO `Account3Name` (`AcNameID`, `ClientID`, `ClientUserID`, `NetCode`, `SysCode`, `UpdatedDate`, `AcName`, `AcAddress`, `AcMobileNo`, `AcContactNo`, `AcEmailAddress`, `AcPassward`, `UserRights`, `AcGroupID`, `SerialNo`,`NameOfPerson`,`Remarks`) VALUES (
        '1',
        '$ClientID',
        '0',
        '$NetCode',
        '$SysCode',
        '$UpdatedDate',
        '$NameOfPerson',
        '$CompanyAddress',
        '$LoginMobileNo',
        '$CompanyNumber',
        '$Email',
        '$Password',
        'Admin',
        '2',
        '1',
        '',
        ''
    )";
    try {
        // $result=$conn->query($sql);
       $result=  $conn->exec($sql);
					    
    $last_id = $conn->lastInsertId();
			if( $result === false ) {
			/////////////not inserted
			
				 $response["success"]     = 0;
        $stmt=false;
            $response["message"]     = "User Not Register:".$last_id;
			//echo json_encode($response);
			}else{
				/////////////inserted
				 $response["success"]     = 1;
         $stmt=true;
            $response["UpdatedDate"] = $UpdatedDate;
            $response["message"]     = "User Register".$last_id;
			//echo json_encode($response);
			}
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
    $ClientUserID = 1;
    
    if ($stmt === false) {
        
        die(print_r(sqlsrv_errors(), true));
        echo sqlsrv_errors();
    } else {
        
        $sql1 = "INSERT INTO `Account2Group` (`AcGroupID`, `AcTypeID`, `AcGruopName`, `ClientID`, `ClientUserID`,`NetCode`, `SysCode`, `UpdatedDate`) 
        SELECT AcGroupID, AcTypeID, AcGroupName, $ClientID, $ClientUserID, '0', '0', '$UpdatedDate'
        FROM ProjectDefaultAccountGroup
        WHERE (ProjectID = $ProjectID)";
        
        $sql     = "INSERT INTO `Account3Name`
                         (`AcGroupID`, `AcName`, `AcNameID`, `SerialNo`, `UserRights`, `ClientID`, `ClientUserID`, `UpdatedDate`,`NameOfPerson`,`Remarks`)
SELECT        AcGroupID, AcName, AcNameID, SerialNo, UserRights, $ClientID AS ClientID, $ClientUserID AS ClientUserID, '$UpdatedDate' AS UpdatedDate,'','' 
FROM            ProjectDefaultAccount
WHERE        (ProjectID = $ProjectID)";
        $params  = array();
        try {
        // $result=$conn->query($sql);
       $result=  $conn->exec($sql1);       
       $result=  $conn->exec($sql);

					    
    $last_id = $conn->lastInsertId();
			if( $result === false ) {
			/////////////not inserted
			
				 $response["success"]     = 0;
        $stmt=false;
            $response["message"]     = "User Not Register:".$last_id;
			echo json_encode($response);
			}else{
				/////////////inserted
				 $response["success"]     = 1;
         $stmt=true;
            $response["UpdatedDate"] = $UpdatedDate;
            $response["message"]     = "User Register".$last_id;
			//echo json_encode($response);
			}
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
        // if ($stmt === false) {
        //     die(print_r(sqlsrv_errors(), true));
        //     echo sqlsrv_errors();
        // } else {
        //     ///////////////////////     Create Folders     /////////////
        //     $folder = "ClientImages";
        //     if (!is_dir($folder)) {
        //         mkdir($folder);
        //     }
            // mkdir($folder . "/" . $ClientID);
            // mkdir($folder . "/" . $ClientID . "/SliderImages");
            
            
            // $decodedImage  = base64_decode($CompanyLogo);
            // $return        = file_put_contents($folder . "/" . $ClientID . "/logo.png", $decodedImage);
            // $decoded_Image = base64_decode($DisplayImage);
            // $return        = file_put_contents($folder . "/" . $ClientID . "/DisplayImage.png", $decoded_Image);
            
            // no number found
            // $response["success"]  = 1;
            // $response["ClientID"] = $ClientID;
            // $response["message"]  = "Register Successfully";
            
            // echo no users JSON
            //echo json_encode($response);
       // }
     }
 
?>