<?php
session_start();
if (isset($_POST["paramValues"])) {
    
    $paramValues = json_decode($_POST["paramValues"]);
    //echo json_encode($paramValues->FTA);


    $claimStatus = $paramValues->inlineRadioOptions;
    $empId = $paramValues->empId;
    $presentDate = $paramValues->presentDate;
    $DepuationAllowance = $paramValues->DepuationAllowance;
    $DaAmount = $paramValues->DaAmount;
    $hotelBill = $paramValues->hotelBill;
    $otherBill = $paramValues->otherBill;
    $TotalAmount = $paramValues->TotalAmount;
    $deductedAmount = $paramValues->loggedInUserDeductAmount;
    $totalDeductedAmount = $paramValues->allTotalDeductedAmount;
    $EditedDate = $paramValues->report_date;
    $deductedReason = $paramValues->Reason;
    $rejectReason = $paramValues->Reason;
    
    
  
    $loggedInUserID = $_SESSION['userId'];
    $loggedInUserName = $_SESSION['userName'];
    $loggedInUserDepartment = $_SESSION['teamLeads'];
    $claimStatusUpdatedBy = $loggedInUserID . "," . $loggedInUserName;
    $uniqueID = $empId . '_' . $presentDate;

    $obj = new updateClaimStatus();
    
    switch($claimStatus){
        case CLAIM_TL_REJECTED:
        case CLAIM_FINANCE_REJECTED:
            $obj->rejectClaim($uniqueID, $claimStatus, $rejectReason, $EditedDate, $claimStatusUpdatedBy, $loggedInUserDepartment);
            break;
        case CLAIM_PAID:
            $obj->paidClaim($uniqueID, $claimStatus, $EditedDate, $claimStatusUpdatedBy);
            break;
        case CLAIM_UNDO:
            $obj->undoClaim($claimStatus, $loggedInUserDepartment, $uniqueID);
            break;
        case CLAIM_TL_APPROVED:
        case CLAIM_FINANCE_APPROVED:
            $obj->updateClaim($uniqueID, $claimStatus, $TotalAmount, $DepuationAllowance, $DaAmount,$otherBill, $hotelBill, $loggedInUserDepartment, $loggedInUserID, $loggedInUserName, $EditedDate, $claimStatusUpdatedBy, $deductedAmount, $totalDeductedAmount, $deductedReason);
            break;
            
        
    }
    // if($claimStatus == CLAIM_TL_REJECTED || $claimStatus == CLAIM_FINANCE_REJECTED){
    //     $obj->rejectClaim($uniqueID, $claimStatus, $rejectReason, $EditedDate, $claimStatusUpdatedBy, $loggedInUserDepartment);
    // }elseif($claimStatus==CLAIM_PAID){
    //     $obj->paidClaim($uniqueID, $claimStatus, $EditedDate, $claimStatusUpdatedBy);
    // }elseif($claimStatus == CLAIM_TL_APPROVED || $claimStatus ==CLAIM_FINANCE_APPROVED){
    // $obj->updateClaim($uniqueID, $claimStatus, $TotalAmount, $DepuationAllowance, $DaAmount,$otherBill, $hotelBill, $loggedInUserDepartment, $loggedInUserID, $loggedInUserName, $EditedDate, $claimStatusUpdatedBy, $deductedAmount, $totalDeductedAmount, $deductedReason);
    // }


} else {
  header('location: ../../login.php');
}

//     $obj = new updateClaimStatus();
// $obj->updateClaim("200424", "2","2020-08-01","350", "No","0", "sdsd", "TL", "180291", "sai", "2020-09-23","180291sai","0","0", "reas");
class updateClaimStatus
{
    function __construct()
    {
        require_once dirname(__DIR__) . '../../php/DB_Connect.php';
        $DBclassObject = new DB_conncect;
        $this->db = $DBclassObject->connectToDB();


         }
    function updateClaim($uniqueID, $claimStatus, $TotalAmount, $DepuationAllowance, $DaAmount, $otherBill, $hotelBill, $loggedInUserDepartment, $loggedInUserID, $loggedInUserName, $EditedDate, $claimStatusUpdatedBy, $deductedAmount, $totalDeductedAmount, $deductedReason) // here  $Reason is not used, it need to insert
    {


        if ($loggedInUserDepartment == TL_DEPARTMENT) {
            $query = $this->db->prepare("UPDATE `normal_site_visit_2` SET `CLAIM_STATUS`=?, `TL_APPROVED_DEPUTATION_ALLOWANCE`=?, `TL_APPROED_DA_AMOUNT`=?,`TL_APPROVED_HOTEL_BILLS_AMOUNT`=?,`TL_APPROVED_OTHER_BILLS_AMOUNT`=?, `TL_APPROVED_FINAL_AMOUNT`=?, `TL_APPROVED_DATE`=?, `TL_DEDUCTED_AMOUNT`=?,`TOTAL_DEDUCTED_AMOUNT`=?,`TL_DEDUCTED_REASON`=?, `FINANCE_APPROVED_DEPUTATION_ALLOWANCE`=?, `FINANCE_APPROVED_DA_AMOUNT`=?,`FINANCE_APPROVED_HOTEL_BILLS_AMOUNT`=?,`FINANCE_APPROVED_OTHER_BILLS_AMOUNT`=?,`FINANCE_APPROVED_FINAL_AMOUNT`=?,`CLAIM_STATUS_UPDATED_BY`=?  WHERE `UNIQUE_ID`=?");
            $query->bind_param("isddddsddssddddss", $claimStatus, $DepuationAllowance, $DaAmount, $hotelBill, $otherBill, $TotalAmount, $EditedDate, $deductedAmount, $totalDeductedAmount, $deductedReason, $DepuationAllowance, $DaAmount, $hotelBill, $otherBill, $TotalAmount, $claimStatusUpdatedBy, $uniqueID);
        } elseif ($loggedInUserDepartment == FINANCE_DEPARTMENT) {
            $query = $this->db->prepare("UPDATE `normal_site_visit_2` SET `CLAIM_STATUS`=?,`FINANCE_EMP_ID`=?,`FINANCE_NAME`=?,`FINANCE_APPROVED_DATE`=?,`FINANCE_APPROVED_DEPUTATION_ALLOWANCE`=?,`FINANCE_APPROVED_DA_AMOUNT`=?,`FINANCE_APPROVED_HOTEL_BILLS_AMOUNT`=?,`FINANCE_APPROVED_OTHER_BILLS_AMOUNT`=?,`FINANCE_APPROVED_FINAL_AMOUNT`=?, `FINANCE_DEDUCTED_AMOUNT`=?,`TOTAL_DEDUCTED_AMOUNT`=?,`FINANCE_DEDUCTED_REASON`=?, `CLAIM_STATUS_UPDATED_BY`=? WHERE `UNIQUE_ID`=?");
            $query->bind_param("issssddddddsss", $claimStatus, $loggedInUserID, $loggedInUserName, $EditedDate, $DepuationAllowance, $DaAmount,$hotelBill, $otherBill, $TotalAmount, $deductedAmount, $totalDeductedAmount, $deductedReason, $claimStatusUpdatedBy, $uniqueID);
        }


        $query->execute();
        //echo "Affected rows: " . mysqli_affected_rows($this->db);

        // if(mysqli_affected_rows($this->db)>0){
        if ($query->execute()) {
            echo  "UPDATED";
        } else {
            echo "FAILURE";
        }
    }

    function rejectClaim($uniqueID, $claimStatus, $rejectReason, $EditedDate, $claimStatusUpdatedBy, $loggedInUserDepartment)
    {
        // if ($claimStatus == CLAIM_TL_REJECTED && $loggedInUserDepartment == FINANCE_DEPARTMENT) {
        //     $claimStatus = CLAIM_FINANCE_REJECTED;
        // }


        $query = $this->db->prepare("UPDATE `normal_site_visit_2` SET `CLAIM_STATUS`=?, `REJECT_REASON`=?, `TL_OR_FINANCE_REJECTED_DATE`=?, `CLAIM_STATUS_UPDATED_BY`=?  WHERE `UNIQUE_ID`=?");
        $query->bind_param("issss", $claimStatus, $rejectReason, $EditedDate, $claimStatusUpdatedBy, $uniqueID);

        if ($query->execute()) {
            echo  "UPDATED";
        } else {
            echo "FAILURE";
        }

    }


    function paidClaim($uniqueID, $claimStatus, $EditedDate, $claimStatusUpdatedBy)
    {
        $query = $this->db->prepare("UPDATE `normal_site_visit_2` SET `CLAIM_STATUS`=?, `PAID_DATE`=?, `PAID_BY`=?  WHERE `UNIQUE_ID`=?");
        $query->bind_param("isss", $claimStatus, $EditedDate, $claimStatusUpdatedBy, $uniqueID);

        if ($query->execute()) {
            echo  "UPDATED";
        } else {
            echo "FAILURE";
        }

    }
    
    function undoClaim($claimStatus, $loggedInUserDepartment, $uniqueID){
        if($loggedInUserDepartment==TL_DEPARTMENT){
            $claimStatus=CLAIM_PENDING;
        }elseif($loggedInUserDepartment==FINANCE_DEPARTMENT){
            $claimStatus=CLAIM_TL_APPROVED;
        }
        
        // $query = $this->db->prepare("UPDATE `normal_site_visit_2` SET `CLAIM_STATUS`=? WHERE `UNIQUE_ID`=?");
        // $query->bind_param("is", $claimStatus, $uniqueID);
        
        //   if ($query->execute()) {
        //     echo  "UPDATED";
        // } else {
        //     echo "FAILURE";
        // }
        $query = $this->db->prepare("UPDATE `normal_site_visit_2` SET  `CLAIM_STATUS`=? WHERE `UNIQUE_ID`=?");
        $query->bind_param("is", $claimStatus, $uniqueID);
            if($query->execute())
            {
                echo  "UPDATED";
            }else{
                echo "FAILURE";
            }
    }

}
