var el = {
    searchForm: '#searchForm',
    tb: '#tb',
    purchaseGroupCode: '#purchaseGroupCode',
    status: '#status',
    type: '#type',
    customs_declaration_add: '#customs_declaration_add',
    customs_declaration_edit: '#customs_declaration_edit',
    customs_declaration_search: '#customs_declaration_search',
    customs_declaration_push_confirm: '#customs_declaration_push_confirm',
    btn_remove: '#btn_remove',
    bodyHeight: document.documentElement.clientHeight
};
$(function () {
    initDict('unit,currency,payment_method,bg_type')
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll');
    initCombo();
    loadGrid();
});

function initCombo(){
    $(el.status).combobox({
        editable: false,
        panelHeight: 'auto',
        valueField: 'value',
        textField: 'label',
        prompt: '--- 请选择 ---',
        data: [
            {label: '暂存', value: 1},
            {label: '待财务确认', value: 7},
            {label: '财务驳回', value: 8},
            {label: '已确认', value: 9}
        ],
    });
    $(el.type).combobox({
        panelHeight: 'auto',
        limitToList: 'true',
        prompt: '-- 请选择 --',
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        data: getDictList('bg_type')
    });

    $(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/finance/customs/declaration/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',
                exportEnum:[
                    {code:1,codeWithDesc:'暂存'},
                    {code:7,codeWithDesc:'待财务确认'},
                    {code:8,codeWithDesc:'财务拒绝'},
                    {code:9,codeWithDesc:'已确认'},
                ],
                formatter: function (value) {
                    switch (value) {
                        case 1:
                            return '暂存';
                        case 2:
                            return '<span style="color: blue;">待采购确认</span>';
                        case 3:
                            return '<span style="color: red;">采购拒绝</span>';
                        case 7:
                            return '<span style="color: orange;">待财务确认</span>';
                        case 8:
                            return '<span style="color: red;">财务驳回</span>';
                        case 9:
                            return '<span style="color: green;">已确认</span>';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'type',
                title: '单据类型',
                width: '120',
                exportEnum:getDictList('bg_type'),
                formatter: function (value) {
                    return getDicName('bg_type',value);
                }
            },
            {
                field: 'code',
                title: '单据流水号',
                width: '120',
                align: 'left'
            },
            {
                field: 'entrustNo',
                title: '委托单号',
                width: '120',
                align: 'left'
            },
            {
                field: 'purchaseGroupCode',
                title: '采购组',
                width: '120',
                align: 'left',
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
                }
            },
            {
                field: 'costMatchingFlag',
                title: '是否存在费用匹配单',
                width: '140',
                align: 'left',
                formatter: function (value) {
                    if (isBlank(value)){
                        return '否';
                    }
                    if (Number(value)===0){
                        return '否';
                    }else {
                        return '<span style="color: green;">是</span>';
                    }
                }
            },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: '100',
                align: 'left'
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: '200',
                align: 'left'
            },
            {
                field: 'taxAmount',
                title: '进口总金额',
                width: '100',
                align: 'left'
            },
            // {
            //     field: 'currencyCode',
            //     title: '币种',
            //     width: '120',
            //     align: 'left'
            // },
            {
                field: 'remark',
                title: '备注',
                width: '120',
                align: 'left'
            },
            {
                field: 'attachmentFiles', title: '附件', width: 120, align: 'left', formatter: function (value, row) {
                    if (value !== "") {
                        return "<a href='javascript:void(0)' onclick=\"viewAttachmentFileDetail('" + row.id + "', '" + row.code +"')\">" + "点击查看(已上传)" + "</a>";
                    } else {
                        return "<a style='color:red;' href='javascript:void(0)' onclick=\"viewAttachmentFileDetail('" + row.id + "', '" + row.code +"')\">" + "点击上传(未上传)" + "</a>";
                    }
                }
            },
            {
                field: 'companyCode',
                title: '交易主体',
                width: '120',
                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:'customs_declaration_add',
                handler: function () {
                    detail(0);
                }
            },
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id:'customs_declaration_edit',
                handler: function () {
                    detail(1);
                }
            },"-",{
                text: '查看',
                iconCls: 'icon-search',
                id:'customs_declaration_search',
                handler: function () {
                    detail(2);
                }
            }
            ,"-",{
                text: '提交确认',
                iconCls: 'icon-ok',
                id: 'customs_declaration_push_confirm',
                handler: function () {
                    pushConfirm();
                }
            },
            "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                id: 'btn_remove',
                handler: function () {
                    remove();
                }
            },
            "-",
            {
                text: '附件上传',
                iconCls: 'icon-add',
                id: 'btn_attachment_file',
                handler: function () {
                    let rows = $(el.tb).datagrid('getChecked');
                    if (rows.length !== 1) {
                        layer.msg("请选中一条记录");
                        return;
                    }
                    openCommonFileDialog('commonFileDialog',{
                        //业务类型编码
                        title:'报关单附件上传',
                        //业务类型编码
                        businessTypeCode:eSignBusinessTypeCodeEnum.CUSTOMS_DECLARATION,
                        businessCode:rows[0].code,
                        //业务唯一id
                        businessId:rows[0].id,
                        callBack : function () {
                            $(el.tb).datagrid('reload');
                        },
                    });
                }
            }
        ],
        onBeforeLoad:function () {
            displayButtonByUserRole('customs_declaration_list');
        },
        onCheck: function (rowIndex) {
            var rows = $(el.tb).datagrid('getRows');
            var status = rows[rowIndex].status;
            //数据状态 1:暂存 2:待采购确认 3:采购拒绝 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认 8:财务拒绝 9:确认通过
            switch (Number(status)) {
                case 1:
                    $(el.customs_declaration_edit).linkbutton('enable');
                    $(el.customs_declaration_push_confirm).linkbutton('enable');
                    $(el.btn_remove).linkbutton('enable');
                    break;
                case 7:
                    $(el.customs_declaration_edit).linkbutton('disable');
                    $(el.customs_declaration_push_confirm).linkbutton('disable');
                    $(el.btn_remove).linkbutton('disable');
                    break;
                case 8:
                    $(el.customs_declaration_edit).linkbutton('enable');
                    $(el.customs_declaration_push_confirm).linkbutton('enable');
                    $(el.btn_remove).linkbutton('enable');
                    break;
                case 9:
                    $(el.customs_declaration_edit).linkbutton('disable');
                    $(el.customs_declaration_push_confirm).linkbutton('disable');
                    $(el.btn_remove).linkbutton('disable');
                    break;
                default:
                    break;
            }
        }
    });
}


/**
 * 删除
 */
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/finance/customs/declaration/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: '温馨提示'});
            }
        });
    });
}

/**
 * 提交
 */
function pushConfirm() {
    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/finance/customs/declaration/rest/pushConfirm';
        $.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: '温馨提示'});
            }
        });
    });
}


