/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-01-30 15:57
 * 描  述：图纸文件
 */
var refreshGirdData;
var ProjectId = parent.request('ProjectId');
var DrawingCatalogueID;
var relrtree; //初始化树形
var ShowFile;
var isCaoz = false;
var showLeftTree = request("showLeftTree");
var showContent;
var currentUserId = "";
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            currentUserId = learun.clientdata.get(['userinfo']).userId;
            page.initGird();
            page.bind();
        },
        bind: function () {
            if (showLeftTree == "0") {
                $(".lr-layout-left").hide();
                $(".lr-layout-title").hide();
                $(".lr-layout-title").parent().addClass("lr-layout-wrap-notitle ");
                $("#lr_layout").removeClass("lr-layout-left-center");
            } else {
                //初始化tree
                relrtree();
            }

            //查询
            $('#btn_Search').on('click', function () {
                page.search($('.lr-layout-tool-left').lrGetFormData());
            });

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

            // 新增
            $('#lr_add').on('click', function () {
                if (!DrawingCatalogueID && showLeftTree != "0") {
                    learun.alert.warning("请选择分类");
                    return;
                }
                learun.layerForm({
                    id: 'form',
                    title: '新增',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/Form?DrawingCatalogueID=' + DrawingCatalogueID + "&ProjectId=" + ProjectId,
                    width: 700,
                    height: 650,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });

            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('DrawingFileID');
                if (learun.checkrow(keyValue)) {
                    var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                    if (currentUserId && currentUserId == createUserID) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑',
                            url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/Form?keyValue=' + keyValue + "&ProjectId=" + ProjectId + "&DrawingCatalogueID=" + DrawingCatalogueID,
                            width: 700,
                            height: 650,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    } else {
                        learun.alert.warning("只能编辑本人创建的图纸!");
                        return;
                    }
                }
            });

            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('DrawingFileID');
                if (learun.checkrow(keyValue)) {
                    var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                    if (currentUserId && currentUserId == createUserID) {
                        //如果是工程图纸就逻辑删除，其他类型就物理删除
                        var delUrl = top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/DeleteForm';
                        var isEngineDWG = $('#gridtable').jfGridValue('IsEngineDWG');
                        if (isEngineDWG == 1) {
                            delUrl = top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/LogicDelete';
                        }
                        learun.layerConfirm('是否确认删除该项！', function (res) {
                            if (res) {
                                learun.deleteForm(delUrl, { keyValue: keyValue, ProjectId: ProjectId }, function () {
                                    refreshGirdData();
                                });
                            }
                        });
                    } else {
                        learun.alert.warning("只能删除本人创建的图纸!");
                        return;
                    }
                }
            });

            // 释放
            $('#lr_free').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('DrawingFileID');
                if (learun.checkrow(keyValue)) {
                    var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                    if (currentUserId && currentUserId == createUserID) {
                        learun.layerConfirm('是否确认释放该项！', function (res) {
                            if (res) {
                                learun.deleteForm(top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/FreeDrawingFile', { keyValue: keyValue, ProjectId: ProjectId }, function () {
                                    refreshGirdData();
                                });
                            }
                        });
                    } else {
                        learun.alert.warning("只能释放本人创建的图纸!");
                        return;
                    }
                }
            });

            // 检入记录
            $('#lr_checkinlog').on('click', function () {
                var drawingFileID = $('#gridtable').jfGridValue('DrawingFileID');
                if (learun.checkrow(drawingFileID)) {
                    learun.layerForm({
                        id: 'logindex',
                        title: '检入记录',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_syn/Index?drawingFileID=' + drawingFileID + "&ProjectId=" + ProjectId,
                        width: 800,
                        height: 600,
                        btn: null
                    });
                }
            });

            // 发布记录
            $('#lr_publishlog').on('click', function () {
                var drawingFileID = $('#gridtable').jfGridValue('DrawingFileID');
                if (learun.checkrow(drawingFileID)) {
                    learun.layerForm({
                        id: 'logindex',
                        title: '发布记录',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_publish/Index?drawingFileID=' + drawingFileID + "&ProjectId=" + ProjectId,
                        width: 800,
                        height: 600,
                        btn: null
                    });
                }
            });

            // 操作记录
            $('#lr_operatelog').on('click', function () {
                var ownerID = $('#gridtable').jfGridValue('DrawingFileID');
                if (learun.checkrow(ownerID)) {
                    learun.layerForm({
                        id: 'logindex',
                        title: '操作记录',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_operate_log/Index?ownerID=' + ownerID + "&ProjectId=" + ProjectId,
                        width: 800,
                        height: 600,
                        btn: null
                    });
                }
            });
            //图纸导入
            $('#lr_importdwg').on('click', function () {
                if (!DrawingCatalogueID && showLeftTree != "0") {
                    learun.alert.warning("请在左侧选择一个图纸目录，作为图纸导入后的存放路径");
                    return;
                }
                learun.layerForm({
                    id: 'importdwg',
                    title: '图纸导入',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/SelectIndex?DrawingCatalogueID=' + DrawingCatalogueID + "&ProjectId=" + ProjectId,
                    width: 1000,
                    height: 700,
                    btn: null,
                    end: function () {
                        refreshGirdData();
                    }
                });
            });


            // 图纸(系统图）上的设备哪些还没有在布置图上出现过的
            $('#lr_Report1').on('click', function () { 
                var drawingFileID = $('#gridtable').jfGridValue('DrawingFileID');
                if (learun.checkrow(keyValue)) {
                     
                    var isEngineDWG = $('#gridtable').jfGridValue('DrawingTypeName');
                    if (isEngineDWG == '系统图') { 
                        var iframeUrl = top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/ReportToExcel?ProjectId=' + ProjectId + "&dwgId=" + drawingFileID;//"&dataItemDetailIDOrDwgCategoryID=" + dataItemDetailIDOrDrawingCategoryID + "&model=" + model;
                        learun.loading(true, "生成报表中...");
                        var body = document.getElementsByTagName("body")[0];
                        var downloadIframe = document.createElement("iframe");
                        downloadIframe.setAttribute("src", iframeUrl);
                        downloadIframe.setAttribute("style", "display:none;")
                        body.appendChild(downloadIframe);
                        learun.loading(false);
                    }
                    else {
                        learun.alert.warning("非系统图。");
                    }
                    

                }
            });
        },
        // 初始化列表
        initGird: function () {
            var tbalewidth = window.innerWidth / 4;
            if (!!top) {
                tbalewidth = top.window.innerWidth / 4.5;
            }
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/GetPageList',
                headData: [
                    {
                        label: "图纸文件名称", name: "DrawingFileName", width: 300, align: "left", sort: false,
                        formatter: function (value, row, op, $cell) {
                            return "<a style='color: #337ab7;text-decoration: underline;' href='javascript:showContent(\"" + row.DrawingFileID + "\");'>" + value + "</a>";
                        }
                    },
                    { label: "图纸类型", name: "DrawingTypeName", width: 150, align: "center", sort: false },
                    { label: "图纸阶段", name: "DrawingStageName", width: 150, align: "center", sort: false },
                    { label: "所属系统", name: "DrawingSystemName", width: 150, align: "center", sort: false },
                    { label: "材料统计", name: "MaterialCensusName", width: 150, align: "center", sort: false },
                    { label: "材料范围", name: "MaterialRangeName", width: 150, align: "center", sort: false },
                    { label: "版本", name: "FileVersion", width: 150, align: "center", sort: false },
                    { label: "排序", name: "OrderID", width: 150, align: "center", sort: false },
                    {
                        label: "是否工程图纸", name: "IsEngineDWG", width: 100, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            if (cellvalue == '1') {
                                return '是';
                            } else {
                                return '否';
                            }
                        }
                    },
                    {
                        label: "是否发布", name: "IsPublish", width: 100, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            if (cellvalue == '1') {
                                return '是';
                            } else {
                                return '否';
                            }
                        }
                    },
                    {
                        label: "是否检出", name: "IsCheckOut", width: 100, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            if (cellvalue == '1') {
                                return "<span style='color: #ed1941;'>是</span>";
                            } else {
                                return '否';
                            }
                        }
                    },
                    { label: "检出人", name: "CheckOutUserName", width: 100, align: "center", sort: false },
                    {
                        label: "检出时间", name: "CheckOutTime", width: 150, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }
                    },
                    { label: "图纸描述1", name: "Describe1", width: 300, align: "left", sort: false },
                    { label: "图纸描述2", name: "Describe2", width: 300, align: "left", sort: false },
                    { label: "图纸描述3", name: "Describe3", width: 300, align: "left", sort: false },
                    { label: "创建人", name: "CreateUserName", width: 100, 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');
                        }
                    },
                    { label: "更新人", name: "UpdateUserName", width: 100, align: "center", sort: false },
                    {
                        label: "更新时间", name: "UpdateTime", width: 150, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }
                    },
                ],
                mainId: 'DrawingFileID',
                sidx: "OrderID",
                sord: "ASC",
                isPage: true
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.DrawingCatalogueID = DrawingCatalogueID;
            param.ProjectId = ProjectId;
            param.IsDelete = "0";
            $('#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: ProjectId },
            nodeClick: function (item) {
                DrawingCatalogueID = item.value;
                $("#titleinfo").html(item.text);
                page.search({ DrawingCatalogueID: item.value });
            }
        });
    };
    ShowFile = function (keyValue) {
        if (learun.checkrow(keyValue)) {
            learun.layerForm({
                id: 'downform',
                title: '附件管理',
                url: top.$.rootUrl + '/LR_SystemModule/Annexes/DownForm?keyVaule=' + keyValue,
                width: $('#gridtable').width() * 0.5,
                height: $('#gridtable').width() * 0.3,
                maxmin: true,
                btn: null
            });
        }
    };
    //查看详情
    showContent = function (id) {
        if (learun.checkrow(id)) {
            learun.layerForm({
                id: 'form',
                title: '查看',
                url: top.$.rootUrl + '/ZZDT_EC/ec_drawing_file/Form?keyValue=' + id + '&openType=look' + "&ProjectId=" + ProjectId,
                width: 700,
                height: 610,
                btn: null
            });
        }
    };
    page.init();
}
