var gridManager = null;
var baosheEmailManage = null;
var customerEmailManage = null;
var customerRelatedListGrid = null;
var customerPayInfoListGrid = null;
$(document).ready(function () {
    initializeTab();
    initializeUI();
    loadContactListGrid();
    bindEvent();
    loadBaoshenEmailListGrid();
    loadCustomerEmailListGrid();
    loadCustomerRelatedListGrid();
    loadCustomerPayInfoListGrid();
});


function initializeUI() {
    UICtrl.layout("#layout", {leftWidth: 3});
}

function initializeTab() {
    $('#customerTab').tab();
    $('#emailTab').tab()
}


function loadContactListGrid() {
    //增删改控件
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function () {
            addHandler();
        },
        updateHandler: function () {
            updateHandler();
        },
        deleteHandler: function () {
            deleteHandler();
        }
    });
    gridManager = UICtrl.grid("#contactListGrid", {
        columns: [
            {
                display: "联系人名称", name: "contactName", width: 300, minWidth: 60, type: "string", align: "left",
                editor: {
                    required: true, type: "select",
                    data: {
                        type: 'system',
                        name: "customerContactList",
                        back: {
                            customerContactId: "customerContactId",
                            customerId: "customerId",
                            contactName: "contactName",
                            contactPhone: "contactPhone",
                            contactFax: "contactFax",
                            contactMail: "contactMail"
                        }
                    },
                }
            },
            {display: "联系人电话", name: "contactPhone", width: 300, minWidth: 60, type: "string", align: "left"},
            {display: "传真", name: "contactFax", width: 300, minWidth: 60, type: "string", align: "left"},
            {display: "邮箱", name: "contactMail", width: 300, minWidth: 60, type: "string", align: "left"}
        ],
        dataAction: "server",
        url: web_app.name + '/customer/slicedContactList.ajax',
        parms: {customerId: $("#customerId").val()},
        pageSize: 10,
        usePager: true,
        toolbar: toolbarOptions,
        width: "100%",
        height: "60%",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        onDblClickRow: function (data, rowindex, rowobj) {
            updateHandler(data);
        }
    });
    UICtrl.setSearchAreaToggle(gridManager);
}

function loadBaoshenEmailListGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions(
        {
            addHandler: function () {
                // addHandlerBaoshenEmail();
            },
            updateHandler: function () {
                // updateHandlerBaoshenEmail();
            },
            deleteHandler: function () {
                // updateHandlerBaoshenEmail();
            }
        }
    );
    baosheEmailManage = UICtrl.grid("#baoshenEmailListGrid", {
        columns: [
            {
                display: "邮箱地址", name: "customerEmail", width: 300, minWidth: 60, type: "string", align: "left",
                editor: {
                    required: true, type: "select",
                    data: {
                        type: 'system',
                        name: "baoshenEmailList",
                        back: {
                            customerDebtContactId: "customerDebtContactId",
                            customerId: "customerId",
                            customerEmail: "customerEmail",
                            customerContact: "customerContact",
                            sendEmail: "sendEmail",
                            customerMailType: "customerEmailType",
                            createById: "createById",
                            createdByName: "createdByName",
                            createdDate: "createdDate"
                        }
                    },
                }
            },
            {display: "联系人", name: "customerContact", width: 300, minWidth: 60, type: "string", align: "left"},
            {display: "开启邮件发送", name: "sendEmail", width: 300, minWidth: 60, type: "string", align: "left"},
            {display: "创建人", name: "createdByName", width: 300, minWidth: 60, type: "string", align: "left"}
        ],
        dataAction: "server",
        url: web_app.name + '/customerDebtContact/slicedCustomerDebtContactList.ajax',
        parms: {customerId: $("#customerId").val(), customerEmailType: 0},
        pageSize: 10,
        usePager: true,
        toolbar: toolbarOptions,
        width: "100%",
        height: "60%",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true
    });
}

function loadCustomerEmailListGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions(
        {
            addHandler: function () {
                // addHandlerCustomerEmail();
            },
            updateHandler: function () {
                // updateHandlerCustomerEmail();
            },
            deleteHandler: function () {
                // updateHandlerCustomerEmail();
            }
        }
    );
    customerEmailManage = UICtrl.grid("#customerEmailListGrid", {
        columns: [
            {
                display: "邮箱地址", name: "customerEmail", width: 300, minWidth: 60, type: "string", align: "left",
                editor: {
                    required: true, type: "select",
                    data: {
                        type: 'system',
                        name: "customerEmailList",
                        back: {
                            customerDebtContactId: "customerDebtContactId",
                            customerId: "customerId",
                            customerEmail: "customerEmail",
                            customerContact: "customerContact",
                            sendEmail: "sendEmail",
                            customerMailType: "customerEmailType",
                            createById: "createById",
                            createdByName: "createdByName",
                            createdDate: "createdDate"
                        }
                    },
                }
            },
            {display: "联系人", name: "customerContact", width: 300, minWidth: 60, type: "string", align: "left"},
            {display: "开启邮件发送", name: "sendEmail", width: 300, minWidth: 60, type: "string", align: "left"},
            {display: "创建人", name: "createdByName", width: 300, minWidth: 60, type: "string", align: "left"}
        ],
        dataAction: "server",
        url: web_app.name + '/customerDebtContact/slicedCustomerDebtContactList.ajax',
        parms: {customerId: $("#customerId").val(), customerEmailType: 1},
        pageSize: 10,
        usePager: true,
        toolbar: toolbarOptions,
        width: "100%",
        height: "60%",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true
    });
}

function loadCustomerRelatedListGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions(
        {
            addHandler: function () {
                // addCustomerRelatedEmail();
            },
            updateHandler: function () {
                // updateCustomerRelatedEmail();
            },
            deleteHandler: function () {
                // updateCustomerRelatedEmail();
            }
        }
    );
    customerRelatedListGrid = UICtrl.grid("#customerRelatedListGrid", {
        columns: [
            {
                display: "关联客户编码",
                name: "customerCodeRelated",
                width: 200,
                minWidth: 60,
                type: "string",
                align: "left",
                editor: {
                    required: true, type: "select",
                    data: {
                        type: 'system',
                        name: "customerRelatedList",
                        back: {
                            customerRelatedId: "customerRelatedId",
                            customerId: "customerId",
                            customerCodeRelated: "customerCodeRelated",
                            customerShortNameRelated: "customerShortNameRelated",
                            customerNameRelated: "customerNameRelated",
                            createdByName: "createdByName",
                            createdDate: "createdDate"
                        }
                    },
                }
            },
            {
                display: "关联客户简称",
                name: "customerShortNameRelated",
                width: 200,
                minWidth: 60,
                type: "string",
                align: "left"
            },
            {
                display: "关联客户全称",
                name: "customerNameRelated",
                width: 200,
                minWidth: 60,
                type: "string",
                align: "left"
            },
            {display: "录入人", name: "createdByName", width: 200, minWidth: 60, type: "string", align: "left"},
            {display: "录入时间", name: "createdDate", width: 200, minWidth: 60, type: "string", align: "left"},

        ],
        dataAction: "server",
        url: web_app.name + '/customer/slicedContactList.ajax',
        parms: {customerId: $("#customerId").val()},
        pageSize: 10,
        usePager: true,
        toolbar: toolbarOptions,
        width: "100%",
        height: "40%",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        onDblClickRow: function (data, rowindex, rowobj) {
            updateHandler(data);
        }
    });
}

function loadCustomerPayInfoListGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions(
        {
            addHandler: function () {
                // addCustomerPayInfo();
            },
            updateHandler: function () {
                // updateCustomerPayInfo();
            },
            deleteHandler: function () {
                // updateCustomerPayInfo();
            }
        }
    );
    customerPayInfoListGrid = UICtrl.grid("#customerPayInfoListGrid", {
        columns: [
            {
                display: "支付方户名",
                name: "customerPayAccountName",
                width: 200,
                minWidth: 60,
                type: "string",
                align: "left",
                editor: {
                    required: true, type: "select",
                    data: {
                        type: 'system',
                        name: "customerContactList",
                        back: {
                            customerPayInfoId: "customerPayInfoId",
                            customerId: "customerId",
                            customerPayAccountName: "customerPayAccountName",
                            createdByName: "createdByName",
                            createdById: "createdById",
                            createdDate: "createdDate",
                            lastModifiedById: "lastModifiedById",
                            lastModifiedByName: "lastModifiedByName",
                            lastModifiedDate: "lastModifiedDate"
                        }
                    },
                }
            },
            {display: "录入人", name: "createdByName", width: 200, minWidth: 60, type: "string", align: "left"},
            {display: "录入时间", name: "createdDate", width: 200, minWidth: 60, type: "date", align: "left"},
            {display: "修改人", name: "lastModifiedByName", width: 200, minWidth: 60, type: "string", align: "left"},
            {display: "修改时间", name: "lastModifiedDate", width: 200, minWidth: 60, type: "date", align: "left"}
        ],
        dataAction: "server",
        url: web_app.name + '/customer/slicedContactList.ajax',
        parms: {customerId: $("#customerId").val()},
        pageSize: 10,
        usePager: true,
        toolbar: toolbarOptions,
        width: "100%",
        height: "40%",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        onDblClickRow: function (data, rowindex, rowobj) {
            updateHandler(data);
        }
    });
}


function bindEvent() {
    $("#save").click(function () {
        saveData("save");
    })

    $("#close").click(function () {//关闭
        closeDispatchTabItem();
    })
}

//右上角的保存控件
function saveData() {

}

function closeDispatchTabItem() {
    UICtrl.closeCurrentTab();
}
