nhance/app/Views/client_policy.php

271 lines
10 KiB
PHP

<div class="tab-pane fade" id="police-tab">
<div class="row float-right" style="padding-bottom: 10px;">
<div class="col-6">
<button type="button" id="BtnAdd" class="btn btn-primary waves-effect waves-light btnAdd"><i class="fa fa-plus-square" aria-hidden="true"></i></button>
</div>
</div>
<div class="table-responsive" id="table_list" >
<table class="table table-borderless table-nowrap mb-0">
<thead class="thead-light">
<tr>
<th>Insurer</th>
<th>Policy</th>
<th>TPA</th>
<th>Action</th>
</tr>
</thead>
<tbody id="policy_table">
</tbody>
</table>
</div>
<div class="row" id="add_form">
<div class="col-12">
<div class="card-body">
<div class="row float-right" style="position: relative; bottom: 20px;">
<div class="col-6">
<button type="button" class="btn btn-primary waves-effect waves-light btnBack"><i class="fa fa-list" aria-hidden="true"></i></button>
</div>
</div>
<hr>
<form role="form" class="parsley-examples" method="post" id="policy_form"
enctype="multipart/form-data">
<input type="hidden" class="form-control" value="<?= csrf_hash() ?>" name="<?= csrf_token() ?>" />
<input type="hidden" name="PrimaryKey" id="policy_PrimaryKey" />
<input type="hidden" id="policy_form_action" />
<input type="hidden" name="client_id" id="client_id_police" value="<?= isset($client['id']) ? $client['id'] : '' ?>"/>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-4">
<label for="email">Insurer<span
class="text-danger">*</span></label>
<select class="form-control" id="insurer" name="insurer">
<option value="">Select Insurer</option>
<?php foreach($insurer as $value) { ?>
<option value="<?= $value['id'] . '-' . $value['insurer_id']?>"><?= $value['insurer_name'] . '-' . $value['branch_code'] ?></option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-4">
<label for="policy">Policies<span
class="text-danger">*</span></label>
<select class="form-control" id="policy" name="policy_id">
<option value="" selected>Select Policy</option>
</select>
</div>
<div class="form-group col-md-4">
<label for="mobile">TPA<span
class="text-danger">*</span></label>
<select class="form-control" id="tpa" name="tpa">
<option value="">Select TPA</option>
<?php foreach($tpa as $value) { ?>
<option value="<?= $value['id']. '-' . $value['tpa_id'] ?>"><?= $value['tpa_short_name'] . '-' . $value['branch_code'] ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<div class="form-group text-right m-b-0">
<button type="submit" class="btn btn-primary waves-effect waves-light mr-1"
id="btnSubmit">Submit</button>
<button type="button" class="btn btn-secondary waves-effect btnBack"
id="btnBack">Cancel</button>
</div>
</form>
</div>
</div> <!-- end col-->
</div>
</div>
<!-- end -->
<script>
$(document).ready(function () {
var policy_PrimaryKey = $('#client_id_police').val();
var policy_client = $('#policy_PrimaryKey').val();
$('#add_form').hide();
$('.btnBack').hide();
$('.btnAdd').click(function(){
$('#add_form').show();
$('#table_list').hide();
$('.btnBack').show();
$('.btnAdd').hide();
$('#policy_form_action').val('<?= base_url("client/policy/edit"); ?>');
;
})
$('.btnBack').click(function(){
$('#add_form').hide();
$('#table_list').show();
$('.btnBack').hide();
$('.btnAdd').show();
$('#insurer').val('');
$('#tpa').val('');
$('#policy').html('<option value="" selected>Select Policy</option>');
})
toastr.options = {
"timeOut": 2000,
"closeButton": true,
};
if (policy_PrimaryKey !== '') {
var policyTable = '';
var data = <?= isset($client_policy) ? json_encode($client_policy) : '[]' ?>;
console.log(data);
data.forEach(function(item) {
policyTable += `
<tr>
<td>${item.insurer_short} - ${item.insurer_branch_name}</td>
<td>${item.policy_name}</td>
<td>${item.tpa_short} - ${item.tpa_branch_code}</td>
<td><a data-id="${item.id}" class="btnPolicyEdit" href="#"><i data-id="${item.id}" class="mdi mdi-lead-pencil btnPolicyEdit" style="font-size:18px;"></i></a></td>
</tr>
`;
});
$('#policy_table').append(policyTable);
}
$("#policy_form").submit(function(event) {
event.preventDefault();
if (policy_PrimaryKey === '' && policy_client === '') {
toastr.error('Client ID is required', 'Error');
$('#insurer').val('');
$('#tpa').val('');
$('#policy').html('<option value="" selected>Select Policy</option>');
return;
}
var formData = new FormData($('#policy_form')[0]);
var policy_form_action = $('#policy_form_action').val();
$.ajax({
data: formData,
url: policy_form_action,
type: "POST",
dataType: 'json',
processData: false,
contentType: false,
success: function(res) {
if (res.status === false) {
toastr.error('Policy Dose Not Create', 'Error');
return;
}
console.log(res);
var message = (policy_PrimaryKey === '') ? 'Policy Created successfully' : 'Updated Successfully';
toastr.success(message, 'Success');
$('#policy_table tr').remove();
var policyTable = '';
$.each(res.data, function(index, item) {
policyTable += `
<tr>
<td>${item.insurer_short} - ${item.insurer_branch_name}</td>
<td>${item.policy_name}</td>
<td>${item.tpa_short} - ${item.tpa_branch_code}</td>
<td><a data-id="${item.id}" class="btnPolicyEdit" href="#"><i data-id="${item.id}" class="mdi mdi-lead-pencil btnPolicyEdit" style="font-size:18px;"></i></a></td>
</tr>
`;
});
$('#policy_table').append(policyTable);
$('#add_form').hide();
$('#table_list').show();
$('.btnBack').hide();
$('.btnAdd').show();
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
}
});
});
$('#insurer').change(function() {
var id = $(this).val();
var url = "<?= base_url("util/police-by-insurer/") ?>" + id;
$.get(url, function(res) {
res = JSON.parse(res)
var OptionsHTML = '';
$.each(res.data, function(index, item) {
OptionsHTML += '<option value="' + item.id + '">' + item.name + '</option>';
});
$('#policy').html(OptionsHTML);
}).fail(function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
});
});
$('body').on('click', '.btnPolicyEdit', function () {
var policy_form_action = '';
var policy_id = $(this).data('id');
$.ajax({
url: '<?= base_url("client/policy/list/") ?>' + policy_id,
type: "GET",
dataType: 'json',
success: function (res) {
$('#policy_form_action').val('<?= base_url("client/policy/edit"); ?>');
if (res.status === false) {
toastr.error(res.status);
return;
}
$('#add_form').show();
$('#table_list').hide();
$('.btnBack').show();
$('.btnAdd').hide();
$('#insurer').val(res.data.insurer_branch_id + '-' + res.data.insurer_id);
$('#tpa').val(res.data.tpa_branch_id + '-' + res.data.tpa_id);
$('#policy').val(res.data.policy_id);
$('#policy_PrimaryKey').val(res.data.id);
var policeOptionHTML = '';
$.each(res.policy, function(index, item) {
policeOptionHTML += '<option value="' + item.id + '" ' + (res.data.policy_id === item.id ? 'selected="selected"' : '') + '>' + item.name + '</option>';
});
$('#policy').html(policeOptionHTML);
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
}
});
});
});
</script>