<div class="container-fluid">
   <div class="row">
      <div class="col-sm-12 col-lg-12">
         <div class="iq-card">
            <div class="iq-card-header d-flex justify-content-between">
               <div class="iq-header-title">
                  <h4 class="card-title">Edit Health Checkup</h4>
               </div>
               <span class="table-add float-right mb-3 mr-2">
                  <a href="<?php echo base_url(); ?>HealthCheckup/" class="btn btn-default pull-right"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</a>&emsp;
               </span>
            </div>
            <div class="iq-card-body">
               <div class="alert alert-success Add" style="display: none;">
                    <strong>Successfully Edited Your Data!</strong>
                </div>
                <div class="alert alert-danger Adderror" style="display: none;">
                    <strong>Duplicate Entry!</strong>
                </div>
                <form role="form" method="post" id="editfrm">
                <div class="row">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <label>Health Checkup Name</label>
                            <input type="text" name="hc_name" id="hc_name" class="form-control" required="" placeholder="Enter Health Checkup Name" value="<?php echo $health_checkup[0]['hc_name']; ?>"><br>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        <div class="form-group">
                            <label>This test is for </label>
                            <input type="text" name="test_for" id="test_for" class="form-control" required="" placeholder="Example: Male,Female" value="<?php echo $health_checkup[0]['test_for']; ?>"><br>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        <div class="form-group">
                            <label>Reports Time </label>
                            <input type="text" name="get_reports" id="get_reports" class="form-control" required="" placeholder="Example: 24 hours" value="<?php echo $health_checkup[0]['get_reports']; ?>"><br>
                        </div>
                    </div>
                    
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        <div class="form-group">
                            <label>Total Price</label>
                            <input type="text" name="total_price" id="total_price" class="form-control" required="" placeholder="Enter Price" value="<?php echo $health_checkup[0]['total_price']; ?>"><br>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        <div class="form-group">
                            <label>Amount Payble</label>
                            <input type="text" name="pay_amount" id="pay_amount" class="form-control" required="" placeholder="Enter Amount" value="<?php echo $health_checkup[0]['pay_amount']; ?>"><br>
                        </div>
                    </div>
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <label>Test Preparation</label>
                            <textarea  class="form-control summernote" name="test_preparation" required><?php echo $health_checkup[0]['test_preparation']; ?></textarea>
                            <input type="hidden" name="hc_id" value="<?php echo $health_checkup[0]['hc_id']; ?>">
                            <br>
                        </div>
                    </div>
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <label>Test Includes List </label>
                            <div id="timingWrapper">
                                <?php $count = 0; ?>
                                <?php foreach ($hc_test as $timing): ?>
                                    <div class="timing-row" id="row-<?= $count ?>">
                                        <div class="row">
                                            <div class="col-lg-8">
                                                <input type="text" name="test_name[<?= $count ?>][name]" class="form-control" value="<?= $timing['test_name'] ?>" required>
                                            </div>
                                            <div class="col-lg-2">
                                                <span class="remove-btn btn btn-danger btn-sm mt-1" onclick="removeRow(<?= $count ?>)">Remove</span>
                                            </div>
                                        </div><br>
                                    </div>
                                    <?php $count++; ?>
                                <?php endforeach; ?>
                            </div>
                            <button type="button" class="btn btn-success" id="addTiming">Add More</button><br>
                        </div>
                    </div>
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <label>Smart Report Measure</label>
                            <div id="timingWrapper1">
                                <?php $count1 = 0; ?>
                                <?php foreach ($hcrm as $hcrms): ?>
                                    <div class="timing-row" id="row-<?= $count1 ?>">
                                        <div class="row">
                                            <div class="col-lg-10">
                                                <input type="text" name="titles[<?= $count1 ?>][title]" class="form-control" value="<?= $hcrms['title'] ?>" required></br>
                                            </div>
                                            <div class="col-lg-2">
                                                <span class="remove-btn btn btn-danger btn-sm mt-1" onclick="removeRow1(<?= $count1 ?>)">Remove</span>
                                            </div>
                                            <div class="col-lg-12">
                                            <textarea class="form-control summernote" name="titles[${count1}][desc]" required placeholder="Enter Description"><?= $hcrms['description'] ?></textarea>
                                            </div>
                                        </div><br>
                                    </div>
                                    <?php $count1++; ?>
                                <?php endforeach; ?>
                            </div>
                            <button type="button" class="btn btn-success" id="addTiming1">Add More</button><br>
                        </div>
                    </div>
                    
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" align="right">
                        <div class="form-group">
                            <button type="reset" class="btn btn-default"><i class="fa fa-repeat" aria-hidden="true"></i> Reset</button>
                            <button type="submit" class="btn btn-primary addbtn"><i class="fa fa-floppy-o" aria-hidden="true"></i> Save</button>
                            <button class="btn btn-primary addload buttonload" style="display: none;"><i class="fa fa-spinner fa-spin" style=" margin-left: -12px; margin-right: 8px;"></i>Please Wait</button>
                        </div>
                    </div>
                    
                </div>
                </form>
            </div>
         </div>
      </div>
   </div>
</div>
<script type="text/javascript">
    let count = <?= count($hc_test) ?>;

    function createTimingRow(index) {
        return `
        <div class="timing-row" id="row-${index}">
            <div class="row">
                <div class="col-lg-10 col-md-10 col-sm-10 col-xs-12">
                <input type="text" class="form-control" name="test_name[${index}][name]" required placeholder="Enter Test Name"/>
                </div>
                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
                <span class="remove-btn btn btn-danger btn-sm mt-1" onclick="removeRow(${index})">Remove</span>
                </div>
                </div><br>
            </div>`;
    }

    function removeRow(index) {
        document.getElementById(`row-${index}`).remove();
    }

    document.getElementById('addTiming').addEventListener('click', () => {
        $('#timingWrapper').append(createTimingRow(count++));
    });
</script>
<script>
    let count1 = <?= count($hcrm) ?>;

    function createTimingRow1(indexs) {
        return `
            <div class="timing-row" id="row-${indexs}">
            <div class="row">
                <div class="col-lg-10 col-md-10 col-sm-10 col-xs-12">
                <input type="text" class="form-control" name="titles[${indexs}][title]" required placeholder="Enter Title"/></br>
                </div>
                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
                <span class="remove-btn btn btn-danger btn-sm mt-1" onclick="removeRow1(${indexs})">Remove</span>
                </div>
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <textarea class="form-control summernote1" name="titles[${indexs}][desc]" required placeholder="Enter Description"></textarea>
                </div>
                </div><br>
            </div>
        `;
    }

    function removeRow1(indexs) {
        $(`#row-${indexs}`).remove();
    }

    $("#addTiming1").on("click", function() {
        $("#timingWrapper1").append(createTimingRow1(count1++));
        $(`#row-${count1 - 1} .summernote1`).summernote({
            height: 100,
            placeholder: 'Description'
        });
    });
</script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
success: "valid"
});
$("#editfrm").validate({
rules: {
d_code: {
required: true
}
}
});
</script>
<script>
$(document).ready(function(e) {
$("#editfrm").on('submit', function(e) {
e.preventDefault();
var formStatus = $('#editfrm').validate().form();
if (true == formStatus) {
$.ajax({
type: 'POST',
url: '<?php echo base_url(); ?>HealthCheckup/Edit_data/',
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
beforeSend: function() {
$('.addbtn').hide();
$('.addload').show();
},
success: function(msg) {
if (msg == 'success') {
$(".Add").show();
$('.Add').delay(3000).hide(200);
$('.addbtn').show();
$('.addload').hide();
setInterval(function() {
location.reload();
}, 3000);
}
}
});
}
});
});
</script>
<script>
$( "#cat_id" ).change(function() {
var cat_id=$( "#cat_id" ).val();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>product/get_sub_cat/",
data: {cat_id : cat_id }, 
success: function(html) {
$('.sub_category').html(html);
}
});
});
</script>