/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-06-01 15:08
 * 描  述：数据字典
 */
var refreshGirdData;
var dataItemID = '';
var ProjectId = parent.request('ProjectId');
var bootstrap = function ($, learun) {
    "use strict";
    var classify_itemCode = '';
    var page = {
        init: function () {
            page.initTree();
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                page.search();
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                if (!classify_itemCode) {
                    learun.alert.warning('请选择字典分类！');
                    return false;
                }
                top.selectedDataItemRow = null;
                var upDataItemDetailID = $('#gridtable').jfGridValue('DataItemDetailID') || '0';
                learun.layerForm({
                    id: 'form',
                    title: '添加字典',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/Form?dataItemID=' + dataItemID + '&itemCode=' + classify_itemCode + '&upDataItemDetailID=' + upDataItemDetailID + "&ProjectId=" + ProjectId,
                    width: 500,
                    height: 370,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('DataItemDetailID');
                top.selectedDataItemRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑字典',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/Form?itemCode=' + classify_itemCode + "&ProjectId=" + ProjectId + "&dataItemID=" + dataItemID + "&keyValue=" + keyValue,
                        width: 500,
                        height: 370,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('DataItemDetailID');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(top.$.rootUrl + '/ZZDT_EC/ec_dataitem/DeleteDetailForm', { keyValue: keyValue, ProjectId: ProjectId }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            /*分类管理*/
            $('#lr_category').on('click', function () {
                learun.layerForm({
                    id: 'ClassifyIndex',
                    title: '分类管理',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/ClassifyIndex?ProjectId=' + ProjectId,
                    width: 900,
                    height: 600,
                    maxmin: true,
                    btn: null,
                    end: function () {
                        location.reload();
                    }
                });
            });

            //excel导入
            $('#lr_bulkload').on('click', function () {
                var format = 'xlsx';
                var uploadKeyValue = Guid.NewGuid().toString();
                learun.layerForm({
                    id: 'importfile2',
                    title: '数据字典通用导入',
                    url: top.$.rootUrl + '/LR_SystemModule/Annexes/UploadForm?keyVaule=' + uploadKeyValue + "&multiple=false" + "&format=" + format,
                    width: 600,
                    height: 400,
                    maxmin: true,
                    btn: ['确认'],
                    callBack: function (id) {
                        learun.layerClose('importfile2');//关闭上传excel的弹窗
                        ExcelImport(uploadKeyValue);
                    }
                });
            });
            $('#lr_bulkload_3D').on('click', function () {
                var format = 'xlsx';
                var uploadKeyValue = Guid.NewGuid().toString();
                learun.layerForm({
                    id: 'importfile',
                    title: 'Smart3D电气分类导入',
                    url: top.$.rootUrl + '/LR_SystemModule/Annexes/UploadForm?keyVaule=' + uploadKeyValue + "&multiple=false" + "&format=" + format,
                    width: 600,
                    height: 400,
                    maxmin: true,
                    btn: ['确认'],
                    callBack: function (id) {
                        learun.layerClose('importfile');//关闭上传excel的弹窗
                        ExcelImport_3D(uploadKeyValue);
                    }
                });
            });

            // lr_export
            $('#lr_export').on('click', function () { 

                    var iframeUrl = top.$.rootUrl + '/ZZDT_EC/ec_dataitem/exportSingle?ProjectId=' + ProjectId + "&dataItemID=" + dataItemID;//"&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);

 
            });
        },
        initTree: function () {
            $('#lr_left_tree').lrtree({
                url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/GetClassifyTree?ProjectId=' + ProjectId,
                nodeClick: function (item) {
                    classify_itemCode = item.value;
                    $('#titleinfo').text(item.text + '(' + classify_itemCode + ')');
                    dataItemID = item.id;
                    page.search();
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/GetDetailList',
                headData: [
                    { label: '名称', name: 'DataItemName', width: 200, align: 'left' },
                    { label: '名称（英文）', name: 'DataItemNameEN', width: 200, align: 'left' },
                    { label: '编号', name: 'DataItemCode', width: 200, align: 'left' },
                    { label: '排序', name: 'OrderID', width: 80, align: 'center' },
                    {
                        label: "有效", name: "IsEnabled", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    { label: "备注", name: "Remark", width: 300, align: "left" },
                    { name: "" }
                ],

                isTree: true,
                mainId: 'DataItemDetailID',
                parentId: 'UpDataItemDetailID',
            });
        },
        search: function (param) {
            param = param || {};
            param.dataItemCode = classify_itemCode;
            param.ProjectId = ProjectId;
            param.keyword = $('#txt_Keyword').val();
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();



}
function ExcelImport(fileId) {
    var learun = top.learun;
    learun.loading(true, "数据字典导入中...");
    $.ajax({
        type: 'POST',
        url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/ExcelImport?ProjectId=' + ProjectId + "&keyValue=" + fileId,
        data: { ProjectId: ProjectId, fileId: fileId },
        traditional: true,
        success: function (data) {
            var data = JSON.parse(data);
            if (data.code == '200') {
                location.reload();
            }
            learun.alert.success(data.info);
        },
        error: function (msg) {
            learun.alert.error(msg);
        },
        complete: function (data) {
            learun.loading(false);
        }
    });
}
function ExcelImport_3D(fileId) {
    var learun = top.learun;
    learun.loading(true, "3D数据导入中...");
    $.ajax({
        type: 'POST',
        url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/ExcelImport3D?ProjectId=' + ProjectId + "&keyValue=" + fileId,
        data: { ProjectId: ProjectId, fileId: fileId },
        traditional: true,
        success: function (data) {
            var data = JSON.parse(data);
            if (data.code == '200') {
                location.reload();
            }
            learun.alert.success(data.info);
        },
        error: function (msg) {
            learun.alert.error(msg);
        },
        complete: function (data) {
            learun.loading(false);
        }
    });
}
