/**
 * view/supplier/product/demo/detail.ftl
 */
var el = {
    examineRemark: '#examineRemark',
    checkName: '#checkName',
    purchaseGroupCode: '#purchaseGroupCode',
    sampleType: '#sampleType',
    sendTarget: '#sendTarget',
    materialCode: '#materialCode',
    materialGroupCode: '#materialGroupCode',
    form: '#form'
};

/**
 * 文档就绪函数
 */
$(function () {
    if (readonly()) {
        // 设置所有 datebox 为只读模式
        $('.easyui-datebox').datebox('readonly', true);

        // 设置所有 NumberBox 为只读模式
        $('.easyui-numberbox').numberbox('readonly', true);

        // 设置所有 TextBox 为只读模式
        $('.easyui-textbox').textbox('readonly', true);

        // 设置所有 ComboBox 为只读模式
        $('.easyui-combobox').combobox('readonly', true);
    }
    initItTable()
    //需要获取oa的附件
    if (oaFile !== '') {
        let fileHtml = []
        fileHtml.push('<div class="file-content file-content-auto">')
        $.each(oaFile, function (i, item) {
            let html = []
            html.push('<span class="file-content-span">')
            html.push('<div class="file-content-span-info">')
            html.push('<div class="img-div"><span class="file-icon file-icon-' + item.fileName.split('.').pop() + '"></span></div>')
            html.push(' <div class="file-name"><span>' + item.fileName + '</span></div><div></div></div>')
            html.push('<div class="file-content-span-preview">')
            html.push('<a onclick="oaPreviewFileForm(\'' + item.url + '\')">预览</a>')
            html.push('</div>')
            html.push('</span>')
            fileHtml.push(html.join(''))
        })
        fileHtml.push('</div>')
        setTimeout(function () {
            $("#oaFilIDiv").append(fileHtml.join(''))
        }, 100)
    }
});

function oaPreviewFileForm(url) {
    $.ajax({
        url: g.ctx + "pc/sys/oaTrack/rest/file",
        data: {url: url},
        type: "POST",
        success: function (data) {
            window.open(data);
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    })
}

function readonly() {
    if (type === 0) {
        return true
    }
    return false;
}

/**
 * 选择员工
 */
function selectUser() {
    topWin.layer.open({
        title: '抄送人选择',
        skin: 'layui-layer-molv',
        type: 2,
        area: ['80%', '85%'],
        content: '/delivery/basis/dialog/user/select/page/plus',
        btn: ['确定'],
        yes: function (index, layero) {
            let iframe = parent.window[layero.find('iframe')[0]['name']];
            var rows = iframe.page.getChecked();
            var row = rows[0];
            $(el.form).form('load', {
                checkUserCode: row.workNo,
                checkName: row.name,
                checkDepartment: row.orgName
            });
            topWin.layer.close(index);
        }
    })
}

function initItTable() {
    var toolBar = [];
    toolBar.push(
        {
            text: '导出',
            iconCls: 'icon-download',
            handler: function () {
                detailExport();
            }
        });
    var columns = [[
        {
            field: 'ck',
            checkbox: true
        },
        {
            field: 'materialCode',
            title: "物料编码",
            align: 'left',
            width: 130
        },
        {
            field: 'materialName',
            title: "物料名称",
            align: 'left',
            width: 350
        },
        {
            field: 'materialGroupCode',
            title: '物料组编码',
            align: 'left',
            width: 120
        },
        {
            field: 'materialGroupName',
            title: '物料组描述',
            align: 'left',
            width: 300
        },
        {
            field: 'envProtectionFlag',
            title: "是否需要处理环保",
            align: 'left',
            width: 115,
            formatter: function (tt) {
                var value = tt + ''
                if (value === '1') {
                    return "是"
                } else if (value === '0') {
                    return "否"
                } else {
                    return ""
                }
            }
        },
        {
            field: 'needCount',
            title: "需求送样数量",
            align: 'left',
            width: 80,
        },
        {
            field: 'sampleCount',
            title: "实际送货数量",
            align: 'left',
            width: 80
        },
        {
            field: 'expressNumber',
            title: "快递单号",
            align: 'left',
            width: 80,
            editor: {
                type: 'textbox',
                options: {
                    prompt: "请输入快递单号"
                }
            }
        }
    ]];
    $("#itb").datagrid({
        iconCls: 'icon-list',
        height: 200,
        method: 'GET',
        url: g.ctx + 'pc/supplier/materialDemo/rest/listByHeader?id=' + $("#id").val(),
        singleSelect: false,
        pagination: false,
        autoSave: true,
        loadMsg: commonMsg.loadData,
        rownumbers: true,
        fitColumns: false,
        columns: columns,
        toolbar: toolBar
    })
}

function confirm(call) {

    topWin.layer.confirm('确定要提交吗？', {icon: 3}, function (iindex) {
        var index = topWin.layer.load(1, {shade: [0.1, '#eee']});
        $.ajax({
            url: g.ctx + "pc/supplier/materialDemo/rest/confirm",
            type: "POST",
            data: {id: id},
            complete: function () {
                topWin.layer.close(index);
            },
            success: function (data) {
                topWin.layer.close(iindex)
                call()
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        })
    })

}

/**
 * 审批 3.审核通过 4.审核不通过
 */
function examine(state, call) {
    var examineRemark = $(el.examineRemark).val();
    if (state === 6 && !examineRemark) {
        topWin.layer.msg('审核拒绝请填写备注');
        return false;
    }
    // let examineFileIds = $("#examineFileIds").attr('data-fileIds');
    // if (!examineFileIds) {
    //     topWin.layer.msg('请上传审核附件');
    //     return false;
    // }
    var index = topWin.layer.load(1, {shade: [0.1, '#eee']});
    $.ajax({
        url: g.ctx + "pc/supplier/materialDemo/rest/examine",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify({
            id: id,
            state: state,
            examineRemark: examineRemark
        }),
        complete: function () {
            topWin.layer.close(index);
        },
        success: function (data) {
            call()
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    })
}

function detailExport() {
    debugger;
    var rows =$("#itb").datagrid('getRows');
    rows.forEach(function(row){
        if( row.envProtectionFlag == 1){
            row.envProtectionFlag = "是";
        }else if(row.envProtectionFlag == 0){
            row.envProtectionFlag = "否";
        }
    });
    $("#itb").datagrid('toExcel', {
        filename: '物料明细数据.xls',
        rows: rows,
        worksheet: '物料明细'
    });
}

