/**
 *  询报价模板首页JS
 *
 * Created by likui on 2019/10/29.
 */
'use strict';
var el = {
    tb: '#tb',
    templateName: '#templateName',
    purchaseGroupCode: '#purchaseGroupCode',
    searchForm: '#searchForm',
    moduleCode: '#moduleCode',
    userId: '#userId',
    addOrEditDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    initCombo();

    loadGrid();
});


/**
 * 初始化表格
 */
function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: "数据加载中......",
        url: g.ctx + 'pc/inquiry/template/rest/list',
        singleSelect: true,
        height: el.bodyHeight - searchHeight,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        rownumbers: true,
        queryParams: {},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'templateCode',
                    title: '模板编码',
                    width: 200
                },
                {
                    field: 'templateName',
                    title: '模板名称',
                    width: 300
                },
                {
                    field: 'purchaseGroupCode',
                    title: '采购组编码',
                    width: 150,
                    formatter: function (value, row) {
                        if(!row.purchaseGroupCode){
                            return '全部采购组';
                        }
                        return value;
                    }
                },
                {
                    field: 'purchaseGroupName',
                    title: '采购组名称',
                    width: 150,
                    formatter: function (value, row) {
                        if(!row.purchaseGroupCode){
                            return '全部采购组';
                        }
                        return value;
                    }
                },
                {
                    field: 'createUserName',
                    title: '创建人',
                    width: 150
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 140,
                    formatter: function (value) {
                        if(value){
                            return moment(value).format('YYYY-MM-DD HH:mm');
                        }
                        return '';
                    }
                },
                {
                    field: 'deleteFlag',
                    title: '删除标识',
                    width: 100,
                    formatter: function (value) {
                        if(Number(value) === 0){
                            return '未删除';
                        }else {
                            return '<span style="color: red;">已删除</span>'
                        }
                    }
                }
            ]
        ],
        toolbar: [
            {
                id: 'btn_add',
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    detail(0);
                }
            },
            '-',
            {
                id: 'btn_edit',
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {

                    detail(1);
                }
            },
            '-',
            {
                id: 'btn_copy',
                text: '复制',
                iconCls: 'icon-plan-copy',
                handler: function () {
                    detail(2);
                }
            },
            '-',
            {
                id: 'btn_del',
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    del();
                }
            },

        ],
        onLoadSuccess: function () {
            displayButtonByUserRole('inquiry.template.manage.inquiry.price.index');
            $(el.tb).datagrid('clearSelections');
        }
    });
}


/**
 * 初始化下拉框
 */
function initCombo() {
    $(el.purchaseGroupCode).combobox({
        valueField: 'purchaseGroupCode',
        textField: 'purchaseGroupName',
        method: 'POST',
        // panelHeight: 200,
        // panelWidth: 240,
        prompt: '--- 请选择 ---',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/listAll'
    });
}


function detail(optType){
    var id = '';
    if (optType===1||optType===2){
        var row = $(el.tb).datagrid('getSelected'), userId = $(el.userId).val();
        if (!row) {
            topWin.layer.alert('请选中一条记录', {icon: 0});
            return false;
        }
        id = row.id;
    }
    if (optType===2&&userId !== (row.createUser + '')){
        topWin.layer.alert('不能编辑别人的模板', {icon: 0});
        return false;
    }
    var dialogElement = openDialog("editDialog", g.ctx + "pc/inquiry/template/detail?id="+id+"&optType="+optType, {
        title: (optType === 0 ? "新增" :optType===1?"编辑":"复制"),
        width: '60%',
        height: '95%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            dialogElement.dialog('destroy');
        },
        buttons: [{
            text: '提交',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['editDialog'].saveOrUpdate(function () {
                    topWin.layer.msg('数据保存成功', {icon: 1});
                    dialogElement.dialog('close');
                    $(el.tb).datagrid('reload');
                });
            }
        }, {
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                dialogElement.dialog('close');
            }
        }]
    });

}

/**
 * 添加模板
 */
function add() {
    var url = g.ctx + 'xunjia/template/manage/inquiry/price/detail?moduleTempId=0&optType=0';
    pageOpenDialog(url, '新增询价模板');
}


/**
 * 修改模板
 */
function edit() {
    var row = $(el.tb).datagrid('getSelected'), userId = $(el.userId).val();
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    if (userId !== (row.create_id + '')) {
        topWin.layer.alert('不能编辑别人的模板', {icon: 0});
        return false;
    }
    var url = g.ctx + 'xunjia/template/manage/inquiry/price/detail?moduleTempId=' + row.id + '&optType=1';
    pageOpenDialog(url, '编辑询价模板');
}


/**
 * 删除模板
 */
function del() {
    var row = $(el.tb).datagrid('getSelected'), userId = $(el.userId).val();
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    if (userId !== (row.createUser + '')) {
        topWin.layer.alert('不能删除别人的模板', {icon: 0});
        return false;
    }
    var id = row.id;
    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/inquiry/template/rest/updateDeleteFlag';
        $.ajax({
            url: url,
            method: "post",
            data: {id: id},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
                $(el.tb).datagrid('clearSelections');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.close(index);
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    });
}


/**
 * 复制模板
 */
function copyTemplate() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    var url = g.ctx + 'xunjia/template/manage/inquiry/price/detail?moduleTempId=' + row.id + '&optType=2';
    pageOpenDialog(url, '复制询价模板');
}


/**
 * 打开弹框
 *
 * @param url 弹框页面的url
 * @param title 弹框的标题
 */
function pageOpenDialog(url, title) {
    el.addOrEditDialog = openDialog('addOrEdit', url, {
        title: title,
        width: '50%',
        height: '95%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addOrEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['addOrEdit'].save(function (data) {
                        if(data){
                            topWin.layer.alert(data, {icon: 2});
                        }else {
                            topWin.layer.msg(tipsi18n.czcg, {icon: 1});
                            $(el.tb).datagrid('reload');
                            el.addOrEditDialog.dialog('close');
                        }
                    });
                }
            },
            {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    el.addOrEditDialog.dialog('close');
                }
            }
        ]
    });
}