
<div class="breadcrumb">
    <h1>Faq</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li><?php echo (isset($faqDetail->id)) ? "Edit Faq" : "Add New Faq"; ?></li>
    </ul>
</div>
<div class="separator-breadcrumb border-top"></div>

<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">
                    <?php echo (isset($faqDetail->id)) ? "Edit Faq" : "Add New Faq"; ?>
                </div>

                <form action="" id="frm-faq" method="post" enctype="multipart/form-data">
                    <?php if(isset($faqDetail->id)) { ?>
                        <input type="hidden" id="doc_id" name="doc_id" value="<?php echo $faqDetail->id ?>" />
                    <?php } ?>

                    <div class="row">
                        <div class="col-md-6 form-group mb-3">
                            <label for="question">Question <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="question" name="question" 
                                   value="<?php echo isset($faqDetail->question) ? $faqDetail->question : ''; ?>" 
                                   placeholder="Enter question">
                            <div class="invalid-tooltip" id="msg_question"></div>
                        </div>

                        <div class="col-md-3 form-group mb-3">
                            <label class="control-label">Status</label>
                            <select class="form-control" id="status" name="status">
                                <option value="1" <?php echo (isset($faqDetail) && $faqDetail->status == 1) ? "selected" : ""; ?>>Active</option>
                                <option value="0" <?php echo (isset($faqDetail) && $faqDetail->status == 0) ? "selected" : ""; ?>>Inactive</option>
                            </select>
                        </div>

                        <div class="col-md-12 form-group mb-3">
                            <label for="answer">Answer</label>

                            <textarea class="ckeditor"  id="answer" name="answer" ><?php echo isset($faqDetail->answer) ? $faqDetail->answer : ''; ?></textarea>
                        </div>

                        <!-- Submit Button -->
                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="./assets/ckeditor/ckeditor.js"></script>

<script type="text/javascript">
$( "form" ).on( "submit", function(e) {
    e.preventDefault();

    var question = $("#question").val();
    var answer = CKEDITOR.instances['answer'].getData();
    var status = $("#status").val();
    if(question == ''){
        $("#question").addClass('error');
        $("#question").focus();
        $("#msg_question").html('Please input question').show();
        return false;
    } else {
        $("#question").removeClass('error');
        $("#msg_question").hide();
    }
    
    <?php if(!isset($_REQUEST['id'])) { ?>
        // Insert new
        $.ajax({
            url: "faqadd.php",
            method: 'POST',
            data: { act:"load", question:question, answer:answer, status:status },
            success: function(msg){
                if(msg!=''){
                    window.location='faqlist.php';
                }
            }
        });
    <?php } else { ?>
        // Update existing
        $.ajax({
            url: "faqadd.php",
            method: 'POST',
            data: { act:"load", id:<?php echo $_REQUEST['id']; ?>, question:question, answer:answer, status:status },
            success: function(msg){
                if(msg!=''){
                    window.location='faqlist.php';
                }
            }
        });
    <?php } ?>
});
</script>
