/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-07-04 10:00
 * 描  述：同步关联表
 */
var refreshGirdData;
var selectData = [];
var mainBusinessTableID = request('mainBusinessTableID');
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGird();
            page.bind();
        },
        bind: function () {
            //获取当前选中的数据
            $.ajax({
                url: top.$.rootUrl + '/ZZDT_EC/ec_synrel_table/GetList',
                data: { queryJson: JSON.stringify({ MainBusinessTableID: mainBusinessTableID}) },
                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 temp1 = selectData.find(function (x) {
                    return x.RelBusinessTableID === mainBusinessTableID
                });
                if (temp1 != undefined && temp1 != "") {
                    learun.alert.warning('关联业务表中不能包含主业务表！');
                    return false;
                }
                var postData = {
                    strEntityList: JSON.stringify(selectData)
                }
                $.lrSaveForm(top.$.rootUrl + '/ZZDT_EC/ec_synrel_table/SaveForm?mainBusinessTableID=' + mainBusinessTableID, postData, function (res) {

                });
            });
        },
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_business_table/GetPageList',
                headData: [
                    {
                        label: "选择", name: "BusinessTableID", index: "IsCheck", width: 60, align: "center", sort: false, frozen: true,
                        formatter: function (cellvalue, row, op, $cell) {
                            return "<input id='" + row.BusinessTableID + "' name='my" + row.BusinessTableID + "' type='checkbox' style='cursor: pointer;margin-top: 8px;' onclick='Select(\"" + row.BusinessTableID + "\")'>";
                        }
                    },
                    { label: '业务表名称', name: 'BusinessTableName', width: 250, align: "left", sort: false },
                    { label: '业务表编号', name: 'BusinessTableCode', width: 250, align: "left", sort: false },
                    { label: '', name: '', width: 1, sort: false },
                ],
                mainId: 'BusinessTableID',
                isPage: true,
                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.RelBusinessTableID === obj[i].id
                            });
                            if (temp != undefined && temp != "") {
                                obj[i].checked = true;
                            } else {
                                obj[i].checked = false;
                            }
                        }
                    }
                }
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.BusinessTableName = $('#BusinessTableName').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.SynRelTableID = top.learun.newGuid();
            param.MainBusinessTableID = mainBusinessTableID;
            param.RelBusinessTableID = id;
            selectData.push(param);
        } else {
            selectData = $.grep(selectData, function (item) {
                return item.RelBusinessTableID !== id;
            });
        }
    }
};