/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-02-24 11:10
 * 描  述：对象类型
 */
var refreshGirdData;
var ProjectId = parent.request('ProjectId');
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGird();
            page.bind();

        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                page.search();
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            $('#lr_lookup').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {

                    var isParent = $('#gridtable').jfGridValue('IsParent');
                    if (isParent != "0") {
                        learun.alert.warning('请选择最后一级！');
                        return false;
                    }
                    else {
                        learun.layerForm({
                            id: 'form',
                            title: 'lookup表管理 (' + $('#gridtable').jfGridValue('ObjectTypeName') +")",
                            url: top.$.rootUrl + '/ZZDT_EC/ec_LookupTable/LookupIndex?ProjectId=' + ProjectId + "&ObjectTypeId=" + keyValue,
                            width: 900,
                            height: 600,
                            btn: null
                        });
                    }

                };

            });
            $('#lr_propertyImport').on('click', function () {
                //批量导入 
                var format = 'xlsx';
                var uploadKeyValue = Guid.NewGuid().toString();
                learun.layerForm({
                    id: 'importfile',
                    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('importfile');
                        MuliteImport(uploadKeyValue);
                    }
                });

            });
            //属性必填矩阵导出
            $('#lr_propertyExport').on('click', function () {
                 
                learun.loading(true, "导出中...");
                var iframeUrl = top.$.rootUrl + '/ZZDT_EC/ec_objecttype/ExportPropsIfNeed?ProjectId=' + ProjectId;
                
                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);
            });
            // 新增
            $('#lr_add').on('click', function () {
                var UpObjectTypeID = $('#gridtable').jfGridValue('ObjectTypeID');
                learun.layerForm({
                    id: 'form',
                    title: '新增',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/Form?UpObjectTypeID=' + UpObjectTypeID + "&ProjectId=" + ProjectId,
                    width: 650,
                    height: 360,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                var keyName = $('#gridtable').jfGridValue('ObjectTypeName');
                if (learun.checkrow(keyValue)) {
                    if (keyName == "OPC" || keyName == "基点" || keyName == "图框") {
                        learun.alert.warning("OPC为系统内置的特殊类型，无法编辑其属性！");
                    }
                    else {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑',
                            url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/Form?keyValue=' + keyValue + "&ProjectId=" + ProjectId,
                            //后端还是form这个action接收到了，然后？后面的参数，在form.js中，会通过request函数去解析里面的内容
                            //request函数定义在login/index中 

                            width: 650,
                            height: 550,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }

                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                var keyName = $('#gridtable').jfGridValue('ObjectTypeName');
                if (learun.checkrow(keyValue)) {
                    if (keyName == "OPC" || keyName == "基点" || keyName == "图框" || keyName == "TagNumber") {
                        learun.alert.warning(keyName + "为系统内置的特殊类型，无法删除！");
                        return false;
                    }
                    //先判断是否在工程数据表已存在数据  不存在就删除，存在就提示信息
                    $.ajax({
                        type: 'GET',
                        url: top.$.rootUrl + "/ZZDT_EC/ec_objecttype/IsHaveEngineData?rdo=" + Math.random(),
                        data: { ProjectId: ProjectId, objectTypeID: keyValue },
                        traditional: true,
                        success: function (data) {
                            var data = JSON.parse(data);
                            if (data.code == 200) {
                                if (data.data) {
                                    learun.alert.error('该对象类型及子类型中包含设计数据，不能被删除！');
                                } else {
                                    learun.layerConfirm('是否确认删除该项！', function (res) {
                                        if (res) {
                                            learun.deleteForm(top.$.rootUrl + '/ZZDT_EC/ec_objecttype/DeleteForm', { keyValue: keyValue, ProjectId: ProjectId }, function () {
                                                refreshGirdData();
                                            });
                                        }
                                    });
                                }
                            }
                        },
                        error: function (msg) {
                            learun.alert.error(msg);
                        }, complete: function (data) {
                        }
                    });

                }
            });
            // 属性设置
            $('#lr_propertyset').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {
                    var isParent = $('#gridtable').jfGridValue('IsParent');
                    if (isParent != "0") {
                        learun.alert.warning('请选择最后一级！');
                        return false;
                    }
                    //if (keyValue == "OPC" || keyValue == "基点" || keyValue == "图框") {
                    //    learun.alert.warning("OPC为系统内置的特殊类型 \u000d 无法进行“属性设置”！");
                    //    return false;
                    //}
                    learun.layerForm({
                        id: 'form',
                        title: '属性设置',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_property/SelectProperty?ObjectTypeID=' + keyValue + "&ProjectId=" + ProjectId,
                        width: 850,
                        height: 600,
                        btn: null
                    });
                }
            });
            //命名规则
            $('#lr_namingconv').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                var keyName = $('#gridtable').jfGridValue('ObjectTypeName');
                if (learun.checkrow(keyValue)) {
                    var isParent = $('#gridtable').jfGridValue('IsParent');//IsParent 是在后端sql查询语句里用case when做的一个列名
                    if (isParent != "0") {
                        learun.alert.warning('请选择最后一级！');
                        return false;
                    }
                    if (keyName == "OPC" || keyName == "基点" || keyName == "图框") {
                        learun.alert.warning("OPC为系统内置的特殊类型 \u000d 无法进行“命名规则设置”！");
                        return false;
                    }
                    learun.layerForm({
                        id: 'formnamingconv',//窗口id
                        title: '命名规则',//标题
                        url: top.$.rootUrl + '/ZZDT_EC/ec_objecttypenamingconv/Index?ObjectTypeID=' + keyValue + "&ProjectId=" + ProjectId,//窗体打开页面连接
                        width: 800,
                        height: 500,
                        maxmin: true,
                        btn: null//窗体底部显示按钮
                    });
                }
            });
            //选择元件库
            $('#lr_selectlibrary').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {
                    var isParent = $('#gridtable').jfGridValue('IsParent');
                    if (isParent != "0") {
                        learun.alert.warning('请选择最后一级！');
                        return false;
                    }
                    learun.layerForm({
                        id: 'selectlibrary',
                        title: '选择元件库',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_library_file/SelectIndex?ObjectTypeID=' + keyValue + "&ProjectId=" + ProjectId,
                        width: 850,
                        height: 600,
                        btn: null
                    });
                }
            });
        },
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/GetList',
                headData: [
                    { label: '对象类型名称', name: 'ObjectTypeName', width: 500, align: "left", sort: false },
                    { label: '对象类型名称(英文)', name: 'ObjectTypeNameEN', width: 300, align: "left", sort: false },
                    {
                        label: "是否生成流水号", name: "IsSerialNumber", width: 120, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            if (cellvalue == '1') {
                                return "是";
                            } else {
                                return "否";
                            }
                        }
                    },
                    { label: '排序', name: 'OrderID', width: 100, align: "center", sort: false },
                    { name: "", width: 1, sort: false }
                ],
                mainId: 'ObjectTypeID',
                isTree: true,
                parentId: 'UpObjectTypeID',
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.KeyWordAndDir = $("#ObjectTypeName").val();
            param.ProjectId = ProjectId;
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };
    function MuliteImport(fileId) {
        var learun = top.learun;
        learun.loading(true, "批量导入数据中...");
        $.ajax({
            type: 'POST',
            url: top.$.rootUrl + "/ZZDT_EC/ec_objecttype/ImportPropsIfNeed?rdo=" + Math.random(),
            data: { fileId: fileId, ProjectId: ProjectId },
            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);
            }
        });
    }
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    page.init();
}
