<script src=" <?php echo THEME_PATH; ?>/plugins/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<script src="<?php echo THEME_PATH; ?>/plugins/jquery-dynatree/jquery.dynatree.min.js" type="text/javascript"></script>
<link href="<?php echo THEME_PATH; ?>/plugins/jquery-dynatree/skin/ui.dynatree.css" rel="stylesheet" type="text/css" media="screen"/>
<style>
    ul.dynatree-container {
        overflow: visible !important;
    }
    .alert-dismissible{
        margin-top:20px;
        margin-left:58px;
        border: 1px dashed #4c0870;
        background-color: #fff;
        color: #4E5E6A;
        font-size:16px;
        width: 50%;
    }
    .TreeItem{
        display:inline-block;
        cursor:default();
    }
    .TreeItem > .ActionButton{
        position:relative;
        top:1px;
        cursor:pointer;
        margin:0 5px;
        opacity:0;
        font-size:18px;
        visibility:hidden;
        -webkit-transition:opacity 0.2s linear;
        -moz-transition:opacity 0.2s linear;
        transition:opacity 0.2s linear;
    }
    .TreeItem:hover > .ActionButton{
        opacity:1;
        visibility:visible;
    }
    .dynatree-exp-cd span.dynatree-expander,.dynatree-exp-cdl span.dynatree-expander {
        background-position: 0 !important ;
        margin-top:-4px;
    }
    .CenterHAndV{
        width:100%;
        height:100%;
        text-align:center;
        font-size: 18px;
    }
    #PrimaryCats .fa-circle-o{
        color: #4c0870;
        position: absolute;
        right: 10px;
        top: 10px;
        cursor:pointer;
    }
    #PrimaryCats .mainPageLinks{
        display: block;
        height: 100%;
    }
    #PrimaryCats .CenterHAndV .MiddleIcon{
        font-size: 45px;
        display: block;
        margin: 30px 0;
    }
    #AddNewItemDiv,#RemoveAddedItemDiv,#AddNewItemElement,#EditItemDiv{
        border: 1px dashed rgba(0, 0, 0, 0.25);
        padding: 10px;
        -webkit-border-radius: 2px;
        -moz-border-radius: 2px;
        border-radius: 2px;
    }
    .tool-tip{
        color: #fff;
        background-color: rgba( 0, 0, 0, .7);
        text-shadow: none;
        font-size: .8em;
        visibility: hidden;
        -webkit-border-radius: 7px; 
        -moz-border-radius: 7px; 
        -o-border-radius: 7px; 
        border-radius: 7px;	
        text-align: center;	
        opacity: 0;
        z-index: 999;
        padding: 3px 8px;	
        position: absolute;
        cursor: default;
        -webkit-transition: all 240ms ease-in-out;
        -moz-transition: all 240ms ease-in-out;
        -ms-transition: all 240ms ease-in-out;
        -o-transition: all 240ms ease-in-out;
        transition: all 240ms ease-in-out;	
    }
    .tool-tip,
    .tool-tip.top{
        top: auto;
        bottom: 114%;
        left: 50%;		
    }
    .tool-tip.top:after,
    .tool-tip:after{
        position: absolute;
        bottom: -12px;
        left: 50%;
        margin-left: -7px;
        content: ' ';
        height: 0px;
        width: 0px;
        border: 6px solid transparent;
        border-top-color: rgba( 0, 0, 0, .7);	
    }
    /* default heights, width and margin w/o Javscript */
    .tool-tip,
    .tool-tip.top{
        z-index:9999;
        width: 150px;
        height: 22px;
        margin-left: -72px;
    }
    /* on hover of element containing tooltip default*/
    *:not(.on-focus):hover > .tool-tip,
    .on-focus input:focus + .tool-tip{
        visibility: visible;
        opacity: 1;
        -webkit-transition: all 240ms ease-in-out;
        -moz-transition: all 240ms ease-in-out;
        -ms-transition: all 240ms ease-in-out;
        -o-transition: all 240ms ease-in-out;
        transition: all 240ms ease-in-out;		
    }
</style>
<div class="wrapper">
    <div class="panel">
        <div class="panel-body">
            <div class="row">
                <div class="col-md-3 form-group  main_machine_div">
                    <label><?php echo CommonLanguage::getMessage('machine') ?></label>
                    <input type="hidden" name="machine_id" class="full-width" id="machine_id"/>
                </div>
                <div class="col-md-6 pull-right m-t-20">
                    <div class="col-md-3 pull-right">
                        <button style="color:white !important;" id="new_machine_part_btn" type="button" class="btn bg-success-dark text-white pull-right"><i class="fa fa-plus"></i> 
                            <span><?php print CommonLanguage::getMessage('new_machine_part'); ?></span>
                        </button>
                    </div>

                    <div class="col-md-3  hidden machine-part-div pull-right">
                        <button id="new_machine_part_link_btn" data-toggle="modal" data-target="#link-machine-part-modal" type="button" class="btn bg-success-dark text-white pull-right" style="margin-right:33px;"><i class="fa fa-link"></i> 
                            <span> <?php print CommonLanguage::getMessage('link_machine_part'); ?></span>
                        </button>
                    </div>

                </div>
            </div>
            <div class="row">
                <div class ="col-md-12 alert alert-info alert-dismissible">
                    <?php print CommonLanguage::getMessage('select_machine'); ?>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <h6><b class="main_tree_span"></b></h6>
                    <div id="check-tree">
                    </div>
                    <div id='AjaxLoader' class="hidden" style='height:100px;text-align:center'><img src='img/demo/progress.svg' /></div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="new-machine-part-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width:50%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('new_machine_part'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_machine_part'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="new_machine_part_form" method="POST" onsubmit="return false">
                    <input name="current_entry_id" id="current_entry_id" type="hidden"/>
                    <input name="current_product_id" id="current_product_id" type="hidden"/>
                    <input name="current_added_id" id="current_added_id" type="hidden"/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('new_machine_part_alert'); ?>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-6">
                            <label><?php print CommonLanguage::getMessage('Code'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('Code'); ?>" id="Product_Code" name="Product_Code" autocomplete="off"  class="form-control textinput" value="<?php echo smc_session()['auto_generate_code'] == 1 ? $max_code : '' ?>" required>
                        </div>
                        <div class="col-md-6">
                            <label><?php print CommonLanguage::getMessage('Name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('Name'); ?>" id="Product_Name" name="Product_Name" autocomplete="off" class="form-control textinput" required>
                        </div>
                    </div> 
                    <div class="row form-group">
                        <div class="col-md-6 ">
                            <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                            <span class="red-star">*</span>
                            <input required id="Category_ID" name="Category_ID" class="full-width Category_ID" type="hidden" />
                        </div>
                        <div class="col-md-6">
                            <label><?php print CommonLanguage::getMessage('UOM'); ?></label>
                            <span class="red-star">*</span>
                            <input required id="UOM_ID" name="UOM_ID" class="full-width UOM_ID" type="hidden" />
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-6 ">
                            <label><?php print CommonLanguage::getMessage('section'); ?></label>
                            <span class="red-star">*</span>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/product_sections?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span>
                            <input required type="hidden" id="section_id" name="section_id" class="full-width"/>
                        </div>
                        <div class="col-md-6">
                            <label><?php print CommonLanguage::getMessage('type'); ?></label>
                            <select id="machine_type" name="machine_type" class="full-width " data-init-plugin="select2" required>
                                <option value="0"><?php print CommonLanguage::getMessage('select'); ?></option>
                                <option value="1"><?php echo CommonLanguage::getMessage('machine'); ?></option>
                                <option value="2"><?php echo CommonLanguage::getMessage('parts'); ?></option>
                            </select>
                        </div>
                    </div> 
                    <div class="row form-group">
                        <div class="col-md-6 ">
                            <label><?php print CommonLanguage::getMessage('note'); ?></label>
                            <input type="text" id="part_note" name="part_note" class="form-control"/>
                        </div>
                        <div class="col-md-6 properties_divs form-group hide-close hidden">
                            <label><?php print CommonLanguage::getMessage('properties'); ?></label>
                            <div class="row  add-row form-group " >
                                <div class="col-md-12 " >
                                    <div class="col-md-9"  >
                                        <input type="text" name="property_name"  class="full-width property_name form-control" >
                                    </div>
                                    <div class="col-md-3 buttons">
                                        <span class="fa fa-plus-circle" style="color:#4c0870;margin-top:15px;margin-right:25px;cursor:pointer;"></span>
                                        <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;cursor:pointer;"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-success-dark text-white" id="submit_new_machine_part"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('NewMachinePartLoader'); ?>
        </div>
    </div>
</div>
<div class="modal fade" id="link-machine-part-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="text-left bold"><?php echo CommonLanguage::getMessage('link_machine_part'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="link_machine_part_form" method="POST" onsubmit="return false">
                    <input name="current_link_id" id="current_link_id" type="hidden"/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('link_machine_part_alert'); ?>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('machines_parts'); ?></label>
                            <span class="red-star">*</span>
                            <input type="hidden" id="machine_part_id" name="machine_part_id" class="full-width" value="" required>
                        </div>
                    </div> 
                    <div class="row form-group machine-note">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('note'); ?></label>
                            <input type="text" id="current_part_note" name="part_note" class="form-control" value="">
                        </div>
                    </div> 
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-success-dark text-white" id="submit_link_machine_part"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('linkMachinePartLoader'); ?>
        </div>
    </div>
</div>
<div class="modal fade" id="unlink-delete-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header text-left ">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="unlink_machine_part h-labels bold hidden"><?php echo CommonLanguage::getMessage('unlink_machine_part'); ?></h5>
                <h5 class="delete_machine_part h-labels bold hidden"><?php echo CommonLanguage::getMessage('delete_machine_part'); ?></h5>
            </div>
            <div class="modal-body">
                <input type="hidden" name="unlink_delete_type_id" id="unlink_delete_type_id"/>
                <input name="current_unlink_delete_id" id="current_unlink_delete_id" type="hidden"/>
                <input name="current_machine_part_id" id="current_machine_part_id" type="hidden"/>
                <div class="row">
                    <?php echo Forms::AlertDiv('unlink_delete_alert'); ?>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <p class="unlink_machine_part hidden"><?php echo CommonLanguage::getMessage('about_unlink_machine_part'); ?></p>
                        <p class="delete_machine_part hidden"><?php echo CommonLanguage::getMessage('about_delete_machine_part'); ?></p>
                        <p><?php echo CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class=" btn btn-danger danger widthauto" id="submit_unlink_delete"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('unlinkDeleteLoader'); ?>
        </div>
    </div>
</div>
<script>
    var ExpandAfterReloading = "",check_uom =<?php echo json_encode($check_uom);?>,active_row;
    $(function(){
        buildDynatree();
        build_select2($("#section_id"), "xhr/CommonSearch?col_name=section_name&col_id=section_id&table=tbl_product_sections");
        build_select2($(".UOM_ID"), "xhr/uomSearch");
        build_select2($(".Category_ID"), "xhr/catSearch");
        build_select2($("input[name=machine_id]"),"xhr/MainMachineSearch?type=1&Status_ID=1");
        //        $('#machine_part_id').on('change',function(){
        //            CommonViewById('',{table:'tbl_stock_products',entry_id:'Product_ID',entry_value:$(this).val()},
        //                           '#linkMachinePartLoader',function(response){
        //                if(response[0]['machine_type_id'] == 1){
        //                    $('.machine-note').removeClass('hidden')
        //                }else{
        //                    $('.machine-note').addClass('hidden')
        //                    $('#current_part_note').val('')
        //                }
        //            })
        //        });
        $("input[name=machine_id]").on('change',function(){
            if(!$(this).val()){
                $('.alert-dismissible').removeClass('hidden')
                $('#check-tree,.machine-part-div,.main_tree_span').addClass('hidden')
            }else{
                $('.alert-dismissible').addClass('hidden')
                $('#check-tree,.machine-part-div,.main_tree_span').removeClass('hidden')
                getData();
            } 
        });
        $("#submit_new_machine_part").on("click", function () {
            if (!$("#new_machine_part_form").valid())
                return;
            var Data = new FormData(document.getElementById("new_machine_part_form")),prop_arr= [],prop_ids=[];
            $(".add-row").each(function () {
                if ($(this).find(".property_name").val() != '') {
                    prop_arr.push({
                        property_name: $(this).find(".property_name").val(),
                        property_id: $(this).find(".property_name").attr('data-id')
                    })
                    prop_ids.push($(this).find(".property_name").attr('data-id'))
                }
            })
            Data.append('machine_type_id',$('#machine_type').val());
            Data.append("prop_arr", JSON.stringify(prop_arr))
            Data.append("prop_ids", JSON.stringify(prop_ids))
            $("#NewMachinePartLoader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitNewMachinePart",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            hideModalAlert('new-machine-part-modal', 'new_machine_part_alert');
                            if($('#current_entry_id').val() != 0){
                                if($('#current_product_id').val() == 0 ){
                                    if($('#machine_id').val()){
                                        if($('#current_added_id').val() == $('#machine_id').val())
                                            getData()
                                        else
                                            refreshCurrentNode($('#current_added_id').val())
                                    }
                                }else{
                                    active_row.find('span.product_name').html($('#Product_Name').val())
                                    active_row.find('span.product_note').html($('#part_note').val())
                                }
                            }else{
                                var content = $('#Product_Name').val()+' - '+$('#Product_Code').val()+' - '+$('#part_note').val();
                                $('.machine_title').html(content)
                                $('.main_machine_div .select2-chosen').html(content)

                            }
                        }
                        AddAlert("#new_machine_part_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#NewMachinePartLoader").addClass('hidden');
                }
            });
        });
        $("#submit_link_machine_part").on("click", function () {
            if (!$("#link_machine_part_form").valid())
                return;
            var Data = new FormData(document.getElementById("link_machine_part_form"));
            $("#linkMachinePartLoader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitLinkMachinePart",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            if($('#current_link_id').val() == $('#machine_id').val())
                                getData()
                            else
                                refreshCurrentNode($('#current_link_id').val())
                            hideModalAlert('link-machine-part-modal', 'link_machine_part_alert');
                        }
                        AddAlert("#link_machine_part_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#linkMachinePartLoader").addClass('hidden');
                }
            });
        });
        $("#submit_unlink_delete").on("click", function () {
            $("#unlinkDeleteLoader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitUnlinkDeleteMachPart",
                method: "POST",
                data: {type: $('#unlink_delete_type_id').val(),auto_id:$('#current_unlink_delete_id').val(), machine_part_id: $('#current_machine_part_id').val()},
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            if($('#current_unlink_delete_id').val() ==0){
                                $('.alert-dismissible').removeClass('hidden')
                                $('#check-tree,.machine-part-div,.main_tree_span').addClass('hidden')
                                $('#machine_id').val('').trigger('change')
                            }else{
                                $("#check-tree").dynatree("getActiveNode").remove()
                            }
                            hideModalAlert('unlink-delete-modal', 'unlink_delete_alert');
                        }
                        AddAlert("#unlink_delete_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#unlinkDeleteLoader").addClass('hidden');
                }
            });
        });
        $('#new_machine_part_btn').on('click',function(){
            getProductNewCode()
            if(!$('#machine_id').val()){
                $('#current_added_id').val(0); 
                $('#machine_type').val(1).select2();
                $('#machine_type').attr('disabled',true);
                $('.properties_divs').removeClass('hidden')
            }else{
                $('#machine_type').val(2).select2();
                $('#current_added_id').val($('#machine_id').val());
                $('#machine_type').attr('disabled',false);
            }
        });
        $('#machine_type').on('click',function(){
            $(this).val() == 1 ? $('.properties_divs').removeClass('hidden') : $('.properties_divs').addClass('hidden');
        });
        $(".properties_divs").on("click", " .fa-plus-circle", function (e) {
            $(".add-row  .fa-minus-circle").css("display", "inline-block")
            var Parent = $(this).parents(".add-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find(".property_name").val('');
            NewItem.find(".property_name").attr('data-id',0);
        });
        $(".properties_divs").on("click", ".fa-minus-circle", function (e) {
            if ($(".add-row").length == 1) {
                $(".property_name").val("")
            } else {
                $(this).closest(".add-row").empty().remove();
            }
        });
        $('#new_machine_part_link_btn').on('click',function(){
            $('#current_link_id').val($('#machine_id').val());
            build_select2($("input[name=machine_part_id]"),"xhr/MachinePartSearch?type=all&Status_ID=1&id="+$('#machine_id').val());
        });
    });
    function getData(){
        $('.main_tree_span').html('<span class="machine_title">'+$('#machine_id').select2('data')['text']+'</span>   <span onclick="EditMachinePart(this,1)" class="ActionButton fa fa-pencil"></span>&nbsp;&nbsp;<span onclick="DeleteItem(this,1)" class="ActionButton fa fa-trash"></span>')
        $('#AjaxLoader').removeClass('hidden')
        PageTreeObject.options.initAjax.url = "InventoryAjax/ajaxProductDetails?product_id="+$('#machine_id').val()+"&cmd=fetch&id=" + 1
        PageTreeObject.reload(function () {
            $("#AjaxLoader").remove();
            $("#check-tree").css("visibility", "visible");
            try {
                if (PageTreeObject.count() == 0) {
                    $("#EmptyTreeMsg").show()
                } else {
                    $("#EmptyTreeMsg").hide()
                }
            } catch (err) {
            }
            $('#AjaxLoader').addClass('hidden')
        });
    }
    function buildDynatree(){
        $("#check-tree").dynatree({
            title: "",
            selectMode: 3,
            keyboard: false,
            fx: {height: "toggle", duration: 200},
            autoFocus: false,
            initAjax: {},
            onCreate: function (dtnode, nodeSpan) {
                if (dtnode.data.DontShow == "yes")
                    dtnode.li.style.display = "none";
            },
            onCustomRender: function (node) {
                return "<div class='TreeItem' data-id='"+node.data.part_id+"'  MyId='" + node.data.part_product_id + "' MyParent='" + node.data.Product_ID + "' > " + "<span class='product_name'>"+node.data.Product_Name +"</span>"+(node.data.part_note ? "  ("+"<span class='product_note'>"+node.data.part_note+"</span>"+")" : "")+(node.data.machine_type_id == 2 ? "" : "<span onclick='NewMachinePart(this)' class='ActionButton fa fa-plus'><div class='tool-tip slideIn top'><?php echo CommonLanguage::getMessage('new_machine_part'); ?></div></span><span onclick='AddLink(this)' class='ActionButton fa fa-link'><div class='tool-tip slideIn top'><?php echo CommonLanguage::getMessage('link_machine_part'); ?></div></span><span onclick='unLink(this)' class='ActionButton fa fa-unlink'><div class='tool-tip slideIn top'><?php echo CommonLanguage::getMessage('unlink_machine_part'); ?></div></span>")+"<span onclick='DeleteItem(this,0)' class='ActionButton fa fa-trash'><div class='tool-tip slideIn top'><?php echo CommonLanguage::getMessage('delete_machine_part'); ?></div></span><span onclick='EditMachinePart(this,0)' class='ActionButton fa fa-pencil'><div class='tool-tip slideIn top'><?php echo CommonLanguage::getMessage('Edit'); ?></div></span></div>";
            },
            onRender: function (node, nodeSpan) {
                node.select()
                $(nodeSpan).find(".TreeItem").addClass("OriginallyChecked")
            },
            onLazyRead: function (node) {
                node.appendAjax({
                    url: "InventoryAjax/ajaxProductDetails?product_id="+node.data.part_product_id+"&cmd=fetch&id=" + node.data.part_product_id,
                    debugLazyDelay: 750
                });
            }
        });
        window.PageTreeObject = $("#check-tree").dynatree("getTree");
    }
    function NewMachinePart(who){
        getProductNewCode()
        $('#machine_type').val(2).select2();
        $('#machine_type').attr('disabled',false);
        $('#current_added_id').val($(who).parent().attr("MyID"))
    } 
    function EditMachinePart(who,type){
        $('#new-machine-part-modal').modal('show');
        if(type == 1){
            $('#current_added_id,#current_product_id').val($('#machine_id').val());
            $('#current_entry_id').val(0);
        }else{
            $('#current_added_id').val($(who).parent().attr("MyID"));
            $('#current_product_id').val($(who).parent().attr("MyID"));
            $('#current_entry_id').val($(who).parent().attr("data-id"));
            active_row = $(who).parent();
        }
        $('#new-machine-part-modal .edit-label').removeClass('hidden');
        CommonViewById('InventoryAjax/getMachinePropertyDetails',{product_id: $('#current_added_id').val(),entry_id: $('#current_entry_id').val()},'#NewMachinePartLoader',function(response){
            var main_response = response['main'];
            $('#Product_Code').val(main_response['Product_Code']);
            $('#Product_Name').val(main_response['Product_Name']);
            $('#Category_ID').val(main_response['Category_ID']);
            $('#Category_ID').attr('data-select-name',main_response['Cat_Name']);
            $('#UOM_ID').val(main_response['UOM_ID']);
            $('#UOM_ID').attr('data-select-name',main_response['UOM_Name']);
            $('#section_id').val(main_response['section_id']);
            $('#section_id').attr('data-select-name',main_response['section_name']);
            build_select2($("#section_id"), "xhr/CommonSearch?col_name=section_name&col_id=section_id&table=tbl_product_sections");
            build_select2($(".UOM_ID"), "xhr/uomSearch");
            build_select2($(".Category_ID"), "xhr/catSearch");
            $('#part_note').val(main_response['part_note']);
            $('#machine_type').val(main_response['machine_type_id']).select2();
            $('#machine_type').attr('disabled',true);
            if(main_response['machine_type_id'] == 1){
                $('.properties_divs').removeClass('hidden');
                var current_row = $(".properties_divs .add-row:eq(0)").clone(),details = response['property'];
                details.length > 0 ? $(".properties_divs .add-row").remove() :  '';
                $.each(details,function(index,value){
                    current_row = current_row == null ? $(".properties_divs .add-row:eq(0)").clone() : current_row;
                    $('input[name=property_name]',current_row).val(details[index].property_name);
                    $('input[name=property_name]',current_row).attr('data-id',details[index].property_id);
                    $(".properties_divs").append(current_row);
                    current_row = null
                }); 
            }else{
                $('.properties_divs').addClass('hidden')
            }
        });
    }
    function AddLink(who){
        $('#link-machine-part-modal').modal('show')
        $('#current_link_id').val($(who).parent().attr("MyID"))
        build_select2($("input[name=machine_part_id]"),"xhr/MachinePartSearch?type=all&Status_ID=1&id="+$(who).parent().attr("MyID"));
    }
    function unLink(who){
        $('#unlink-delete-modal').modal('show')
        $('.unlink_machine_part').removeClass('hidden')
        $('.delete_machine_part').addClass('hidden')
        $('#current_unlink_delete_id').val($(who).parent().attr("data-id"))
        $('#current_machine_part_id').val($(who).parent().attr("MyID"))
        $('#unlink_delete_type_id').val(1)
    }
    function DeleteItem(who,type){
        $('#unlink-delete-modal').modal('show')
        $('.unlink_machine_part').addClass('hidden')
        $('.delete_machine_part').removeClass('hidden')
        if(type == 1){
            $('#current_unlink_delete_id').val(0)
            $('#current_machine_part_id').val($('#machine_id').val()) 
        }else{
            $('#current_unlink_delete_id').val($(who).parent().attr("data-id"))
            $('#current_machine_part_id').val($(who).parent().attr("MyID")) 
        }
        $('#unlink_delete_type_id').val(2)
    }
    function refreshCurrentNode(id){
        ExpandAfterReloading = id;
        var Completed = function () {
            if (ExpandAfterReloading != "") {
                PageTreeObject.getNodeByKey(ExpandAfterReloading).expand()
                ExpandAfterReloading = "";
            }
        }
        PageTreeObject.getNodeByKey(ExpandAfterReloading).appendAjax({
            url: "InventoryAjax/ajaxProductDetails?product_id="+id+"&cmd=fetch",
            debugLazyDelay: 750,
            success: function () {
                Completed();
            },
            error: function () {
                Completed();
            }
        });   
    }
    function getProductNewCode(){
        if(typeof check_uom === 'object'){
            $('#UOM_ID').val(check_uom['UOM_ID']).attr('data-select-name',check_uom['UOM_Name']);
            build_select2($(".UOM_ID"), "xhr/uomSearch");
        }
        $('#new-machine-part-modal .add-label').removeClass('hidden');
        $('#machine_type') == 1 ? $('.properties_divs').removeClass('hidden') : $('.properties_divs').addClass('hidden');
        $('#current_product_id').val(0);
        $('#new-machine-part-modal').modal('show');
        CommonViewById("InventoryAjax/getMaxProductCode",{},"#NewMachinePartLoader",function(response){
            $('#Product_Code').val(response) 
        })
    }
</script>