<div class="title">發票管理</div>
<form id="form" class="form-horizontal" role="form" method="post" action="/invoice_management/<?= $action ?>">
    <input type="hidden" name="id" value="<?= $id ?>">
    <div class="form-group mas-col">
        <div class="col-sm-3 text-center mas-title" style="color:white;"><label for="toc">發票條款</label></div>
        <div class="col-sm-9 mas-box"><textarea id="toc" name="toc" class="form-control mas-textarea" style="min-height: 40em;"><?=$toc?></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>
        <input class="btn btn-footer submit" type="submit" id='btnSubmit' value="提交">
        <a id="btnReset" class="btn btn-footer reset" href="javascript:window.location.reload(true)">重設</a>           
    </div>
</form>

<script>
    var viewMode = true;
    $(document).ready(function () {
        changeMode();
    });    
    
    function validform(){
        var msg='';
        if($('#toc').val()===''){
            msg+='請輸入發票修款!\n';
        }
        if(msg!=='')alert(msg);
        return msg==='';
    }
    
    $(document).on('click','#btnSubmit',function(e){
        e.preventDefault();
        if(validform()){
            $('#form').submit();
        }
    });

    $(document).on('click', '#btnEdit', function () {
        viewMode = false;
        changeMode();
    });
    $(document).on('click', '#btnCancel', function () {
        window.location.href = '/invoice_management';
    });
    
    function changeMode() {
        $('.btn-footer').toggle();
        $('.form-control').prop('disabled', viewMode);
        $('#toc').prop('disabled', viewMode);
    }
    function getRemoteData(remote_url, data, callback, callbackFail) {
        $.ajax({
            //async: false,
            //timeout: 30000, //最長等候回應時間
            type: "GET", //提交類型
            url: remote_url, //提交地址
            data: data, //提交內容
            dataType: 'json', //返回數據類型
            success: function (data) { //請求完成並成功
                if (data.success) {                   
                        callback(data.data);
                } else {
                    callbackFail();
                }
            },
            error: function (data) { //請求返回錯誤信息
                callbackFail();
            },
            beforeSend: function (XHR) { //请求開始前执行
            },
            complete: function (XHR, status) { //请求完成后最终执行
            }
        });
    }
    function getRemoteDataFail() {
        alert("發生錯誤");
    }
    function getParameterByName(name, url = window.location.href) {
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
                results = regex.exec(url);
        if (!results)
            return null;
        if (!results[2])
            return '';
        return decodeURIComponent(results[2].replace(/\+/g, ' '));
    }
</script>