/*  
 * 描  述：图纸导入
 */
var refreshGirdData;
var curDrawingCatalogueID = "";
var DrawingCatalogueID = request('DrawingCatalogueID');
var selectData = [];
var ProjectId = request('ProjectId');
var relrtree; //初始化树形
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.bind();
            page.initGird();
        },
        bind: function () {
            $('#SelectProjectId').lrselect({
                allowSearch: true,
                url: top.$.rootUrl + '/ZZDT_EC/ec_project/GetProjectSelectList',
                param: {}, 
                value: 'ProjectId',
                text: 'ProjectName',
                title: 'ProjectName',
                select: function (item) {
                    //选择后，触发的方法
                    relrtree();
                    curDrawingCatalogueID = '';
                    selectData = [];
                    //page.search();
                }
            });

            //查询
            $('#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_drawing_file/ImportDrawing?drawingCatalogueID=' + DrawingCatalogueID + "&ProjectId=" + ProjectId, postData, function (res) {

                });
            });
        },
        // 初始化列表
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/GetPageList',
                headData: [
                    { label: "图纸文件名称", name: "DrawingFileName", width: 300, align: "left", sort: false },
                    { label: "图纸类型", name: "DrawingTypeName", width: 100, align: "center", sort: false },
                    { label: "图纸阶段", name: "DrawingStageName", width: 100, align: "center", sort: false },
                    { label: "所属系统", name: "DrawingSystemName", width: 100, align: "center", sort: false },
                    { label: "版本", name: "FileVersion", width: 80, align: "center", sort: false },
                    { name: "", width: 1, sort: false }
                ],
                mainId: 'DrawingFileID',
                isPage: true,
                isMultiselect: true,
                onSelectRow: function (rowData, isSelect) {
                    selectData = $.grep(selectData, function (item) {
                        return item.DrawingFileID != rowData.DrawingFileID || item.ProjectId != $('#SelectProjectId').lrselectGet();
                    });
                    if (isSelect) {
                        var param = {};
                        param.ProjectId = $('#SelectProjectId').lrselectGet();
                        param.DrawingFileID = rowData.DrawingFileID;
                        selectData.push(param);
                    }
                },
                onRenderComplete: function (data) {
                    //默认已经选中的
                    for (var i = 0; i < data.length; i++) {
                        if (selectData) {
                            var gdata = $.grep(selectData, function (item) {
                                return item.DrawingFileID == data[i].DrawingFileID && item.ProjectId == $('#SelectProjectId').lrselectGet();
                            });
                            if (gdata && gdata.length > 0) {
                                $('#gridtable').jfGridSet("selectRow", data[i].DrawingFileID);
                            }
                        }
                    }
                }
            });
            //page.search();
        },
        //导入图纸所在原项目的清单
        search: function (param) {
            param = param || {};
            param.DrawingCatalogueID = curDrawingCatalogueID;
            param.ProjectId = $('#SelectProjectId').lrselectGet();
            param.DrawingFileName = $("#DrawingFileName").val();
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    
    relrtree = function () {
        // 初始化图纸导入的左侧树形数据
        $('#dataTree').lrtree({
            url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_catalogue/GetTreeData',
            param: { ProjectId: $('#SelectProjectId').lrselectGet() }, //下拉框的选择
            nodeClick: function (item) {
                curDrawingCatalogueID = item.value;
                $("#titleinfo").html(item.text);
                page.search();
            }
        });
    };
    page.init();
}