/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-03-03 17:00
 * 描  述：元件库文件表
 */
var refreshGirdData;
var ProjectId = request('ProjectId');
var LibraryCatalogueID;
var selectData = [];
var ObjectTypeID = request('ObjectTypeID');
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGird();
            page.bind();
        },
        bind: function () {
            // 初始化左侧树形数据
            $('#dataTree').lrtree({
                url: top.$.rootUrl + '/ZZDT_EC/ec_library_catalogue/GetTreeData',
                param: { ProjectId: ProjectId },
                nodeClick: function (item) {
                    LibraryCatalogueID = item.value;
                    $("#titleinfo").html(item.text);
                    page.search(); //选中某个节点，相当于触发这个search，然后去reload右侧
                }
            });

            //获取当前选中的数据
            $.ajax({
                url: top.$.rootUrl + '/ZZDT_EC/ec_objecttypelib/GetObjectTypeLib',
                data: { objectTypeID: ObjectTypeID, ProjectId: ProjectId },
                async: false,
                type: "GET",
                dataType: "json",
                success: function (data) {
                    if (data.code == 200 && data.data.length > 0)
                        selectData = data.data;
                },
                error: function () {
                },
                complete: function (data) {
                }
            });

            //查询
            $('#btn_Search').on('click', function () {
                page.search();
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            // 保存
            $('#lr_save').on('click', function () {
                var postData = {
                    strEntity: JSON.stringify(selectData)
                }
                $.lrSaveForm(top.$.rootUrl + '/ZZDT_EC/ec_objecttypelib/SaveObjectTypeLib?objectTypeID=' + ObjectTypeID + "&ProjectId=" + ProjectId, postData, function (res) {

                });
            });

        },
        // 初始化列表
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_library_file/GetPageList',
                headData: [
                    {
                        label: "选择", name: "LibraryFileID", index: "IsCheck", width: 60, align: "center", sort: false, frozen: true,
                        formatter: function (cellvalue, row, op, $cell) {
                            return "<input id='" + row.LibraryFileID + "' name='my" + row.LibraryFileID + "' type='checkbox' style='cursor: pointer;margin-top: 8px;' onclick='Select(\"" + row.LibraryFileID + "\")'>";
                        }
                    },
                    { label: "元件库文件名称", name: "LibraryFileName", width: 250, align: "left", sort: false },
                    { label: '创建人', name: 'CreateUserName', width: 120, align: "center", sort: false },
                    {
                        label: "创建时间", name: "CreateTime", width: 150, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }
                    },
                    { name: "", width: 1, sort: false }
                ],
                mainId: 'LibraryFileID',
                //sidx: "LibraryFileName",
                //sord: "ASC",
                isPage: false,
                onRenderComplete: function (data) {
                    //默认已经选中的用户
                    var obj = $("input[type='checkbox']");
                    for (var i = 0; i < obj.length; i++) {
                        if (selectData.length > 0) {
                            var temp = selectData.find(function (x) {
                                return x.LibraryFileID === obj[i].id
                            });
                            if (temp != undefined && temp != "") {
                                obj[i].checked = true;
                            } else {
                                obj[i].checked = false;
                            }
                        }
                    }
                }
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.LibraryCatalogueID = LibraryCatalogueID;
            param.ProjectId = ProjectId;
            param.LibraryFileName = $("#LibraryFileName").val();
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    page.init();
}
//选中
function Select(id) {
    var obj = $('#gridtable').context.getElementsByName("my" + id);
    if (obj && obj.length > 0) {
        if (obj[0].checked) {
            var param = {};
            param.ObjectTypeLibID = top.learun.newGuid();
            param.LibraryFileID = id;
            param.ObjectTypeID = ObjectTypeID;
            selectData.push(param);
        } else {
            selectData = $.grep(selectData, function (item) {
                return item.LibraryFileID !== id;
            });
        }
    }
};