 $(document).ready(function () {
    bindQueryEvent();
     initialize();
});



 function initialize() {
     /**
      * 如果是详情页，展示数据，隐藏按钮
      * @type {boolean}
      */
     debugger
     let isDetailPage = ($("#isDetailPage").val() === "true");
     if (isDetailPage) {
         $("#jobPageTop").hide();
         if($("#jobPageTop").length>0){
             $("#formButton").hide();
         }
     }
 }



//绑定selectC
function bindQueryEvent() {
    $('#productCode').searchbox({
        type: "product", name: "productDetail",
        onChange: function (value, data) {
            $('#submitForm').formSet(data);
            renderingDictionaries(data);
        }
    });
}


function saveForm(){
    var _self=this;
    $('#submitForm').ajaxSubmit({
        url : web_app.name + '/workInstruction/saveWorkInstruction.ajax',
        param:{},
        success : function(data) {
            debugger
            $("#workInstructionBaseInfoId").val(data.workInstructionBaseInfoId);
            $("#workInstructionMaterialInfoId").val(data.workInstructionMaterialInfoId);
            $("#workInstructionOptionalAttributeId").val(data.workInstructionOptionalAttributeId);
            $("#workInstructionPdPublishedConfId").val(data.workInstructionPdPublishedConfId);
            $("#workInstructionPrdLookedId").val(data.workInstructionPrdLookedId);
            $("#productLossId").val(data.productLossId);
            Public.successTip("保存成功！");
            _self.close();
        }
    });
}


function resetForm(obj) {
    $(obj).formClean();
}


 /**
  * 渲染带数据字典的select
  * @param obj
  */
 function renderingDictionaries(obj) {
     console.log(obj)
     //获取所有select的id名与对象匹配
     $("select").each(function () {
         var id = $(this).attr("id");
         if (obj.hasOwnProperty(id)) {

             var keyValuePairs = new Map();

             $('#' + id + ' option').each(function () {
                 var value = $(this).val();
                 var text = $(this).text();
                 keyValuePairs.set(value, text);
             });

             // 获取 对应的文本值
             var selectedValue = obj[id];
             var selectedText = keyValuePairs.get(selectedValue);

             // 将文本值赋值给目标元素
             $('#' + id + '_text').val(selectedText);
             $('#' + id).val(selectedValue);

             console.log("select:" + id+"("+selectedText+":"+selectedValue+")")
         }
     });
 }