/**
 * 供应商绩效考核整改审批详情页面
 *
 * Created by likui on 2019/9/16.
 */
'use strict';
var el = {
    noticeId: '#id',
    optType: '#optType',
    examineDiv: '#examineDiv',
    supplierCode: '#supplierCode',
    supplierName: '#supplierName',
    measuresTb: '#measuresTb',
    tbLog: '#tbLog',
    uploadRectifiedAttachment: null,
    examineStatus: '#examineStatus',
    examineUserName: '#examineUserName',
    examineUserCode: '#examineUserCode',
    examineView: '#examineView'
};

$(function () {
    // 审批状态
    $(el.examineStatus).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '同意', value: 1},
            {label: '不同意', value: 2}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto'
    });

    var noticeId = $(el.noticeId).val();
    var optType = $(el.optType).val();
    if (optType === '1') {
        $(el.examineDiv).css('display', 'none');
    }

    initDataGrid(noticeId, optType);

    initLogDataGrid(noticeId);
});


/**
 * 初始化整改措施表格
 */
function initDataGrid(noticeId, optType) {
    var defaults = {
        url: g.ctx + 'pc/spe/rectifiedNotice/rest/measuresList',
        method: "GET",
        queryParams: {
            noticeId: noticeId
        },
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: true,
        pagination: false,
        checkbox: true,
        rownumbers: true,
        height: optType === '1' ? 400 : 300,
        autoSave: true
    };

    var columns = [
        {
            field: 'ck',
            checkbox: true
        },
        {
            field: 'id',
            hidden: true
        },
        {
            field: 'noticeOrderId',
            hidden: true
        },
        {
            field: 'indexRemark',
            title: '不合格项',
            width: 360
        },
        {
            field: 'measuresContent',
            title: '处理措施',
            width: 360
        },
        {
            field: 'personLiable',
            title: '责任人',
            width: 90
        },
        {
            field: 'attachments',
            title: '整改附件',
            width: 90,
            align: 'center',
            formatter: function (value, row, index) {
                return '<a href="javascript:lookAttachment(\'' + value + '\')" >查看</a>';
            }
        },
        {
            field: 'planCompleteTime',
            title: '计划完成时间',
            width: 130,
            formatter: function (value) {
                if (value) {
                    return moment(value).format('YYYY-MM-DD');
                }
                return '';
            }
        },
        {
            field: 'actualCompleteTime',
            title: '完成时间',
            width: 140,
            formatter: function (value) {
                if (value) {
                    return moment(value).format('YYYY-MM-DD HH:mm:ss');
                }
                return '';
            }
        }
    ];

    if (optType === '1') {
        var obj = {
            field: 'status',
            title: '状态',
            width: 80,
            formatter: function (value) {
                if ((value * 1) === 0) {
                    return 'open';
                }
                if ((value * 1) === 1) {
                    return 'close';
                }
            },
            editor: {
                type: 'combobox',
                options: {
                    valueField: 'value',
                    textField: 'label',
                    data: [{label: 'open', value: 0}, {label: 'close', value: 1}],
                    editable: false,
                    panelHeight: 'auto',
                    onSelect: function (data) {
                        var row = $(el.measuresTb).edatagrid('getSelected');
                        if (data.value === 1) {
                            if (null != row) {
                                row.actualCompleteTime = Date.parse(new Date());
                            }
                        } else {
                            if (null != row) {
                                row.actualCompleteTime = '';
                            }
                        }
                    }
                }
            }
        };
        columns.push(obj);
    }
    defaults.columns = [columns];

    var settings = jQuery.extend(true, {}, defaults);
    $(el.measuresTb).edatagrid(settings);
}


/**
 * 初始化审批日志表格
 *
 * @param noticeId 整改通知单ID
 */
function initLogDataGrid(noticeId) {
    $(el.tbLog).datagrid({
        url: g.ctx + 'pc/spe/rectifiedNotice/rest/examineLogList',
        method: "GET",
        queryParams: {
            noticeId: noticeId
        },
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: true,
        pagination: false,
        checkbox: true,
        rownumbers: true,
        height: 400,
        columns: [[
            {
                field: 'noticeOrderId',
                hidden: true
            },
            {
                field: 'examineStatus',
                title: '审核状态',
                width: 120,
                formatter: function (value) {
                    if (value === 1) {
                        return '<span style="color: green;">审批通过</span>';
                    }
                    if (value === 2) {
                        return '<span style="color: red;">拒绝</span>';
                    }
                    return '';
                }
            },
            {
                field: 'examineUserCode',
                title: '审核人工号',
                width: 130
            },
            {
                field: 'examineUserName',
                title: '审核人姓名',
                width: 140
            },
            {
                field: 'examineView',
                title: '审核意见',
                width: 300
            },
            {
                field: 'examineTime',
                title: '审核时间',
                width: 140,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:ss');
                    }
                    return '';
                }
            }
        ]]
    });
}

/**
 * 查看整改措施上附件信息
 *
 * @param measuresId 措施ID
 */
function lookAttachment(fileIds) {
    openFileUploadDialog("noticeFileUploadDialog", {
        title: "附件查看",
        fileIds: fileIds,
        lookupFlag: 1
    })
}


/**
 * 保存整改数据
 *
 * @param callback 回调函数
 */
function saveExamineData(callback) {
    var noticeId = $(el.noticeId).val();
    var examineStatus = $(el.examineStatus).combobox('getValue');
    var examineView = $(el.examineView).val();
    var optType = $(el.optType).val();
    if (optType === '2') {
        if (!examineStatus) {
            layer.msg('请选择审核状态', {icon: 0});
            return false;
        }
        if (!examineView) {
            layer.msg('请填写审批意见', {icon: 0});
            return false;
        }
    }
    var rows = $(el.measuresTb).edatagrid('getRows');
    $.each(rows, function (index, item) {
        $(el.measuresTb).edatagrid('endEdit', index);
    });

    var updatedRows = $(el.measuresTb).edatagrid('getChanges', 'updated');

    var result = [];
    for (var n = 0; n < updatedRows.length; n++) {
        var ob = {}, item = updatedRows[n];
        ob.id = item.id;
        ob.noticeOrderId = item.noticeOrderId;
        ob.status = item.status;
        if (item.actualCompleteTime) {
            ob.actualCompleteTime = moment(item.actualCompleteTime).format('YYYY-MM-DD HH:mm:ss');
        } else {
            ob.actualCompleteTime = '';
        }

        result.push(ob);
    }
    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/spe/rectifiedNotice/rest/submitExamine',
        contentType: 'application/json',
        data: JSON.stringify({
            noticeId: noticeId,
            examineStatus: examineStatus,
            examineView: examineView,
            trackFlag: optType === '1' ? 1 : 0,
            measures: result
        }),
        success: function () {
            topWin.layer.msg('操作成功', {icon: 1, time: 500}, function () {
                callback();
            });
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message)
        }
    });
}