<?php
/**
 * Customer Page
 * Author: Ritu modi
 * Date: 19-02-2024
 * 
 * This page is used to display customer details and related information. It decodes the customer ID received through the GET request, fetches customer data based on the ID, and displays it on the page. It also allows for updating customer information and viewing associated case information and documents.
 */
// Decode the customer ID received through GET request
   $customerid =  base64_decode($_GET['CustomerID']);
   //echo $customerid; die;
   // Get the current user ID from session
   $vuserid     =   $_SESSION['userid'];
  
// Fetch customer data based on the customer ID
   $resonse = fetch_customer_data($customerid);
   $res = mysqli_fetch_assoc($resonse);
  // Extract individual data fields from the fetched customer data
    $phone =    $res['phone'];
    $mobile=    $res['mobile'];
    $name =  explode(' ', $res['fname']);// Split fname into an array of two values seperated by space.
    $first_name     = $name[0];
    $last_name      = $res['lname'];
    $email          =$res['email'];
    $gender          =$res['gender'];
    $age          =$res['age_grp'];
    $address_1      = $res['address'];
    $address_2      = $res['v_Location'];
    $district       = $res['district'];
    $v_Village       = $res['v_Village'];
    $languagename   = $res['language'];
    $fbhandle       = $res['fbhandle'];
    $twitterhandle  = $res['twitterhandle'];
    $type= $res['type'];
    $loaneeno = $res['loaneeNo'];
    $nationalId = $res['nationalID'];
    $business_number = $res['business_number'];
    $register_tpin = $res['tpin'];
    $passport_number = $res['passport_number'];
    $area = $res['area'];
    $town = $res['town'];
    $whatsapphandle = $res['whatsapphandle'];
    $instagramhandle = $res['instagramhandle'];//[vastvikta nishad][29-11-2024]
    $messengerhandle = $res['messengerhandle'];//[vastvikta nishad][29-11-2024]
    $sms_number = $res['smshandle'];
    $nationality= $res['nationality'];
    $companyname= $res['company_name'];
    $company_registration= $res['company_registration'];
    $regional= $res['regional'];
    $customertype= $res['customertype'];
    $final_action = "Customer Detial View of Customer ID $customerid by $name User";
    $priority = $res['priority_user'];
  //  add_audit_log($vuserid, 'customer_detial_view ', $customerid, 'Show Customer Detail', $db, $final_action);

    $selecttab=3;
    ?>
<style type="text/css">
  .content__history {
    display: flex;
    flex-direction: column;
    height: calc(100% - 18px - 3em);
    /*overflow: auto;*/
    /*overflow-x: hidden;*/
    font-size: 13px;
}
.content__history__entry__content__data {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid rgba(123,123,123,.3);
    padding: .8em 0;
    margin-right: .8em;
    align-items: center;
    border-left: 1px solid rgba(123,123,123,.3);
}
.content__history__entry__header {
  font-size: inherit;
    font-weight: 500;
    height: 47px;
    text-transform: uppercase;
    border: #228b22 2px solid;
    border-bottom: none;
    border-right: none;
    /* display: inline-block; */
    /* float: left; */
    margin-bottom: unset;
    border-top-left-radius: 25px;
    padding: 13px 15px;
    width: 100%;
    background-color: #f5f5f5;
    display: flex;
    justify-content: space-between;
    /*margin-bottom: 1em;*/
    /*font-size: 16px;*/
    /*font-weight: 700;*/
}
.content__entry__left{
  margin-left: 10px;
}
.content__history__entry {
    padding: 1em;
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;
    background-color: #fff;
    /*height: calc(30% - 40px);*/
    height: calc(92% - 40px);
    margin-bottom: 1em;
        /*margin-bottom: -126px;*/

}
.content__history__entry__content {
    display: flex;
    flex-direction: column;
    /*height: calc(100% - 24px - -16em);*/
    overflow: auto;
    overflow-x: hidden;
    font-size: 13px;
    margin-bottom: -126px;
    height: 90%;
}
.text-with-dots::after {
    content: ".";
}
</style>
<div class="wrapper">
    <div class="container-fluid">
        <div class="row" style="min-height:88vh">
            <div class="rightpanels">
                <div class="style2-table">        
                    <div class="table"> 
                        <div class="row">
                            <div class="col-sm-12">                          
                                <div class="old-customer-simple-table" id="formContainer">
                                    <?php if(empty(!$customerid)){?>
                                        <span class="breadcrumb_head" style="height:37px;padding:9px 16px">Customer Profile</span>
                                    <?php }else{?>
                                        <span class="breadcrumb_head" style="height:37px;padding:9px 16px">New Customer Profile</span>
                                    <?php }?>
                                    <form name="frmviewcustomer" id="frmviewcustomer" method="post" novalidate="novalidate">
                                        <input type="hidden" class="upadate_data" value="upadate_data" name="action">
                                        <table class="tableview tableview-2 main-form new-customer">
                                        <?php if(!empty($customerid)){?>
                                            <tbody>
                                                <tr class="">
                                                    <td height="44" colspan="2" align="left">
                                                        <div class="log-case">
                                                            <input name="customerid" id="customerid" type="hidden" value="<?=$customerid?>" class="input-style1" readonly="">
                                                        </div>
                                                    </td>
                                                    <!-- <td height="44" colspan="2" align="right"> 
                                                  <input name="customerid" id="customerid" type="submit" value="Edit"
                                                        class="button-orange1 float-right"> <button type='button' id='editBtn' class="button-orange1 float-right" >Edit</button>
                                                    </td> -->
                                                </tr>
                                            </tbody>
                                            <?php }?>
                                        </table>
                                        <!-- changed layout of the form[vastvikta]][14-05-2025] -->
                                        <table class="tableview tableview-2 main-form new-customer">
                                            <tbody>
                                                <tr>
                                                    <td class="left boder0-left" width="40%">
                                                        <label>Phone Number</label>
                                                        <div class="log-case">
                                                            <input name="phone" id="phone" type="text" maxlength="12" value="<?=$phone?>" class="input-style1"  style="pointer-events: none;width:150px;">
                                                        </div>
                                                    </td>
                                                    <td class="left boder0-left" width="40%">
                                                        <label>Loanee Number</label>
                                                        <div class="log-case">
                                                            <input name="loaneeno" id="loaneeno" type="text" maxlength="12" value="<?=$loaneeno?>" class="input-style1"  readonly style="width:150px;">
                                                        </div>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="left boder0-left" width="40%">
                                                        <label>National Id</label>
                                                        <div class="log-case">
                                                            <input name="nationalid" id="nationalid" type="text" value="<?=$nationalId?>" class="input-style1" readonly style="width:150px;">
                                                        </div>
                                                    </td>   
                                                    <td class="left boder0-right" width="40%">
                                                        <label>First Name</label>
                                                        <div class="log-case">
                                                            <?php $first_name = str_replace("singlequote", "'", $first_name); ?>
                                                            <input name="first_name" id="first_name" type="text" value="<?=$first_name?>" class="input-style1" readonly onkeypress="return isAlphabetKey(event)" style="width:150px;">
                                                        </div>
                                                    </td>
                                                    <tr>
                                                    <td class="left boder0-right" width="40%">
                                                        <label>Last Name</label>
                                                       
                                                        <div class="log-case">
                                                            <input name="last_name" id="last_name" type="text" value="<?=$last_name?>" class="input-style1" readonly onkeypress="return isAlphabetKey(event)" style="width:150px;">
                                                        </div>
                                                    </td>
                                                    <td class="left boder0-right" width="40%">
                                                        <label>Priority Customer</label>
                                                        <div class="log-case">
                                                            <span class="slug">
                                                                <input type="radio" name="priority" value="1" disabled <?= $priority == '1' ? 'checked' : '' ?>> Priority
                                                            </span>
                                                            <span class="slug">
                                                                <input type="radio" name="priority" value="0" disabled <?= $priority == '0' ? 'checked' : '' ?>> Non Priority
                                                            </span>
                                                        </div>
                                                    </td>
                                        </tr>
                                                    <tr>
                                                         <td class="left boder0-left" width="40%">
                                                        <label>Email</span></label>
                                                        <input type="email" disabled name="email" id="email" value="<?=$email?>" class="input-style1" >
                                                    </td> 
                                        </tr>
                                                    
                                                <?php if (!empty($customerid)) { ?>
                                                <tr>
                                                    <td colspan="2" style="text-align: center;">
                                                        <button type="button" id="updateBtn" class="button-orange1" style="display:none;">Update</button>
                                                        <button type="button" id="cancelBtn" class="button-orange1" style="display:none;">Cancel</button>
                                                    </td>
                                                </tr>
                                                <?php } ?>
                                                <?php if (empty($customerid)) { ?>
                                                <tr>
                                                    <td colspan="2" style="text-align: center;">
                                                        <button type="button" id="insertBtn" class="button-orange1">Insert</button>
                                                        <?php $web_customer_detail = base64_encode('web_consumer_home'); ?>
                                                        <a id="backLink" href="customer_index.php?token=<?=$web_customer_detail?>" style="display:none;"></a>
                                                        <button type="button" id="backBtn" class="button-orange1">Back</button>
                                                        <script>
                                                            document.getElementById("backBtn").addEventListener("click", function () {
                                                                document.getElementById("backLink").click();
                                                            });
                                                        </script>
                                                    </td>
                                                </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                    </form>
                                </div>
                            </div>
                          </div>
                        </div>
                    </div>
                </div>
               <?php if(!empty($customerid)){?>
               <!-- Documents upload -->
               <div class="old-customer-table">
                  <h6>Documents</h6>
                  <table width="685" class="tableview tableview-2 main-form">
                  <tbody>
                     <tr bgcolor="#dddddd"class="background">
                        <td width="30% " align="center"><b>Document Name </b></td>
                        <td width="15%" align="center"><b>Uploaded By</b></td>
                        <td width="20%" align="center"><b>Uploaded On</b></td>
                        <td width="20%" align="center"><b>Document</b></td>
                     </tr>
                  <?php
                  $documents_str = '';
                  $documents_chat = '';
                  $countopp = 0;
                  $resopp = get_documents($customerid,$groupid);
                  $rowcolopp            =   "";
                  $rownumopp            =   0;
                  $enddocuments = '';
                  $documents = '';
                  while($rowopp=mysqli_fetch_array($resopp)){
                     $rownumopp=$rownumopp+1;
                     $I_DocumentID          = $rowopp['I_DocumentID'];
                     $V_Doc_Name=$rowopp['V_Doc_Name'];
                     $V_Doc_Description=$rowopp['V_Doc_Description'];
                     $V_Path=$rowopp['V_Path'];
                     $v_uploadedFile=$rowopp['v_uploadedFile'];
                     $downloads            ="document/$db";
                     $v_uploadedFile=$rowopp['v_uploadedFile'];
                     $exp_upload=explode(",",$v_uploadedFile);
                     if(count($exp_upload)>1){
                        for($k=0;$k<count($exp_upload);$k++)
                        {
                         $f=$downloads."/".$exp_upload[$k];
                           $img="<img src=../public/images/download.jpg title='Click to download $f' border=0>";
                           $downloadpath1.="<a href='JavaScript:void(0)' onClick=\"JavaScript:window.open('$f')\" class='cptext' 'view','height=350, width=550,scrollbars=0'>".$img."</a> &nbsp;&nbsp;";
                      }
                     }else if(count($exp_upload)==1){
                       $f=$downloads."/".$v_uploadedFile;
                       $img="<img src=../public/images/download.jpg title='Click to download $f' border=0>";
                       $downloadpath1="<a href='JavaScript:void(0)' onClick=\"JavaScript:window.open('$f')\" class='cptext' 'view','height=350, width=550,scrollbars=0'>".$img."</a>";
                     }
                     $cdateopp=$rowopp['I_UploadedON'];
                     $conopp1=explode(" ",$cdateopp);
                     $conopp=explode("-",$conopp1[0]);
                     $createdonopp=$conopp[2].'-'.$conopp[1].'-'.$conopp[0];
                     $madebyopp=$rowopp['I_UploadedBY'];
                     $countopp=$countopp+1;

                     if($rownumopp%2==0)
                     $rowcolopp="white";
                     else
                     $rowcolopp="#EFEFEF";
                  $documents.='<tr bgcolor="#FFFFFF">
                  <td bgcolor="'.$rowcolopp.'" class="normaltextabhi">'.$V_Doc_Name.'</td>
                  <td bgcolor="'.$rowcolopp.'" class="normaltextabhi">'.$madebyopp.'</td>
                  <td bgcolor="'.$rowcolopp.'" class="normaltextabhi">'.$createdonopp.'</td>
                  <td bgcolor="'.$rowcolopp.'" class="normaltextabhi">'.$downloadpath1.'</td>
                  </tr>';
                  }
                  if(empty($countopp) && $documents_str=='' && $documents_chat == ''){
                     $enddocuments.='<tr bgcolor="white" height=22><td colspan="9" align="center" class="ttext">No Documents added.</td></tr>';
                  }
                  echo $documents.$documents_str.$enddocuments.$documents_chat;
                  ?>
                  </tbody>
                  </table>
                  <?php }?>
                </div>
              
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="<?=$SiteURL?>public/js/ss.custom.js" ></script><script type="text/javascript">
</script>
