<?php
    include "../db/headerApi.php";
    include "../db/connection_main.php";

    $json = file_get_contents('php://input');
    $data = json_decode($json);

    $sql = "UPDATE apps.yty_g_prc_modifier_list_tl
               SET approval_flag = 'P',
                   submitted_date = sysdate
             WHERE period = :period
               AND currency_code = :currency
               AND NVL(approval_flag,'N') IN ('N','R')
               AND created_by = :userid";
    
    $sqlarr = oci_parse($dbcon , $sql);
    oci_bind_by_name($sqlarr, ':period', $data->period->ID);
    oci_bind_by_name($sqlarr, ':currency', $data->currency->ID);
    oci_bind_by_name($sqlarr, ':userid', $data->userid);
    $r = oci_execute($sqlarr, OCI_NO_AUTO_COMMIT);
    
    if (!$r) {
        $e = oci_error($sqlarr);
        $errorMsg = json_encode($e['message']);
        goto finalStep;
    }

    $sql = "SELECT emp_id, employee_name, email_id 
              FROM production.employee_master
             WHERE emp_id = :userid";

    $sqlarr = oci_parse($dbcon , $sql);
    oci_bind_by_name($sqlarr, ':userid', $data->userid);
    $r = oci_execute($sqlarr);

    if (!$r) {
         $e = oci_error($sqlarr);
         $resMsg = json_encode($e['message']);
    }
    oci_fetch_all($sqlarr, $rows_emp, null, null, OCI_FETCHSTATEMENT_BY_ROW);

    //Approver Email
   //$senderEmail = 'reyndo.frenliedo@ytygroup.com.my,satish.madala@ytygroup.com.my';
   $senderEmail = 'suresh.bhalla@ytygroup.com.my,laura.chong@ytygroup.com.my';
   $html='<!doctype html>
          <html>
          <head>
                <title></title>
                <style type="text/css">
                   table {border-collapse:separate;}
                   .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td {line-height: 100%}
                   .ExternalClass {width: 100%;}
          
                   .wrapper {
                      width:100%;
                      background-color: #fff;
                   }
          
                   .container {
                      width:800px !important;
                   }
          
                   .content {
                      background-color: #fff;
                      border: 1px solid #D6D6D6;
                      -webkit-box-shadow: 0px 0px 7px 1px rgba(0,0,0,0.75);
                      -moz-box-shadow: 0px 0px 7px 1px rgba(0,0,0,0.75);
                      box-shadow: 0px 0px 7px 1px rgba(0,0,0,0.55);
                   }
          
                   .mbody {
                      color: #000;
                      font-family: Verdana;
                      font-style: normal;
                      font-size: 10.5pt;
                   }
          
                   .note {
                      font-size: 7.5pt;
                      font-family: Verdana;
                      color: #000;
                      text-align: justify;
                   }
                </style>
          </head>
          <body>
          <table cellpadding="0" cellspacing="0" align="center" border="0" width="100%" style="height:100%!important; margin:0; padding:0; width:100%!important; text-align:center">
          <tr>
                <td style="height:20px; display:block;"></td>
          </tr>
          <tr>
                <td style="width:20px; display:block;"></td>
                <td align="center">
                   <table class="content" border="0" cellspacing="0" cellpadding="0" width="800" style="border-collapse: collapse; border:1px solid #7f7f7f;">
                   <tr>
                      <td>
                            <img src="http://dl.ytygroup.com.my/CommonLogo/MailHeader.jpg" width="952" height="106" style="display:block;"/>
                      </td>
                   </tr>
                   <tr>
                      <td style="height:10px; display:block;"></td>
                   </tr>
                   <tr>
                      <td>
                            <table class="mbody" style="width:100%;">
                            <tr>
                               <td style="width:20px; display:block;"></td>
                               <td style="line-height: 1.3em;">
                                  <p>Dear Approver,</p>
                                  <p>Your kind approval is sought on the quotation price list for '.$data->period->ID.'. Please <a href="https://ytyportal.ytygroup.my/advance_pricing/dashboard.php" target="_blank" style="color:#1355a7; font-weight:600; text-decoration:none;">login</a> to Pricing system to respond.</p>
                                  
                                  <br/>
                                  <p>Sincerely,<br /><b>YTY Workflow</b></p> 
                                  <p style="font-size:11px;">Note: This message is auto-generated so please do not respond to this email.</p></td>
                               <td style="width:20px; display:block;"></td>
                            </tr>
                            </table>
                      </td>
                   </tr>
                   <tr>
                      <td style="height:10px; display:block;"></td>
                   </tr>
                   <tr>
                      <td>
                            <img src="http://dl.ytygroup.com.my/CommonLogo/MailFooter.jpg" style="display:block;"/>
                      </td>
                   </tr>
                   </table>
                </td>
                <td style="width:20px; display:block;"></td>
          </tr>
          <tr>
                <td style="height:20px; display:block;"></td>
          </tr>
          </table>
          </body>
          </html>';
          
   $mailSubject="Request for quotation price list for ".$data->period->ID;

   $strInsertEmailQuery = "BEGIN
                              ytycustom.set_notification.generate(ytycustom.set_notification.get_id,    --no change <br>
                              'EMAIL',                              --no change<br>
                              '".$senderEmail."',      --to email id<br>
                              NULL,    --cc email id (Null for none)<br>
                              NULL,                                 --attachment file path, let it be null for the time being<br>
                              '".$mailSubject."',                               --Email Subject<br>
                              '".$html."',               --Email body (in html format)<br>
                              'HTML',                               --no change<br>
                              SYSDATE,                              --no change<br>
                              'ORACLE PRICING',                      --Claim system (will change later on)<br>
                              Null     --bcc email id (Null for none)<br>
                              );
                           END;";
   $insertEmailQuery = oci_parse($dbcon, $strInsertEmailQuery);
   oci_execute($insertEmailQuery);

   //Sender Email
   $html1='<!doctype html>
           <html>
           <head>
                 <title></title>
                 <style type="text/css">
                    table {border-collapse:separate;}
                    .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td {line-height: 100%}
                    .ExternalClass {width: 100%;}
           
                    .wrapper {
                       width:100%;
                       background-color: #fff;
                    }
           
                    .container {
                       width:800px !important;
                    }
           
                    .content {
                       background-color: #fff;
                       border: 1px solid #D6D6D6;
                       -webkit-box-shadow: 0px 0px 7px 1px rgba(0,0,0,0.75);
                       -moz-box-shadow: 0px 0px 7px 1px rgba(0,0,0,0.75);
                       box-shadow: 0px 0px 7px 1px rgba(0,0,0,0.55);
                    }
           
                    .mbody {
                       color: #000;
                       font-family: Verdana;
                       font-style: normal;
                       font-size: 10.5pt;
                    }
           
                    .note {
                       font-size: 7.5pt;
                       font-family: Verdana;
                       color: #000;
                       text-align: justify;
                    }
                 </style>
           </head>
           <body>
           <table cellpadding="0" cellspacing="0" align="center" border="0" width="100%" style="height:100%!important; margin:0; padding:0; width:100%!important; text-align:center">
           <tr>
                 <td style="height:20px; display:block;"></td>
           </tr>
           <tr>
                 <td style="width:20px; display:block;"></td>
                 <td align="center">
                    <table class="content" border="0" cellspacing="0" cellpadding="0" width="800" style="border-collapse: collapse; border:1px solid #7f7f7f;">
                    <tr>
                       <td>
                             <img src="http://dl.ytygroup.com.my/CommonLogo/MailHeader.jpg" width="952" height="106" style="display:block;"/>
                       </td>
                    </tr>
                    <tr>
                       <td style="height:10px; display:block;"></td>
                    </tr>
                    <tr>
                       <td>
                             <table class="mbody" style="width:100%;">
                             <tr>
                                <td style="width:20px; display:block;"></td>
                                <td style="line-height: 1.3em;">
                                   <p>Dear '.$rows_emp[0]["EMPLOYEE_NAME"].',</b></p>
                                   <p>Your quotation price list for '.$data->period->ID.' is sent for Approval.</p>
                                   
                                   <br/>
                                   <p>Sincerely,<br /><b>YTY Workflow</b></p> 
                                   <p style="font-size:11px;">Note: This message is auto-generated so please do not respond to this email.</p></td>
                                <td style="width:20px; display:block;"></td>
                             </tr>
                             </table>
                       </td>
                    </tr>
                    <tr>
                       <td style="height:10px; display:block;"></td>
                    </tr>
                    <tr>
                       <td>
                             <img src="http://dl.ytygroup.com.my/CommonLogo/MailFooter.jpg" style="display:block;"/>
                       </td>
                    </tr>
                    </table>
                 </td>
                 <td style="width:20px; display:block;"></td>
           </tr>
           <tr>
                 <td style="height:20px; display:block;"></td>
           </tr>
           </table>
           </body>
           </html>';

   $mailSubject="Request for quotation price list for ".$data->period->ID;

   $strInsertEmailQuery1= "BEGIN
                              ytycustom.set_notification.generate(ytycustom.set_notification.get_id,    --no change <br>
                              'EMAIL',                              --no change<br>
                              '".$rows_emp[0]["EMAIL_ID"]."',      --to email id<br>
                              NULL,    --cc email id (Null for none)<br>
                              NULL,                                 --attachment file path, let it be null for the time being<br>
                              '".$mailSubject."',                               --Email Subject<br>
                              '".$html1."',               --Email body (in html format)<br>
                              'HTML',                               --no change<br>
                              SYSDATE,                              --no change<br>
                              'ORACLE PRICING',                      --Claim system (will change later on)<br>
                              Null     --bcc email id (Null for none)<br>
                              );
                           END;";
   $insertEmailQuery1 = oci_parse($dbcon, $strInsertEmailQuery1);
   oci_execute($insertEmailQuery1);

finalStep:
    $result = [];
    if ($errorMsg == "") {
        oci_commit($dbcon);
        $result[] = [
            "message" => "Success",
            "id" => $newid
        ];
    } else {
        oci_rollback($dbcon);
        $result[] = [
            "message" => $errorMsg,
            "id" => ""
        ];
    }
    print json_encode($result);
?>
