/**
 * 发票管理
 */
var el = {
    cartId: '#cartId',

    receivingUser: '#receivingUser',
    receivingTel: '#receivingTel',
    provinceCityCounty: '#provinceCityCounty',
    address: '#address',

    purchaseOrg: '#purchaseOrg',
    purchaseGroup: '#purchaseGroup',
    factoryCode: '#factoryCode',
    centerCode: '#centerCode',
    remark: '#remark',

    invoiceState: '#invoiceState',
    invoiceType: '#invoiceType',
    invoiceTitle: '#invoiceTitle',
    invoiceNo: '#invoiceNo',
    invoiceAddress: '#invoiceAddress',
    invoiceTel: '#invoiceTel',
    openingBank: '#openingBank',
    bankAccount: '#bankAccount',
    bankTel: '#bankTel',
    bankAddress: '#bankAddress',
    content: '#content',


    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    findHeadData();

    loadGrid();
});


/**
 * 加载明细表
 */
function loadGrid() {
    $(el.tb).datagrid({
        url: '/pc/mall/cart/rest/detailPO/list?cartId=' + $(el.cartId).val(),
        height: el.bodyHeight - 400,
        pagination: false,
        queryParams: {},
        rownumbers: true,
        remoteSort: false,
        columns: [
            [
                /*{
                    field: 'ck',
                    checkbox: true
                },*/
                {
                    field: 'deliveryStatus',
                    title: '发货状态',
                    width: 90,
                    exportEnum:[
                        {code:1,codeWithDesc:'已发货'},
                        {code:2,codeWithDesc:'签收'},
                        {code:3,codeWithDesc:'拒收'},
                        {code:0,codeWithDesc:'待发货'},
                    ],
                    formatter: function (value, row) {
                        //商城订单发货状态：发货状态（0-待发货，1-已发货，2-签收，3-拒收）
                        switch (Number(value)) {
                            case 0:
                                return '<span style="color: #050300;">待发货</span>';
                            case 1:
                                return '<span style="color: #3d49d5;">已发货</span>';
                            case 2:
                                return '<span style="color: #49b003;">签收</span>';
                            case 3:
                                return '<span style="color: red;">拒收</span>';
                            default:
                                return '--';
                        }
                    }
                },
                {
                    field: 'storeName',
                    title: '商城名称',
                    width: 90
                },
                {
                    field: 'skuName',
                    title: '商品名称',
                    width: 400,
                    formatter: function (value) {
                        return '<span style="display: inline-block;width: 450px;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;" title="' + value + '">' + value + '</span>';
                    }
                },
                {
                    field: 'unitCost',
                    title: '单价',
                    width: 70,
                    sortable: true
                },
                {
                    field: 'unit',
                    title: '单位',
                    width: 70
                },
                {
                    field: 'qty',
                    title: '商品数量',
                    width: 70
                },
                {
                    field: 'requireNum',
                    title: '请购数量',
                    width: 70
                },
                {
                    field: 'receiptNum',
                    title: '收货数量',
                    width: 70,
                    formatter: function (value, row) {
                        if (Number(value) === 0) {
                            return '<a style="color: red">' + value + '</a>'
                        } else {
                            return value
                        }
                    }
                },
                {
                    field: 'returnQuatity',
                    title: '退货数量',
                    width: 70,
                },
                {
                    field: 'receivedTime',
                    title: '收货时间',
                    width: '120px',
                    align: 'left',
                    formatter: function (value, row, index) {
                        return DateFormatMs(value);
                    }
                },
                {
                    field: 'prCode',
                    title: '采购申请单号',
                    width: 210
                },
                {
                    field: 'purchaseOrderNumber',
                    title: 'OA流程单号',
                    width: 210
                },
                {
                    field: 'packageId',
                    title: '发货单号',
                    width: 140,
                    formatter: function (value, row) {
                        if(value !=null && value!=''){
                            return "<a href='javascript:void(0)' onclick=\"lookUpDetail('" + value + "')\">" + value + "</a>";
                        }else{
                            return '';
                        }
                    }

                },{
                field: 'invoiceNumber',
                title: '发票号',
                width: 210
            },
                {
                    field: 'note',
                    title: '备注',
                    width: 200,
                    sortable:true,
                    formatter: function(value, row, index) {
                        return '<a href="#" style="color: black" title="' + value + '" class="note">' + value + '</a>';
                    }
                    // formatter: function (value, row) {
                    //     return openNoteDialog(value)
                    // }
                    // formatter: function (value, row) {
                    //
                    //     return "<a href='javascript:void(0)' onclick=\"editNote('" + row.id + "')\">" + value + "</a>";
                    // }
                },
            ]
        ]
    });
}


/**
 * 获取表头数据并赋值
 */
function findHeadData() {
    var loading = topWin.layer.msg('数据加载中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
    $.ajax({
        type: 'POST',
        url: '/pc/mall/cart/rest/find/by/id',
        data: {cartId: $(el.cartId).val()},
        success: function (data) {
            topWin.layer.close(loading);
            handleHeadData(data);
        },
        error: function (data) {
            topWin.layer.close(loading);
            var errorMsg = data.responseJSON;
            if (!errorMsg) {
                errorMsg = "程序异常, 请联系管理员";
            }
            topWin.layer.alert(errorMsg, {icon: 2});
        }
    });
}


/**
 * 处理表头数据
 *
 * @param headData 表头数据
 */
function handleHeadData(headData) {
    $(el.receivingUser).textbox('setValue', headData.receivingUser);
    $(el.receivingTel).textbox('setValue', headData.receivingTel);
    $(el.provinceCityCounty).textbox('setValue', headData.provinceName + '/' + headData.cityName + '/' + headData.countyName);
    $(el.address).textbox('setValue', headData.address);

    $(el.purchaseOrg).textbox('setValue', headData.pgoName);
    $(el.purchaseGroup).textbox('setValue', headData.pgName);
    $(el.factoryCode).textbox('setValue', headData.factoryName);
    $(el.centerCode).textbox('setValue', headData.centerCode);
    $(el.remark).textbox('setValue', headData.remark);

    $(el.invoiceState).textbox('setValue', Number(headData.invoiceState) === 1 ? '随单开票' : '集中开票');
    $(el.invoiceType).textbox('setValue', Number(headData.invoiceType) === 1 ? '电子普通发票' : '增值税专用发票');
    $(el.invoiceTitle).textbox('setValue', headData.invoiceTitle);
    $(el.invoiceNo).textbox('setValue', headData.invoiceNo);
    $(el.invoiceAddress).textbox('setValue', headData.invoiceAddress);
    $(el.invoiceTel).textbox('setValue', headData.invoiceTel);
    $(el.openingBank).textbox('setValue', headData.openingBank);
    $(el.bankAccount).textbox('setValue', headData.bankAccount);
    $(el.bankTel).textbox('setValue', headData.bankTel);
    $(el.bankAddress).textbox('setValue', headData.bankAddress);
    $(el.content).textbox('setValue', headData.content);
}

function lookUpDetail(packageId) {
    var url = g.ctx + "pc/mall/cart/page/orderTrackDetail?packageId=" +packageId;
    var deliveryCreateDialog =openDialog("deliveryCreateDialog", url, {
        title: '发货单物流查看',
        width: '50%',
        height: '80%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            deliveryCreateDialog.dialog('destroy');
        },
        buttons: [{
            text: '关闭', iconCls: 'icon-cancel', handler: function () {
                deliveryCreateDialog.dialog('close');
            }
        }]
    })

}


