<div class="title">新增產品</div>
<ul class="table-toolbar"style="text-align: left;">
    <li><a class="toolbar-element btn btn-default btn-create" href="/product" ><i style="margin-right: 8px;"><span class="iconify" data-icon="typcn-arrow-back" data-inline="false"></span></i>返回</a></li>
</ul>

<div class="form-container">
    <form id="frmphp" class="form-horizontal" role="form" method="POST" action="/product/insert" enctype="multipart/form-data">
        <div class="form-group mas-col">
            <div class="col-sm-3 text-center mas-title"><label for="productname">產品名稱</label></div>
            <div class="col-sm-9 text-center mas-title mas-box "><input id="productname" name="p_name" type="text" class="form-control mas-input" value="" /></div>
        </div>      

        <div class="form-group mas-col">
            <div class="col-sm-3 text-center mas-title"><label for="originalprice">原本價格(HK$)</label></div>
            <div class="col-sm-9 text-center mas-title mas-box "><input id="originalprice" name="pp_original_price" type="number" class="form-control mas-input" value="" /></div>
        </div>

        <div class="form-group mas-col">
            <div class="col-sm-3 text-center mas-title"><label for="sellingprice">銷售價格(HK$)</label></div>
            <div class="col-sm-9 text-center mas-title mas-box "><input id="sellingprice" name="pp_selling_price" type="number" class="form-control mas-input" value="" /></div>
        </div>     

        <div class="form-group mas-col">
            <div class="col-sm-3 text-center mas-title"><label for="p_product_category_id">產品分類</label></div>
            <div class="col-sm-9 text-center mas-title mas-box ">
                <select class="select mas-select" name="p_product_category_id" id="p_product_category_id">                    
                </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="p_remark" ></textarea></div>
        </div>
        <div class="form-footer">
            <a class="btn btn-footer reset" href="javascript:window.location.reload(true)">重設</a>
            <button class="btn btn-footer submit" type="button" id='btnSubmit'>提交</button>
        </div>
    </form>

</div>

<script>
    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("發生錯誤");
    }

    $(document).ready(function () {
         var li = $('#sidebar-menu li').filter(function(i){
            return $(this).find('.menu-dropdown').find('.menu-text').text()===' 產品';
        });
        li.addClass('active open');
        getRemoteData("/api/product/getcategories", {}, getCategoriesOk, getRemoteDataFail);
    });

    function getCategoriesOk(categories) {
//        console.log('categories:',categories);
        $.each(categories, function (k, v) {
            var o = new Option(v.name, v.id);
            $(o).html(v.name);
            $('#p_product_category_id').append(o);
        });
    }

    function validform() {
        var msg = '';
        if ($('#productname').val() === '') {
            msg += "請輸入產品名稱\n";
        }
        if ($('#originalprice').val() === '') {
            msg += "請輸入原本售價\n";
        }
        if ($('#sellingprice').val() === '') {
            msg += "請輸入銷售價格\n";
        }
        if (msg !== '') {
            alert(msg);
            return false;
        }
        return true;
    }

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