<div class="title">課程主分類詳情</div>
<ul class="table-toolbar"style="text-align: left;">
    <li>
        <a class="toolbar-element btn btn-default btn-create" href="/course_category" >
            <span class="iconify" data-icon="typcn-arrow-back" data-inline="false"></span><i style="margin-right: 8px;"></i>返回
        </a>
    </li>
</ul>
<div class="form-container">
    <form id="frmphp" class="form-horizontal" role="form" method="post" action="/course_category/save">
        <input type="hidden" name="course_category_id" value="<?= $course_category['id']; ?>" />
		<input type="hidden" name="status_value" value="<?= $course_category['status']; ?>" />
		<?php $disabled = (intval($course_category['inuse']) > 0) ? 'disabled' : ''; ?>
		<?php
			if ($disabled !== '') {
				echo '<span class="edit-warning">* 此項目正被使用，不能修改狀態</span>';
			}
		?>
        <div class="form-group mas-col">
            <div class="col-sm-3 text-center mas-title">
                <label for="name">主分類名稱*</label>
            </div>
            <div class="col-sm-9 mas-box">
                <input id=name type="text" class="form-control mas-input" id="name" name="name" placeholder="--- 輸入主分類名稱 ---" value="<?= $course_category['name']; ?>" />
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-3 text-center mas-title"><label for="status">狀態</label></div>
            <div class="col-sm-9 text-center mas-title mas-box ">
                <select  class="select mas-select" name="status" id="status" data-disabled="<?=$disabled?>" <?= $disabled ?>>
                    <?php
                    foreach ($status as $idx => $value) {
                        if ($idx == $course_category['status']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $idx, $selected, $value);
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-3 text-center mas-title">
                <label for="remark">備註</label>
            </div>
            <div class="col-sm-9 mas-box">
                <textarea id="remark"  class="form-control mas-textarea" rows="3" name="remark" ><?= $course_category['remark']; ?></textarea>
            </div>
        </div>
        <div class="form-footer">
            <button class="btn btn-footer submit" type="button" id='btnEdit' style="display:none;">編輯</button>
            <button class="btn btn-footer submit" type="button" id='btnCancel' style="display:none;">取消</button>
            <button class="btn btn-footer submit" type="button" id='btnSubmit'>提交</button>
            <a id="btnReset" class="btn btn-footer reset" href="javascript:window.location.reload(true)">重設</a>            
        </div>
    </form>
</div>
<script>
    var viewMode = true;
    $(document).ready(function () {
        var li = $('#sidebar-menu li').filter(function (i) {
            return $(this).find('.menu-dropdown').find('.menu-text').text() === ' 課程';
        });
        li.addClass('active open');
        changeMode();
    });
    $(document).on('click', '#btnEdit', function () {
        viewMode = false;
        changeMode();
    });
    $(document).on('click', '#btnCancel', function () {
        window.location.href = '/course_category';
    });

    function changeMode() {
        $('.btn-footer').toggle();
        $('.form-control').prop('disabled', viewMode);
        if ($('#status').data('disabled') !== 'disabled') {
			$('#status').prop('disabled', viewMode);
		}
    }

    function validform() {
        var msg = '';
        if ($('#name').val() === '') {
            msg += "輸入主分類名稱\n";
        }
        if (msg !== '') {
            alert(msg);
            return false;
        }
        return true;
    }

    $(document).on('click', '#btnSubmit', function () {
        if (validform()) {
            $('#frmphp').submit();
        }
    });

    $('#status').on('change', function() {
    	$('INPUT[name="status_value"]').val($('#status option:checked').val());
	});
</script>
