/**
 * 交付模块-标准预测计划采购确认页面JS
 *
 * @author jitong.zhang
 * @version 1.0
 */
var page = {
    editIndex: undefined,
    bodyHeight: document.documentElement.clientHeight,
    /**
     * 时间选中下边记录
     */
    replySelectInfo: {index: undefined, rows: undefined},
    saveOrUpdate: function (call) {
        var data = page.replyDetailGrid.datagrid('getRows');
        var replySelectVal = page.replyDataGrid.datagrid('getSelected');
        if(!data || !replySelectVal){
            layer.alert('参数错误', {icon: 2});
            return
        }
        $.each(data, function () {
            this['dataId'] = this.id;
            this['supplierCode'] = replySelectVal.supplierCode;
        });
        var loadingIndex = layer.msg(tipsi18n.tjz, {icon: 16, shade: 0});
        $.ajax({
            url: g.ctx + 'pc/order/basicPlan/rest/save/reply/detail',
            data: {data: JSON.stringify(page.replyDetailGrid.datagrid('getRows')),
                isPurchaseReplace: true},//确认时保存时间、操作人、状态
            type: 'post',
            success: function (res) {
                layer.close(loadingIndex);
                layer.alert(tipsi18n.bccg, function (index) {
                    if(!call){
                        layer.close(index)
                    }
                    call();
                });
            },
            error: function (e) {
                layer.close(loadingIndex);
                layer.alert('保存出现错误', function (index) {
                    if(!call){
                        layer.close(index)
                    }
                    call();
                });
            }
        });
    },
    /**
     * 确认回复
     */
    confirmBatchData: function() {
        var row = $('#tb').datagrid('getSelected');
        if (!row) {
            parent.layer.msg(itablei8n.plselect);
            return;
        }
        parent.layer.prompt({title: tipsi18n.qtxqryj, formType: 2, value: handlei18n.qr, maxlength: 500}, function(text, index) {
            // layer.confirm('确定此条信息?', function (index) {
            var loadIndex = layer.msg(tipsi18n.qrz, {icon: 16, shade: 0});
            $.ajax({
                url: g.ctx + 'pc/order/basicPlan/rest/confirm/reply/by/ids',
                type: 'POST',
                data: {isBatch: true, detailId: row.detailId, supplierCode: row.supplierCode, confirmType: 'confirm', refusalReason: text},
                success: function (res) {
                    layer.close(loadIndex);
                    parent.layer.close(index);
                    layer.msg(tipsi18n.yqr, {icon: 1});
                    page.replyDetailGrid.datagrid("load");
                },
                error: function (e) {
                    layer.close(loadIndex);
                    layer.alert('出现错误, 确认失败');
                }
            })
        })
    },
    /**
     * 拒绝
     */
    refuseBatchData: function() {
        var row = page.dataGrid.datagrid('getSelected');
        if (!row) {
            parent.layer.msg(itablei8n.plselect);
            return;
        }
        parent.layer.prompt({title: tipsi18n.qtxjjyy, formType: 2, value: itablei8n.jj, maxlength: 500}, function(text, index) {
            parent.layer.close(index);
            var loadIndex = layer.msg(tipsi18n.jzz, {icon: 16, time:0 ,shade: 0.1});
            $.ajax({
                url: g.ctx + 'pc/order/basicPlan/rest/confirm/reply/by/ids',
                type: 'post',
                data: {isBatch: true, detailId: row.detailId, supplierCode: row.supplierCode, refusalReason: text, confirmType: 'refuse'},
                success: function (res) {
                    layer.close(loadIndex);
                    layer.msg(tipsi18n.yjj, {icon: 1});
                    page.replyDetailGrid.datagrid("load");
                },
                error: function (e) {
                    layer.close(loadIndex);
                    var respObj = JSON.parse(e.responseText);
                    layer.alert(respObj.message || (typeof (respObj === 'string') ? respObj : "出现错误"), {icon: 2});
                }
            });
        });
    },
    showDetail: function(id) {
        var row = page.dataGrid.datagrid('getSelected');
        if (!id && !row) {
            parent.layer.msg(itablei8n.plselect);
            return;
        }
        page.settingDialog = openDialog("settingDialog", "/delivery/require/goods/plan/show/reply/by_date?replyId=" + (id ? id : row.id),
            {
                title: handlei18n.gyshfslck,
                width: 1200,
                height: 753,
                cache: false,
                modal: true,
                border: false,
                draggable: false,
                shadow: false,
                onClose: function () {
                    page.settingDialog.dialog('destroy');
                },
                buttons: [{
                    text: itablei8n.close,
                    handler: function () {
                        page.settingDialog.dialog('close');
                    }
                }]
            });
    },
    selectRow: function(rowIndex) {
        var rows = page.dataGrid.datagrid("getRows");
        page.replyDetailGrid.datagrid('options').url = g.ctx + 'pc/order/basicPlan/rest/by/detail_id/get/plan_data';
        page.replyDetailGrid.datagrid("load", {
            detailId: rows[rowIndex].detailId,
            supplierCode: rows[rowIndex].supplierCode
        });
    },
    /**
     * 单个回复操作
     * @param confirmType 确认类型 确认or拒绝
     * @param replyId 回复ID
     */
    replySingleHandle: function(confirmType, replyId) {
        if(!confirmType || !replyId){
            layer.msg('参数为空', {icon: 2});
            return
        }
        if(confirmType === 'refuse'){
            parent.layer.prompt({title: tipsi18n.qtxjjyy, formType: 2, value: itablei8n.jj, maxlength: 500}, function(text, index) {
                parent.layer.close(index);
                var loadingIndex = layer.msg(tipsi18n.tjz, {icon: 16, shade: 0, time: 0});
                $.ajax({
                    url: g.ctx + 'pc/order/basicPlan/rest/confirm/reply/by/ids',
                    data: {replyIds: JSON.stringify([replyId]), confirmType: confirmType, refusalReason: text},
                    type: 'POST',
                    success: function () {
                        layer.close(loadingIndex);
                        layer.msg(tipsi18n.czcg, {icon: 1});
                        page.replyDetailGrid.datagrid("load");
                    },
                    error: function (e) {
                        layer.close(loadingIndex);
                        layer.alert('保存出现错误', function (index) {
                            layer.close(index);
                            page.replyDetailGrid.datagrid("load");
                        });
                    }
                });
            });
        }else {
            parent.layer.prompt({title: tipsi18n.qtxqryj, formType: 2, value: handlei18n.qr, maxlength: 500}, function(text, index) {
                var loadIndex = parent.layer.msg('确认中...', {icon: 16, shade: 0, time: 0});
                $.ajax({
                    url: g.ctx + 'pc/order/basicPlan/rest/confirm/reply/by/ids',
                    type: 'POST',
                    data: {confirmType: confirmType, replyIds: JSON.stringify([replyId]), refusalReason: text},
                    success: function (res) {
                        parent.layer.close(loadIndex);
                        parent.layer.close(index);
                        parent.layer.msg(tipsi18n.czcg, {icon: 1});
                        page.replyDetailGrid.datagrid("load");
                    },
                    error: function (e) {
                        parent.layer.close(loadIndex);
                        parent.layer.alert('出现错误, 操作失败');
                    }
                })
            });
        }

    },
    /**
     * 刷新时间表格
     */
    refreshTable: function() {
        page.dataGrid.datagrid("load");
    },
    initTable: function () {
        var toolbars = [
            {
                text: itablei8n.hfqr,
                iconCls: 'icon-confirm',
                handler: page.confirmBatchData
            }, "-",
            {
                text: itablei8n.hfjj,
                iconCls: 'icon-cancel',
                handler: page.refuseBatchData
            }
        ];
        page.dataGrid = $('#tb').datagrid({
            url: g.ctx + 'pc/order/basicPlan/rest/get/reply',
            iconCls: 'icon-list', width: '100%', height: page.bodyHeight - 25,
            singleSelect: true, pagination: true, loadMsg: itablei8n.dataLoading, rownumbers: true,
            fitColumns: true, nowrap: true, remoteSort: true, striped: true, page: 1, pageSize: 20,
            queryParams: {id: planId},
            toolbar: toolbars,
            onSelect: page.selectRow,
            columns: [[
                {field: 'ck',checkbox: true},
                {field: 'serialNumber', title: popupi18n.hfbb, width: 130, align: 'left'},
                {field: 'productCode', title: itableTitlei18n.wlbm, width: 130, align: 'left'},
                {field: 'isRiskMaterial', title: itableTitlei18n.fxwl, width: 80, align: 'left',
                    formatter: function (val) {
                        if ('是' === val) {
                            return '<span style="color:red">是</span>';
                        }
                        return val;
                    }
                },
                {field: 'supplierCode', title: itableTitlei18n.gysbm, width: 130, align: 'left'},
                {field: 'adjustNum', title: handlei18n.hfsl, width: 120, align: 'left'},
                {field: 'distributeNum', title: popupi18n.ycfpsl, width: 120, align: 'left'}
            ]]
        });

        page.replyDetailGrid = $('#reply_data_table').datagrid({
            iconCls: 'icon-list', width: '100%', height: page.bodyHeight - 25,
            singleSelect: true, pagination: true, loadMsg: itablei8n.dataLoading, rownumbers: true,
            fitColumns: true, nowrap: true, remoteSort: true, striped: true, page: 1, pageSize: 50,
            queryParams: {page: 1, rows: 50},
            columns: [[
                {field: 'ck',checkbox: true},
                {field: 'dateType', title: popupi18n.rqlx, width: 80, align: 'left',
                    formatter: function (val) {
                        if (val === 1) {
                            return popupi18n.ri
                        } else if (val === 2) {
                            return popupi18n.zhou
                        } else {
                            return popupi18n.yue
                        }
                    }
                },
                {field: 'dateTime', title: popupi18n.rq, width: 130, align: 'left'},
                {field: 'distributeNum', title: popupi18n.fpsl, width: 100, align: 'left'},
                {field: 'adjustNum', title: handlei18n.hfsl, width: 100, align: 'left'},
                {
                    field: 'adjustDate', title: handlei18n.hfsj, width: 160, align: 'left',
                    formatter: function (val) {
                        return val ? moment(val).format('YYYY-MM-DD HH:mm:ss') : ''
                    }
                },
                {
                    field: 'status', title: itablei8n.status, width: 100, align: 'left',
                    formatter: function (val) {
                        val = Number(val);
                        switch (val) {
                            case 10:
                                return '<span style="color: #a4a32e">'+statusi18n.dqr+'</span>';
                            case 12:
                                return '<span style="color: #01e300">'+tipsi18n.yqr+'</span>';
                            case 11:
                                return '<span style="color: #e30002">'+tipsi18n.yjj+'</span>';
                            case 1:
                                return '<span style="color: #ffbf05">'+statusi18n.dhf+'</span>';
                            case 0:
                                return '<span style="color: #e38e00">'+statusi18n.dhf+'</span>';
                            default:
                                return '<span style="color: #ffbf05">'+itablei8n.wz+'</span>'
                        }
                    }
                },
                {
                    field: 'refusalReason', title: popupi18n.qryj, width: 100, align: 'center',
                    formatter: function (val) {
                        return '<span title="'+ val +'">'+ val +'</span>'
                    }
                },
                {field: 'id', title: handlei18n.cz, width: 150, align: 'center',
                    formatter: function (val) {
                        return '<a onclick="page.replySingleHandle(\'confirm\', '+ val +')"' +
                            ' href="javascript:void(0)">'+itablei8n.qr+'</a>' +
                            '<a style="margin-left: 10px;color: red;"' +
                            ' onclick="page.replySingleHandle(\'refuse\', '+ val +')"' +
                            ' href="javascript:void(0)">'+itablei8n.jj+'</a>';
                    }
                }
            ]]
        });
    }
};
$(function () {
    page.initTable();
});