var el = {
    searchForm: '#searchForm',
    tb: '#tb',
    purchaseGroupCode: '#purchaseGroupCode',
    status: '#status',
    price_audit_add: '#price_audit_add',
    price_audit_edit: '#price_audit_edit',
    price_audit_search: '#price_audit_search',
    price_audit_effect: '#price_audit_effect',
    price_push_oa: '#price_push_oa',
    btn_remove: '#btn_remove',
    bodyHeight: document.documentElement.clientHeight
};
$(function () {
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll');
    initCombo();
    loadGrid();
});

function initCombo(){
    $(el.status).combobox({
        editable: false,
        panelHeight: 'auto',
        valueField: 'value',
        textField: 'label',
        data: [
            {value: '', label: '全部'},
            {value: 0, label: '新建'},
            {value: 1, label: 'OA审批中'},
            {value: 2, label: 'OA审批拒绝'},
            {value: 3, label: 'OA审批通过'},
            {value: 4, label: 'OA审批驳回'}
        ],
    });

    $(el.purchaseGroupCode).combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        method: 'POST',
        prompt: '--- 请选择 ---',
        data: getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll')
    });
}


function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight - 20, 37, 40);
    $(el.tb).datagrid({
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/inquiry/price/audit/rest/list',
        singleSelect: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        loadMsg: '数据加载中......',
        rownumbers: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'status',
                title: '状态',
                width: '120',
                formatter: function (value) {
                    if (value === 0) {
                        return "待提交OA审批"
                    } else if (value === 1) {
                        return '<a style="color: blue">OA审批中</a>'
                    } else if (value === 2) {
                        return '<a style="color: red">OA审批拒绝</a>'
                    } else if (value === 3) {
                        return '<a style="color: green">OA审批通过</a>'
                    } else if (value === 4) {
                        return '<a style="color: grey">OA审批驳回</a>'
                    }
                }
            },
            {
                field: 'auditCode',
                title: '调价单号',
                width: '120',
                align: 'left'
            },
            {
                field: 'processId',
                title: 'OA流程号',
                width: 200,
                sortable: true,
                formatter: function (value, row) {
                    return oaTrack(value, OaProcessType.AUDIT_PRICE)
                }
            },
            {
                field: 'title',
                title: '标题',
                width: '220',
                align: 'left'
            },
            {
                field: 'purchaseGroupCode',
                title: '采购组',
                width: '180',
                align: 'left',
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
                }
            },
            {
                field: 'supplierName',
                title: '供应商',
                width: '200',
                align: 'left'
            },
            {
                field: 'createUserName',
                title: '创建人',
                width: '180',
                align: 'left'
            },

            {
                field: 'createTime',
                title: '创建时间',
                width: '180',
                align: 'center',
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                    return '';
                }
            }
        ]],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                id:'price_audit_add',
                handler: function () {
                    detail(0);
                }
            },
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id:'price_audit_edit',
                handler: function () {
                    detail(1);
                    setOperationLog("编辑按钮", "调价管理");

                }
            },"-",{
                text: '查看',
                iconCls: 'icon-search',
                id:'price_audit_search',
                handler: function () {
                    detail(2);
                    setOperationLog("查看按钮", "调价管理");

                }
            }
            ,"-",{
                text: '提交OA审批',
                iconCls: 'icon-ok',
                id: 'price_push_oa',
                handler: function () {
                    pushOa();
                }
            },
            "-",{
                text: '授权生效',
                iconCls: 'icon-add',
                id:'price_audit_effect',
                handler: function () {
                    pass();
                }
            },
            "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                id: 'btn_remove',
                handler: function () {
                    remove();
                }
            }
        ],
        onLoadSuccess:function () {
            displayButtonByUserRole('priceInfoAudit');
        },
        onCheck: function (rowIndex) {
            var rows = $(el.tb).datagrid('getRows');
            var status = rows[rowIndex].status;
            //状态 0：新建 1：审批中 2：审批拒绝 3：审批通过
            switch (Number(status)) {
                case 0:
                    $(el.price_audit_add).linkbutton('enable');
                    $(el.price_audit_edit).linkbutton('enable');
                    $(el.price_audit_search).linkbutton('enable');
                    $(el.price_push_oa).linkbutton('enable');
                    $(el.price_audit_effect).linkbutton('enable');
                    $(el.btn_remove).linkbutton('enable');
                    break;
                case 1:
                    $(el.price_audit_add).linkbutton('enable');
                    $(el.price_audit_edit).linkbutton('disable');
                    $(el.price_audit_search).linkbutton('enable');
                    $(el.price_push_oa).linkbutton('disable');
                    $(el.price_audit_effect).linkbutton('enable');
                    $(el.btn_remove).linkbutton('disable');
                    break;
                case 2:
                    $(el.price_audit_add).linkbutton('enable');
                    $(el.price_audit_edit).linkbutton('enable');
                    $(el.price_audit_search).linkbutton('enable');
                    $(el.price_push_oa).linkbutton('enable');
                    $(el.price_audit_effect).linkbutton('enable');
                    $(el.btn_remove).linkbutton('enable');
                    break;
                case 4:
                    $(el.price_audit_add).linkbutton('enable');
                    $(el.price_audit_edit).linkbutton('disable');
                    $(el.price_audit_search).linkbutton('enable');
                    $(el.price_push_oa).linkbutton('disable');
                    $(el.price_audit_effect).linkbutton('disable');
                    $(el.btn_remove).linkbutton('disable');
                    break;
                default:
                    break;
            }
        }
    });
}

function detail(optType){
    var id = '';
    if (optType===otpTypeEnum.EDIT||optType===2){
        var row = $(el.tb).datagrid('getSelected'), userId = $(el.userId).val();
        if (!row) {
            topWin.layer.alert('请选中一条记录', {icon: 0});
            return false;
        }
        id = row.id;
    }
    var buttons = [];
    if (optType===2){
        buttons.push(
            {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    dialogElement.dialog('close');
                }
            }
        );
    }else {
        buttons.push(
            {
                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');
                }
            }
        );
    }
    var dialogElement = openDialog("editDialog", g.ctx + "pc/inquiry/price/audit/detail?id="+id+"&optType="+optType, {
        title: (optType === otpTypeEnum.ADD ? "新增" :optType===otpTypeEnum.EDIT?"编辑":"查看"),
        width: '100%',
        height: '100%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            dialogElement.dialog('destroy');
        },
        buttons: buttons
    });

}

/**
 * 删除
 */
function remove(){
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        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});
        $.ajax({
            url: g.ctx + 'pc/inquiry/price/audit/rest/remove',
            method: "post",
            data: {id: id},
            success: function () {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
                $(el.tb).datagrid('clearSelections');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

/**
 * 提交Oa
 */
function pushOa() {
    var cbox = $(el.tb).datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var id = cbox.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/price/audit/rest/pushOa';
        $.ajax({
            url: url,
            method: "post",
            data: {id: id},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

/**
 * 生效
 */
function pass(){
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        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});
        $.ajax({
            url: g.ctx + 'pc/inquiry/price/audit/rest/pass',
            method: "post",
            data: {id: id},
            success: function () {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
                $(el.tb).datagrid('clearSelections');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}
