<?php
   
    $mess="Error";
    $CompID=0;
    if (isset($_POST["comp_id"]))
    {
         $CompID=$_POST["comp_id"]; 
    }
    if($CompID==0) {
          $mess= "Error: Web Co ID invalid";
          die($mess);
    }
    include_once 'opendb.php';
    $EDIT_TIME=date("ymdHi");
    $cust_name=$_POST["acc_name"];
    $cust_name = str_replace("&", "and", $cust_name);
    $cust_add=$_POST["address"];
    $cust_add = str_replace("&", "and", $cust_add);
    $country_code = isset($Record->country_code) ? $Record->country_code : '';
    $street_name = isset($Record->street_name) ? $Record->street_name : '';
    $bldg_no = isset($Record->bldg_no) ? $Record->bldg_no : '';
    $city = isset($Record->city) ? $Record->city : '';
    $district = isset($Record->district) ? $Record->district : '';
    $id_type = isset($Record->id_type) ? $Record->id_type : '';
    $name_ar = isset($Record->name_ar) ? $Record->name_ar : '';
    $add1_ar = isset($Record->add1_ar) ? $Record->add1_ar : '';
    $add2_ar = isset($Record->add2_ar) ? $Record->add2_ar : '';
    $add3_ar = isset($Record->add3_ar) ? $Record->add3_ar : '';
    $reg_no2 = isset($Record->reg_no2) ? $Record->reg_no2 : '';
    
    $sql = "UPDATE subsidiary SET SUB_NAME='".$cust_name."'," 
                                ."ADDRESS='".$cust_add."'," 
                                ."EMAIL='".$_POST["email"]."'," 
                                ."PLACE='".$_POST["place"]."'," 
                                ."PHONE='".$_POST["phone"]."'," 
                                ."MOBILE='".$_POST["mobile"]."'," 
                                ."FAX='',"
                                ."CONT_NAME=''," 
                                ."CONT_PHONE='',"
                                ."AREA_CODE=''," 
                                ."Area_Name='".$_POST["area"]."'," 
                                ."SMAN_CODE='".$_POST["client_Code"]."'," 
                                ."SUB_TYPE='C'," 
                                ."Inactive=0,"                                   
                                ."Reg_No1='".$_POST["reg_no"]."'," 
                                ."price_level=0," 
                                ."App_Pass='12345'," 
                                ."country_code='".$country_code."',"
                                ."street_name='".$street_name."',"
                                ."bldg_no='".$bldg_no."',"
                                ."city='".$city."',"
                                ."district='".$district."',"
                                ."id_type='".$id_type."',"
                                ."name_ar='".$name_ar."',"
                                ."add1_ar='".$add1_ar."',"
                                ."add2_ar='".$add2_ar."',"
                                ."add3_ar='".$add3_ar."',"
                                ."Reg_No2='".$reg_no2."',"
                                ."EDIT_TIME=".$EDIT_TIME;
    if($_POST["Action"]=='New'){
        $sql.=",create_source=2,comp_id=$CompID,sub_code='". $_POST["sub_code"] . "'  WHERE pkey=".$_POST["pKey"];
    } else {
         if ( !startsWith( $_POST["sub_code"],"W")){
            $conn->close();
            die("Error :WEB Edit not permitted for this customer");
        }
        $sql.="  WHERE sub_code like '". $_POST["sub_code"] . "' and comp_id=".$CompID;
    } 
   // die($sql);
    if ($conn->query($sql) === TRUE) {
        $mess="OK";
    } else {
        $mess= "Error updating customer record: " . $conn->error.$sql;
        $errorLog = "Error updating customer record: " . $conn->error . "\nSQL Query: " . $sql . "\n";
        $logFile = 'error_logs.txt'; // Specify the log file path here.
        file_put_contents($logFile, $errorLog, FILE_APPEND);
    }
    $conn->close();
    echo  $mess;
    
      function startsWith ($string, $startString)
    {
        $len = strlen($startString);
        return (substr($string, 0, $len) === $startString);
    }
    
?>