<?php
$_SESSION['upload_dir'] = '/content-library/' . $person_info->url . '/my-work-file/'.$topic_url.'/';
$_SESSION['current_path'] = '../content-library/' . $person_info->url . '/my-work-file/'.$topic_url.'/';

$person_id = $this->session->userdata('person_id');
$directory = 'content-library/' . $person_info->url . '/my-work-file/' . $topic_url;
if (is_dir($directory)) {
    $_SESSION['upload_dir'] = '/content-library/' . $person_info->url . '/my-work-file/' . $topic_url . '/';
    $_SESSION['current_path'] = '../content-library/' . $person_info->url . '/my-work-file/' . $topic_url . '/';
} else {
    $_SESSION['upload_dir'] = '/source/' . $person_info->url . '/';
    $_SESSION['current_path'] = '../source/' . $person_info->url . '/';
}
?>

<div class="col-sm-10">
    <div class="row gray_ttc">
        <div class="col-sm-10 ">
            <div class="col-sm-12">
                <h3>Edit Topic</h3>
                <hr>
            </div>

            <div class="col-sm-12"><br></div>
            <div class="col-sm-12" id="max499">
                <?php
//                echo validation_errors(); //formID2
                $url = 'super-admin/edit-topic/' . $topic_info->url;
                echo form_open($url, 'class="" id="" name="add_topic_form" ');
                ?>
                <div class="form-group col-sm-8">
                    <div class="input-group">
                        <input type="text" 
                               data-errormessage-value-missing="Quiz Topic name is required!" 
                               class="form-control validate[required] text-input"
                               name="topic_name" id="topic_name"
                               value="<?php
                               if (set_value('topic_name')) {
                                   echo set_value('topic_name');
                               } else {
                                   echo $topic_info->topic_name;
                               }
                               ?>" 
                               placeholder="Please enter topic name"
                               oninput="checkUrlValue(this.value, urlValue)">
                        <span class="input-group-addon"  title="Please enter your  topic name"><i class="fa fa-book"></i></span>
                    </div>
                    <div id="topic_name_message" style="color:red">
                        <?php echo form_error('topic_name'); ?>
                    </div>
                </div>
                <div class=" col-sm-12"><br></div>

                <div class="form-group col-sm-6">
                    <div class="input-group">
                        <input type="text" data-errormessage-value-missing="topic url is required!"
                               class="form-control validate[required, custom[ttURL], custom[spaceFunction], custom[ttURLExistence]] text-input"  
                               name="topic_url" id="topic_url"
                               value="<?php
                               if (set_value('topic_url')) {
                                   echo set_value('topic_url');
                               } else {
                                   echo $topic_info->url;
                               }
                               ?>"
                               oninput="checkUrlValue(topic_name.value, this.value)"
                               placeholder="Please enter topic URL..">
                        <span class="input-group-addon"  title="Please enter topic URL"><i class="fa fa-link"></i></span>
                        <input type="hidden" id="urlcode" name='urlcode' value=''/>
                        <input type="hidden" name="url_exist" id="url_exist" value="">
                    </div>
                    <div id="topic_url_message" style="color:red">
                        <?php echo form_error('topic_url'); ?>
                    </div>
                </div>
                <div class=" col-sm-12"><br></div>

                <div class="form-group col-sm-12">
                    <div class="input-group" id="screen1280" >
                        <textarea class="form-control resoulation1280" name="topic_detail" rows="4"><?php
                            if (set_value('topic_detail')) {
                                echo set_value('topic_detail');
                            } else {
                                echo $this->utility_model->view_tinyMce_information($person_id, $topic_url, $topic_info->topic_detail);
                                //echo $topic_info->topic_detail;
                            }
                            ?></textarea>
                    </div>

                </div>
                <div class="col-sm-12"><br></div>                

                <div class="form-group col-sm-12">
                    <div class="text-left padding-zero col-sm-1">  <label >Status  :  &nbsp;&nbsp;</label></div>
                    <div class="radio radio-success col-sm-11" style="margin-top: -3px" >

                        <input type="radio"
                               data-errormessage-value-missing="Please Select Course Status" 
                               class="validate[required]" 
                               name="topic_status" 
                               id="published" 
                               value="1"
                               onchange="check_topic_status();">
                        <label for="published">Published</label>
                        <span style="margin-right: 30px"></span>

                        <?php if (!$topic_quizes) { ?>
                            <input type="radio" 
                                   data-errormessage-value-missing="Please Select Course Status" 
                                   class="validate[required]" 
                                   name="topic_status" 
                                   id="unpublished" 
                                   value="0"
                                   onchange="check_topic_status();">
                            <label for="unpublished">unpublished</label>
                            <span style="margin-right: 30px"></span>

                            <input type="radio" 
                                   data-errormessage-value-missing="Please Select Course Status" 
                                   class="validate[required]" 
                                   name="topic_status" 
                                   id="pending" 
                                   value="3"
                                   onchange="check_topic_status();">
                            <label for="pending">Pending</label>
                            <span style="margin-right: 30px"></span>
                        <?php } ?>

                        <input type="radio" 
                               data-errormessage-value-missing="Please Select Course Status" 
                               class="validate[required]" 
                               name="topic_status" 
                               id="approved" 
                               value="4"
                               onchange="check_topic_status();">
                        <label for="approved">Approved</label>
                        <span style="margin-right: 30px"></span>

                    </div>
                    <div id="topic_status_message" style="color:red">
                        <?php echo form_error('topic_status'); ?>
                    </div>
                </div>

                <div class="col-sm-12"><br></div>
                <div class="col-sm-3"></div>
                <div class="form-group col-sm-3">
                    <input type="submit" class="green-btn btn btn-default" value="Update Topic"><br/><br/>
                </div>
                <div class="col-sm-3"></div>
                <?php echo form_close(); ?>
            </div>
        </div>
    </div>
    <br>
    <div class="row gray">
        <div class="col-sm-12">
            <label>
                <a href="<?php echo base_url() ?>super_admin">Dashboard </a>
                ->><a href="<?php echo base_url() . 'super-admin/manage-topic' ?>">Manage topic </a>
                ->> Edit topic 
            </label>
        </div>
    </div>
</div>

<script>
    //START url processing
    var urlValue = '';
    function checkUrlValue(givenText, urlInput) {
        urlValue = urlInput;
        if (urlValue === '') {
            makeUrl(givenText);
        } else {
            checkUrl(urlInput);
        }
    }

    function makeUrl(profileID) {
        var name = profileID;
        if (profileID == '') {
            _('topic_url').value = '';
        } else {
            profileID = profileID.replace(/  +/g, '');
            for (i = 0; profileID.length > i; i++) {
                if (profileID.charAt(i) === ' ') {
                    profileID = profileID.replace(/ +/g, '-');
                }
                if (profileID.charAt(i) === '.') {
                    profileID = profileID.replace(/\./g, '');
                }
                if (profileID.charAt(i) === '-') {
                    profileID = profileID.replace(/--/g, '-');
                }
            }

            profileID = profileID.replace(/[&\/\\#@!^_=,+()$~%.'":*?<>{}]/g, '');
            if (profileID.charAt(profileID.length - 1) === '-') {
                profileID = setCharAt(profileID, profileID.length - 1, '');
            }
            profileID = profileID.toLowerCase();
            _('topic_url').value = profileID;
            checkName(name);
            checkUrl(profileID);
        }
    }

    var topicNameStatus;
    function checkName(name) {
        if (name != '') {
            var cn_xmlhttp = create_xmlhttp();
            var formdata = new FormData();
            formdata.append("topic_name", name);
            cn_xmlhttp.addEventListener("load", function (event) {
                topicNameStatus = cn_xmlhttp.responseText;                
            }, false);
            cn_xmlhttp.open("POST", base_url + 'super-admin/check-topic-name-existance');
            cn_xmlhttp.send(formdata);
        }
    }

    function checkUrl(given_text) {
        var old_url = '<?php echo $topic_info->url; ?>';
        var xmlhttp2 = create_xmlhttp();
        if (given_text == old_url) {            
        } else if (given_text != '') {
            var serverPage = base_url + 'super-admin/check-topic-url-existance/' + given_text;
            xmlhttp2.open("GET", serverPage);
            xmlhttp2.onreadystatechange = function ()
            {
                if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200)
                {
                    _('url_exist').value = xmlhttp2.responseText;
                }
            };
            xmlhttp2.send(null);
        }
    }
    //END url processing

    // JQuery and PHP validation message manage    
</script>


<script>
    document.forms['add_topic_form'].elements['topic_status'].value = '<?php
                if (set_value('topic_status')) {
                    echo set_value('topic_status');
                } else {
                    echo $topic_info->topic_status;
                }
                ?>';
</script>