$(document).ready(function() {
    checkSessionStat();
    check_client_id();
});
$(function () {
    $('#input-document_date').datetimepicker({
        format: 'DD-MM-YYYY',
    });
});
function check_client_id(){
    var client_id = $("#input-cid").val();
    if(client_id != ""){
        toggleDisableClientInput("disable");
        $("#group-cname").addClass("input-group");
        $("#cname-refresh").show();
    } else {
        toggleDisableClientInput("enable");
        $("#group-cname").removeClass("input-group");
        $("#cname-refresh").hide();
    }
}
function checkSessionStat(){
    var status = sessionStorage.getItem("alert_status");
    var message = sessionStorage.getItem("alert_message");
    if(status == "success"){
        successAlert(message);
        sessionStorage.removeItem("alert_status");
        sessionStorage.removeItem("alert_message");
    } else if(status == "failed"){
        errorAlert(message);
        sessionStorage.removeItem("alert_status");
        sessionStorage.removeItem("alert_message");
    }
}
function validateFormItem(){
    var adesc = $("#input-adesc").val();
    var aproduct = $("#input-aproduct").val();
    var aqty = $("#input-aqty").val();
    var aunit = $("#input-aunit").val();
    var aprice = $("#input-aprice").val();

    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var numFormat = /^\+?[\d+ ]*$/;
    var urlFormat = /^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/;

    if(adesc != ""){
        $("#error-adesc").html("");
        $("#error-adesc").hide();
    } else {
        $("#error-adesc").show();
        $("#error-adesc").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-adesc").focus();
        return false;
    }
    if(aproduct != ""){
        $("#error-aproduct").html("");
        $("#error-aproduct").hide();
    } else {
        $("#error-aproduct").show();
        $("#error-aproduct").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-aproduct").focus();
        return false;
    }
    if(aqty != ""){
        if(aqty > 0){
            $("#error-aqty").html("");
            $("#error-aqty").hide();
        } else {
            $("#error-aqty").show();
            $("#error-aqty").html("<i class='fa fa-warning'></i> This field must more than 0.");
            $("#input-aqty").focus();
            return false;
        }
    } else {
        $("#error-aqty").show();
        $("#error-aqty").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-aqty").focus();
        return false;
    }
    if(aunit != ""){
        $("#error-aunit").html("");
        $("#error-aunit").hide();
    } else {
        $("#error-aunit").show();
        $("#error-aunit").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-aunit").focus();
        return false;
    }
    if(aprice != ""){
        if(aprice > 0){
            $("#error-aprice").html("");
            $("#error-aprice").hide();
        } else {
            $("#error-aprice").show();
            $("#error-aprice").html("<i class='fa fa-warning'></i> This field must more than 0.");
            $("#input-aprice").focus();
            return false;
        }
    } else {
        $("#error-aprice").show();
        $("#error-aprice").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-aprice").focus();
        return false;
    }
    return true;
}
function confirmRemove(atpoid){
    var url_page = $("#input-pageurl").val();
    var poid = $("#input-poid").val();
    var poppn = $("#input-poppn").val();

    var redir_url = url_page+"?action=delete&poid="+poid+"&atpoid="+atpoid+"&poppn="+poppn;
    swal({
        title: "Are you sure?",
        text: "You will remove this item from list!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Remove ! ",
        cancelButtonText: "Cancel !",
        closeOnConfirm: true,
        closeOnCancel: true
    },
    function (isConfirm) {
        if (isConfirm) {
            window.location.href = redir_url;
        } else {
            //nothing
        }
    });
}
function showEditData(param_id){
    $('#modalEdit').modal('show');
    var edesc = $("#hdesc-"+param_id).val();
    var eproduct = $("#hproduct-"+param_id).val();
    var eqty = $("#hqty-"+param_id).val();
    var eunit = $("#hunitid-"+param_id).val();
    var eunittxt = $("#hunit-"+param_id).val();
    var eprice = $("#hprice-"+param_id).val();

    $("#input-edesc").val(edesc);
    $("#input-eproduct").val(eproduct);
    $("#input-eqty").val(eqty);
    $("#input-eunit").val(eunit);
    $("#input-eunittxt").val(eunittxt);
    $("#input-eprice").val(eprice);
    $("#input-erow").val(param_id);
}
function validateFormItemEdit(){
    var edesc = $("#input-edesc").val();
    var eproduct = $("#input-eproduct").val();
    var eqty = $("#input-eqty").val();
    var eunit = $("#input-eunit").val();
    var eprice = $("#input-eprice").val();

    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var numFormat = /^\+?[\d+ ]*$/;
    var urlFormat = /^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/;

    if(edesc != ""){
        $("#error-edesc").html("");
        $("#error-edesc").hide();
    } else {
        $("#error-edesc").show();
        $("#error-edesc").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-edesc").focus();
        return false;
    }
    if(eproduct != ""){
        $("#error-eproduct").html("");
        $("#error-eproduct").hide();
    } else {
        $("#error-eproduct").show();
        $("#error-eproduct").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-eproduct").focus();
        return false;
    }
    if(eqty != ""){
        if(eqty > 0){
            $("#error-eqty").html("");
            $("#error-eqty").hide();
        } else {
            $("#error-eqty").show();
            $("#error-eqty").html("<i class='fa fa-warning'></i> This field must more than 0.");
            $("#input-eqty").focus();
            return false;
        }
    } else {
        $("#error-eqty").show();
        $("#error-eqty").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-eqty").focus();
        return false;
    }
    if(eunit != ""){
        $("#error-eunit").html("");
        $("#error-eunit").hide();
    } else {
        $("#error-eunit").show();
        $("#error-eunit").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-eunit").focus();
        return false;
    }
    if(eprice != ""){
        if(eprice > 0){
            $("#error-eprice").html("");
            $("#error-eprice").hide();
        } else {
            $("#error-eprice").show();
            $("#error-eprice").html("<i class='fa fa-warning'></i> This field must more than 0.");
            $("#input-eprice").focus();
            return false;
        }
    } else {
        $("#error-eprice").show();
        $("#error-eprice").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-eprice").focus();
        return false;
    }
    return true;
}
$("#input-cname").keyup(function(){
    get_client();
});
function get_client(){
    var url = api_client_select;
    var session_id = $("#session-id").val();
    var cname = $("#input-cname").val();
    var data = {
        user_id : session_id,
        keyword : cname
    }
    $.ajax({type: "POST",url: url,data : data, success:function(result){
        var code = result.code;
        if(code == 200){
            var datas = result.data;
            $('#list-cname').html(show_address(datas));
            $("#view-select").show();
        } else {
            $("#view-select").hide();
        }
    }});  
};
function show_address(datas){
    var resultHTML="";
    if(datas != null){
        var obj = datas;
        var num = 1;
        for(var i=0;i < obj.length;i++){
            resultHTML += "<li class='address-li' onclick='getDetailClient(&apos;"+obj[i].client_id+"&apos;,&apos;"+obj[i].client_name+"&apos;)'>";
            resultHTML += "<div class='adr-name'>"+obj[i].client_name+"</div>";
            resultHTML += "<div class='adr-desc'>";
            resultHTML += obj[i].client_email;
            resultHTML += "</div>";
            resultHTML += "</li>";
            num++;
        }
    }
    return resultHTML;
}
$(document).mouseup(function(e) 
{
    var container = $("#view-select");
    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
        container.hide();
    }
});
function getDetailClient(param,client_name){

    $("#input-cname").val(client_name);
    $("#view-select").hide();

    var url = api_client_detail;
    var session_id = $("#session-id").val();
    var cname = $("#input-cname").val();
    var data = {
        user_id : session_id,
        client_id : param
    }
    $.ajax({type: "POST",url: url,data : data, success:function(result){
        var code = result.code;
        if(code == 200){
            var datas = result.data;

            $("#input-cid").val(param);
            $("#input-cemail").val(datas[0].client_email);
            $("#input-cphone").val(datas[0].client_phone);
            $("#input-caddress").val(datas[0].client_address);
            toggleDisableClientInput("disable");
            $("#group-cname").addClass("input-group");
            $("#cname-refresh").show();
        } else {

            $("#input-cid").val("");
            $("#input-cemail").val("");
            $("#input-cphone").val("");
            $("#input-caddress").val("");
            toggleDisableClientInput("enable");
            $("#group-cname").removeClass("input-group");
            $("#cname-refresh").hide();

        }
    }});
    
};
function toggleDisableClientInput(param){
    if(param == "disable"){
        $(".client-input").addClass("disable-input");
    } else {
        $(".client-input").removeClass("disable-input");
    }
}
$("#btn-refresh-client").click(function(){
    $("#input-cname").val("");
    $("#input-cid").val("");
    $("#input-cemail").val("");
    $("#input-cphone").val("");
    $("#input-caddress").val("");
    toggleDisableClientInput("enable");
    $("#group-cname").removeClass("input-group");
    $("#cname-refresh").hide();
});
function validateFormSave(){
    var ponumber = $("#input-ponumber").val();
    var region = $("#input-region").val();
    var pic = $("#input-pic").val();
    var document_date = $("#input-document_date").val();
    var cname = $("#input-cname").val();
    var cemail = $("#input-cemail").val();
    var cphone = $("#input-cphone").val();
    var csub = $("#input-csub").val();
    var caddress = $("#input-caddress").val();
    var posubtotal = $("#posubtotal").val();
    var poppn_price = $("#poppn-price").val();
    var pototal = $("#pototal").val();
    var total_item = $("#total-list").val();

    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var numFormat = /^\+?[\d+ ]*$/;
    var urlFormat = /^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/;

    if(ponumber != ""){
        $("#error-ponumber").html("");
        $("#error-ponumber").hide();
    } else {
        $("#error-ponumber").show();
        $("#error-ponumber").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-ponumber").focus();
        return false;
    }
    if(region != ""){
        $("#error-region").html("");
        $("#error-region").hide();
    } else {
        $("#error-region").show();
        $("#error-region").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-region").focus();
        return false;
    }
    if(pic != ""){
        $("#error-pic").html("");
        $("#error-pic").hide();
    } else {
        $("#error-pic").show();
        $("#error-pic").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-pic").focus();
        return false;
    }
    if(document_date != ""){
        $("#error-document_date").html("");
        $("#error-document_date").hide();
    } else {
        $("#error-document_date").show();
        $("#error-document_date").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-document_date").focus();
        return false;
    }
    if(cname != ""){
        $("#error-cname").html("");
        $("#error-cname").hide();
    } else {
        $("#error-cname").show();
        $("#error-cname").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-cname").focus();
        return false;
    }
    if(cemail != ""){
        if(cemail.match(mailformat)){
            $("#error-cemail").html("");
            $("#error-cemail").hide();
            $("#input-cemail").removeClass("input-error");
        } else {
            $("#error-cemail").show();
            $("#error-cemail").html("<i class='fa fa-warning'></i> Incorrect email format.");
            $("#input-cemail").addClass("input-error");
            $("#input-cemail").focus();
            return false;
        }
    } else {
        $("#error-cemail").show();
        $("#error-cemail").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-cemail").addClass("input-error");
        $("#input-cemail").focus();
        return false;
    }
    if(cphone != ""){
        if(cphone.match(numFormat)){
            $("#error-cphone").html("");
            $("#error-cphone").hide();
            $("#input-cphone").removeClass("input-error");
        } else {
            $("#error-cphone").show();
            $("#error-cphone").html("<i class='fa fa-warning'></i> Please insert only number.");
            $("#input-cphone").addClass("input-error");
            $("#input-cphone").focus();
            return false;
        }
    } else {
        $("#error-cphone").show();
        $("#error-cphone").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-cphone").addClass("input-error");
        $("#input-cphone").focus();
        return false;
    }
    if(csub != ""){
        $("#error-csub").html("");
        $("#error-csub").hide();
    } else {
        $("#error-csub").show();
        $("#error-csub").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-csub").focus();
        return false;
    }
    if(caddress != ""){
        $("#error-caddress").html("");
        $("#error-caddress").hide();
    } else {
        $("#error-caddress").show();
        $("#error-caddress").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-caddress").focus();
        return false;
    }
    if(total_item > 0){
        //nothing
    } else {
        errorAlert("Please insert at least 1 item");
        return false;
    }
    return true;
}
$('#input-eunit').on('change', function() {
    var eunit = $("#input-eunit").val();
    var eunit_txt = $("#input-eunit option:selected").text();

    $("#input-eunittxt").val(eunit_txt);
});
$('#input-aunit').on('change', function() {
    var aunit = $("#input-aunit").val();
    var aunit_txt = $("#input-aunit option:selected").text();

    $("#input-aunittxt").val(aunit_txt);
});