<?php require_once('../auth/auth.php'); ?>
<?php
$acid = $_GET['acid'];
$accounts_query = mysqli_query($conn, "SELECT * FROM accounts WHERE acid='$acid' ") or die(mysqli_error($conn));
$rowaac = mysqli_fetch_assoc($accounts_query);
$gcatid = $rowaac['gcatid'];
$ac_area = $rowaac['ac_area'];
$userrrid = $rowaac['em_id'];

$custom_field = mysqli_query($conn, "SELECT customer FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
$rowcf = mysqli_fetch_assoc($custom_field);
$debitor_id = mysqli_fetch_assoc(mysqli_query($conn, "SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$coid' AND g_type='su-deb' LIMIT 1 "))['ac_g_catid'];

$p = $rowcf['customer'];
function input_check($ap)
{
   if (strpos($GLOBALS['p'], $ap) !== false) {
      return 1;
   }
}
$group_check = mysqli_query($conn, "SELECT g_type FROM ac_sub_g_category WHERE ac_g_catid='$gcatid'  AND coid='$coid' LIMIT 1") or die(mysqli_error($conn));
$check_row = mysqli_fetch_assoc($group_check);
$gttype = $check_row['g_type'];
?>
<section class="content">
   <div class="container-fluid">
      <div class="row">
         <div class="col-md-12">
            <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
               <div class="card-header" style="padding: 5px 8px;">
                  <h3 class="card-title"> <?php echo language(25); ?> </h3>
                  <div class="card-tools"> <button type="button" class="btn btn-tool" data-card-widget="remove"> <i class="fas fa-times"></i> </button> </div>
               </div>
               <form id="account_update_form" action="#" method="post" autocomplete="off">
                  <input type="hidden" name="update_from" required="true">
                  <input type="hidden" name="acid" value="<?php echo $acid; ?>" required="true">
                  <div class="card-body">
                     <div class="col-md-12">
                        <div class="row mt-2">
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label><?php echo language(215); ?> <b class="text-danger"> *</b></label>
                                 <input type="text" name="ac_name" value="<?php echo @$rowaac['ac_name']; ?>" class="form-control form-control-sm inputselect">
                              </div>
                           </div>
                           <div class="col-md-2">
                              <div class="form-group">
                                 <label> <?php echo language(110); ?></label>
                                 <input type="text" name="ac_alise" value="<?php echo @$rowaac['ac_alise']; ?>" class="form-control form-control-sm inputselect">
                              </div>
                           </div>
                           <div class="col-md-3">
                              <div class="form-group">
                                 <label> <?php echo language(111); ?></label>
                                 <input type="number" name="ac_number" value="<?php echo @$rowaac['ac_number']; ?>" id="checknumber" onkeyup="checknumber();" class="form-control form-control-sm inputselect">
                              </div>
                           </div>
                           <div class="col-md-3 <?php if (input_check('email') !== 1) {
                                                   echo 'd-none';
                                                } ?>">
                              <div class="form-group">
                                 <label> <?php echo language(112); ?></label>
                                 <input type="emai
                              l" name="ac_email" value="<?php echo $rowaac['ac_email']; ?>" class="form-control form-control-sm inputselect">
                              </div>
                           </div>
                           <?php if (input_check('s_address') == 1) { ?>

                              <div class="col-md-4">
                                 <div class="form-group">
                                    <label><?php echo language(113); ?> </label>
                                    <select name="divisions" id="divisions" onchange="divisionsList();" class="form-control" style="width: 100%;">
                                       <option selected value="<?php echo $rowaac['divisions']; ?>"> <?php echo $rowaac['divisions']; ?></option>
                                       <option value="Barishal">Barishal</option>
                                       <option value="Chattogram">Chattogram</option>
                                       <option value="Dhaka">Dhaka</option>
                                       <option value="Khulna">Khulna</option>
                                       <option value="Mymensingh">Mymensingh</option>
                                       <option value="Rajshahi">Rajshahi</option>
                                       <option value="Rangpur">Rangpur</option>
                                       <option value="Sylhet">Sylhet</option>
                                    </select>
                                 </div>
                              </div>
                              <div class="col-md-4">
                                 <div class="form-group">
                                    <label> <?php echo language(114); ?> </label>
                                    <select id="distr" name="distr" onchange="thanaList();" class="form-control">
                                       <option value="<?php echo $rowaac['distr']; ?>" selected> <?php echo $rowaac['distr']; ?> </option>
                                    </select>
                                 </div>
                              </div>
                              <div class="col-md-4">
                                 <div class="form-group">
                                    <label> <?php echo language(115); ?> </label>
                                    <select id="polic_sta" name="polic_sta" class="form-control">
                                       <option value="<?php echo $rowaac['polic_sta']; ?>" selected> <?php echo $rowaac['polic_sta']; ?> </option>

                                    </select>
                                 </div>
                              </div>
                           <?php } else { ?>
                              <input type="hidden" name="divisions" value="<?php echo $rowaac['divisions']; ?>">
                              <input type="hidden" name="distr" value="<?php echo $rowaac['distr']; ?>">
                              <input type="hidden" name="polic_sta" value="<?php echo $rowaac['polic_sta']; ?>">


                           <?php } ?>
                           <div class="col-md-5">
                              <div class="form-group">
                                 <label> <?php echo language(116); ?></label>
                                 <input name="ac_details" value="<?php echo $rowaac['ac_details']; ?>" class="form-control form-control-sm inputselect">
                              </div>
                           </div>

                           <div class="col-md-4">
                              <div class="form-group">
                                 <label> <?php echo language(118); ?> </label>
                                 <input type="text" name="out_it" value="<?php echo $rowaac['outlet_id']; ?>" class="form-control form-control-sm inputselect">
                              </div>
                           </div>
                           <?php if ($gttype == 'su-deb' || $gttype == 'su-cre' || $gttype == 'sl-exp') { ?>
                              <div class="col-md-3">
                                 <div class="form-group">
                                    <label><?php echo language(119); ?> </label>
                                    <select id="customer_type" name="customer_type" class="form-control">
                                       <?php if ($rowaac['acsub_type'] == 'ws') {
                                          echo '<option value="ws">WholeSaler </option>';
                                       } else if ($rowaac['acsub_type'] == 'retail') {
                                          echo '<option value="retail">Retail </option>';
                                       } else if ($rowaac['acsub_type'] == 'distri') {
                                          echo '<option value="distri">Dristributor </option>';
                                       } else if ($rowaac['acsub_type'] == 'emi') {
                                          echo '<option value="emi">EMI (installment) </option>';
                                       } else if ($rowaac['acsub_type'] == 'B2B') {
                                          echo '<option value="B2B">B2B (sales) </option>';
                                       } ?>
                                       <option value="<?php echo $rowaac['acsub_type']; ?>"></option>
                                       <option value="ws">WholeSaler</option>
                                       <option value="retail">Retail</option>
                                       <option value="distri">Dristributor</option>
                                       <option value="emi">EMI (installment) </option>
                                       <option value="B2B">B2B (sales) </option>
                                    </select>
                                 </div>
                              </div>
                              <div class="col-md-3 <?php if (input_check('discount') !== 1) {
                                                      echo 'd-none';
                                                   } ?>">
                                 <div class="form-group">
                                    <label> <?php echo language(88); ?></label>
                                    <input type="text" name="dis_margin" value="<?php echo $rowaac['ac_dis']; ?>" class="form-control form-control-sm inputselect" placeholder="0%" style="text-align:center;">
                                 </div>
                              </div>
                              <div class="col-md-3  <?php if (input_check('cr_limit') !== 1) {
                                                         echo 'd-none';
                                                      } ?>">
                                 <div class="form-group">
                                    <label> <?php echo language(120); ?> </label>
                                    <input type="text" name="crlimit" value="<?php echo $rowaac['cr_limit']; ?>" class="form-control form-control-sm inputselect">
                                 </div>
                              </div>

                              <div class="col-md-3">
                                 <div class="form-group">
                                    <label><?php echo language(214); ?> </label>
                                 
                                 
                                    
                                    
                                     <select  name="emid"  class="form-control" > 
                             <?php $emdd_query= mysqli_query($conn,"SELECT uid,us_em_id FROM user 
                             WHERE user.us_coid='$coid'  AND user.us_bid='$bid'  AND  user.us_brid='$brid'  AND user.us_status=1  ") or die (mysqli_error($conn)); 
                             
                             
                             while($rouid= mysqli_fetch_assoc($emdd_query)){ $get_emdata=$rouid["us_em_id"]; 
                             $em_acname= mysqli_fetch_assoc(mysqli_query($conn,"SELECT em_acname FROM em_employee WHERE em_coid='$coid' AND em_bid='$bid' AND em_brid='$brid' AND em_id='$get_emdata' LIMIT 1"))['em_acname'];
                             $id_card = mysqli_fetch_assoc(mysqli_query($conn,"SELECT id_card FROM em_employee WHERE em_coid='$coid' AND em_bid='$bid' AND em_brid='$brid' AND em_id='$get_emdata' LIMIT 1"))['id_card'];

                             echo '<option value='.$rouid["us_em_id"].'>'.$em_acname.'  ('.$id_card.') </option>'; }?> 
                        </select>
                                 </div>
                              </div>
                              <div class="col-md-3">
                                 <div class="form-group">
                                    <label> <?php echo language(124); ?> </label>
                                    <select id="areaid" name="areaid" class="form-control">

                                       <?php $road_name = mysqli_fetch_assoc(mysqli_query($conn, "SELECT roid,route_name,road_name FROM route_area WHERE  roid='$ac_area' LIMIT 1 "));
                                       echo '<option value=' . $road_name["roid"] . '>' . $road_name["route_name"] . "-" . $road_name["road_name"] . ' </option>';
                                       ?>
                                       <?php
                                       $em_Route = mysqli_query($conn, "SELECT * FROM route_area  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' ") or die(mysqli_error($conn));
                                       while ($rot = mysqli_fetch_assoc($em_Route)) {
                                          echo '<option value=' . $rot["roid"] . '>' . $rot["route_name"] . "-" . $rot["road_name"] . ' </option>';
                                       } ?>
                                    </select>
                                 </div>
                              </div>
                              <div class="col-md-4  <?php if (input_check('p_terms') !== 1) {
                                                         echo 'd-none';
                                                      } ?>">
                                 <div class="form-group">
                                    <label><?php echo language(127); ?> </label>
                                    <select id="p_terms" name="p_terms" class="form-control">
                                       <?php if ($rowaac['p_terms'] == '1') {
                                          echo '<option value="1">Cash Only </option>';
                                       } else if ($rowaac['p_terms'] == '2') {
                                          echo '<option value="2">Due 15th Of the Following Month </option>';
                                       } else if ($rowaac['p_terms'] == '3') {
                                          echo '<option value="3">Due By End Of The Following Month </option>';
                                       } else if ($rowaac['p_terms'] == '4') {
                                          echo '<option value="4">Payment due within 10 days </option>';
                                       } else if ($rowaac['p_terms'] == '5') {
                                          echo '<option value="5">Prepaid </option>';
                                       } ?>
                                       <option value="1">Cash Only</option>
                                       <option value="2">Due 15th Of the Following Month</option>
                                       <option value="3">Due By End Of The Following Month</option>
                                       <option value="4">Payment due within 10 days</option>
                                       <option value="5">Prepaid</option>
                                    </select>
                                 </div>
                              </div>
                           <?php } ?>

                           <!-- account info part  -->
                           <?php if ($debitor_id === $gcatid) {
                              $accounts_info_query = mysqli_query($conn, "SELECT * FROM account_info WHERE acid='$acid' ") or die(mysqli_error($conn));
                              $rowinfo = mysqli_fetch_assoc($accounts_info_query); ?>
                              <div class="text-center bg-danger col-md-12">Account Others Information</div>
                              <hr>

                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label> <?php echo language(117); ?> </label>
                                    <input type="text" name="nid" col_name="nid" value="<?php echo $rowinfo['nid']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>

                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label> <?php echo language(412); ?> </label>
                                    <input type="text" name="father_name" col_name="father_name" value="<?php echo $rowinfo['father_name']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>

                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label> <?php echo language(423); ?> </label>
                                    <input type="text" name="mother_name" col_name="mother_name" value="<?php echo $rowinfo['mother_name']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>

                              <div class="col-md-4">
                                 <div class="form-group">
                                    <label><?php echo language(411); ?> </label>
                                    <input type="text" name="brithday" col_name="brithday" class="form-control form-control-sm cuslive" customer_id="<?php echo $acid; ?>" onmouseover="lcdate(this)" style="text-align:center;" readonly="true"  value="<?php echo $rowinfo['brithday']; ?>">
                                 </div>
                              </div>
                              
                              <!---->
                              <div class="text-center bg-danger col-md-12">Account Guarantor Information</div>
                              <hr>
                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label> <?php echo language(429); ?> </label>
                                    <input type="text" name="g_name1" col_name="g_name1" value="<?php echo $rowinfo['g_name1']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>

                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label> <?php echo language(430); ?> </label>
                                    <input type="text" name="g_address1" col_name="g_address1" value="<?php echo $rowinfo['g_address1']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>

                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label><?php echo language(431); ?> </label>
                                    <input type="text" name="g_phone1" col_name="g_phone1" value="<?php echo $rowinfo['g_phone1']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>
                              
                                <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label> <?php echo language(432); ?> </label>
                                    <input type="text" name="g_name2" col_name="g_name2" value="<?php echo $rowinfo['g_name2']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>

                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label> <?php echo language(433); ?> </label>
                                    <input type="text" name="g_address2" col_name="g_address2" value="<?php echo $rowinfo['g_address2']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>

                              <div class="col-md-4 mt-3">
                                 <div class="form-group">
                                    <label><?php echo language(434); ?> </label>
                                    <input type="text" name="g_phone2" col_name="g_phone2" value="<?php echo $rowinfo['g_phone2']; ?>" customer_id="<?php echo $acid; ?>" class="form-control form-control-sm  cuslive" >
                                 </div>
                              </div>
                              <!---->

                              <div class="col-md-4">
                                 <div class="form-group">
                                    <label><?php echo language(123); ?></label>
                                    <input type="text" name="bbr_name" col_name="bbr_name" title="Banking And Financial Services Certificate." customer_id="<?php echo $acid; ?>" class="form-control form-control-sm inputselect cuslive" value="<?php echo $rowinfo['bbr_name']; ?>">
                                 </div>
                              </div>

                              <div class="col-md-4">
                                 <div class="form-group">
                                    <label><?php echo language(413); ?> </label>
                                    <select name="gender" col_name="gender" customer_id="<?php echo $acid; ?>" class="form-control cuslive">
                                       <option value="<?php echo $rowinfo['gender']; ?>">

                                          <?php if ($rowinfo['gender'] == 'Male') {
                                             echo language(414) . " (Male)";
                                          } else if ($rowinfo['gender'] == 'Female') {
                                             echo language(415) . "(Female)";
                                          } else if ($rowinfo['gender'] == 'Others') {
                                             echo language(416) . "(Others)";
                                          }
                                          ?>


                                       </option>
                                       <option value="Male"><?php echo language(414); ?> (Male)</option>
                                       <option value="Female"><?php echo language(415); ?> (Female)</option>
                                       <option value="Others"><?php echo language(416); ?> (Others)</option>


                                    </select>
                                 </div>
                              </div>




                              <div class="col-md-12">
                                 <div class="form-group">
                                    <label><?php echo language(128); ?> </label>
                                    <textarea name="g_note" col_name="g_note" class="form-control form-control-sm inputselect cuslive" customer_id="<?php echo $acid; ?>" rows="1" placeholder="Enter ..."><?php echo $rowinfo['g_note']; ?></textarea>
                                 </div>
                              </div>


                              <script type="text/javascript">
                                 $(document).ready(function() {
                                    $("select").select2();
                                    $(".cuslive").on('input change', function() {
                                       customer_id = $(this).attr("customer_id");
                                       col_name = $(this).attr("col_name");
                                       col_value = $(this).val();
                                       $.ajax({
                                          type: "POST",
                                          url: "../accounts/account_update_db.php",
                                          data: ({
                                             customer_id: customer_id,
                                             col_name: col_name,
                                             col_value: col_value
                                          }),
                                          cache: false,
                                       });
                                    });
                                 });
                              </script>
                           <?php } ?>

                           <!-- end  -->

                        </div>
                     </div>
                  </div>
                  <center>
                     <div class="card-footer" style="padding: 8px 5px;">
                        <button type="submit" id="save" class="btn btn-success btn-xs"><?php echo language(25); ?></button>
                     </div>
                  </center>
               </form>
            </div>
         </div>
      </div>
   </div>
</section>
<script>
   $(document).ready(function() {
      $("select").select2();
      $('#account_update_form').on('submit', function(e) {
         $('#save').prop('disabled', true);
         $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> ' + 'Loading...');
         e.preventDefault();
         $.post("../accounts/account_update_db.php", $(this).serialize(), function(data) {
            $('#account_update_form')[0].reset();
            $('#save').prop('disabled', false);
            $.post('../accounts/ac_profile_update.php?acid=<?php echo $acid ?>', {}, function(res) {
               $('#pview<?php echo $acid ?>').html(res);
            });
            toastr.success(Showing, yourdata);
         });
      });
   });
</script>