package controller

import (
	"bswas/utilities"
	"encoding/json"
)

func WaybillFormat(data []map[string]string) ([]interface{}, [][]interface{}) {
	row := []interface{}{
		"运单类型", "货主", "上游单号", "wms订单号", "运单号", "运输收费",
		"其他收费", "其他收费备注", "始发地", "目的地", "承运商", "承运商单号",
		"仓库计抛", "承运商计抛", "仓库计重", "承运商计重", "车型", "车辆信息",
		"计件", "里程数", "运费(元)", "提货费(元)", "其他费用(元)", "发货时间",
		"调度备注", "创建时间",
	}
	column := map[string]int{
		"type" : 0,
		"owner_name" : 1,
		"source_bill" : 2,
		"wms_bill_number" : 3,
		"waybill_number" : 4,
		"charge" : 5,
		"other_charge" : 6,
		"other_charge_remark" : 7,
		"origination" : 8,
		"destination" : 9,
		"carrier_name" : 10,
		"carrier_bill" : 11,
		"warehouse_weight" : 12,
		"carrier_weight" : 13,
		"warehouse_weight_other" : 14,
		"carrier_weight_other" : 15,
		"car_type_name" : 16,
		"car_owner_info" : 17,
		"amount" : 18,
		"mileage" : 19,
		"fee" : 20,
		"pick_up_fee" : 21,
		"other_fee" : 22,
		"deliver_at" : 23,
		"dispatch_remark" : 24,
		"created_at" : 25,
	}
	list := make([][]interface{},len(data))
	for k,v := range data{
		line := make([]interface{},len(row))
		for key,value := range column {
			if key == "created_at" {
				line[value] = utilities.DateFormat(v[key],"2006-01-02T15:04:05Z")
				continue
			}
			line[value] = v[key]
		}
		list[k] = line
	}
	return row,list
}

func WaybillDeliveringFormat(data []map[string]string) ([]interface{}, [][]interface{}) {
	row := []interface{}{
		"日期", "承运商", "宝时运单号", "提货仓", "货主", "预估重量",
		"预估体积", "状态", "专线运单号", "查件电话", "件数", "重量",
		"体积",
	}
	column := map[string]int{
		"created_at" : 0,
		"carrier_name" : 1,
		"waybill_number" : 2,
		"origination" : 3,
		"owner_name" : 4,
		"warehouse_weight_other" : 5,
		"warehouse_weight" : 6,
		"status" : 7,
		"carrier_bill" : 8,
		"inquire_tel" : 9,
		"amount" : 10,
		"carrier_weight_other" : 11,
		"carrier_weight" : 12,
	}
	list := make([][]interface{},len(data))
	for k,v := range data{
		line := make([]interface{},len(row))
		for key,value := range column {
			if key == "status" {
				if v[key] == "已完结" {
					line[value] = "已完成"
				}else{
					if v["carrier_bill"] != "" {
						line[value] = "已提交"
					}else{
						line[value] = "待提交"
					}
				}
				continue
			}
			if key == "created_at" {
				line[value] = utilities.DateFormat(v[key],"2006-01-02T15:04:05Z")
				continue
			}
			line[value] = v[key]
		}
		list[k] = line
	}
	return row,list
}
func WaybillFinancialFormat(data []map[string]string) ([]interface{}, [][]interface{}) {
	row := []interface{}{
		"运单类型", "运单号", "货主", "WMS单号", "始发地", "目的地", "收件人", "收件人电话",
		"收费(元)", "下单备注", "承运商", "承运商单号", "始发市", "目的市", "仓库计数(抛)", "仓库计数二",
		"承运商计数(抛)", "承运商计数二", "车型", "车辆信息","运费(元)", "提货费(元)", "其他费用(元)", "到付金额(元)",
		"调度备注", "终审人员", "运单创建时间", "应收款(元)", "应付款(元)", "毛利(元)",
		"毛利率(元)", "报表生成时间",
	}
	column := map[string]int{
		"type" : 0,
		"waybill_number" : 1,
		"owner_name" : 2,
		"wms_bill_number" : 3,
		"origination" : 4,
		"destination" : 5,
		"recipient" : 6,
		"recipient_mobile" : 7,
		"charge" : 8,
		"ordering_remark" : 9,
		"carrier" : 10,
		"carrier_bill" : 11,
		"origination_city" : 12,
		"destination_city" : 13,
		"warehouse_weight" : 14,
		"warehouse_weight_other" : 15,
		"carrier_weight" : 16,
		"carrier_weight_other" : 17,
		"car_type_name" : 18,
		"car_owner_info" : 19,
		"fee" : 20,
		"pick_up_fee" : 21,
		"other_fee" : 22,
		"collect_fee" : 23,
		"dispatch_remark" : 24,
		"auditLog_user_name" : 25,
		"created_at" : 26,
		"total_receivable" : 27,
		"total_expense" : 28,
		"gross_margin" : 29,
		"gross_profit_rate" : 30,
		"snapshotCreated_at" : 31,
	}
	list := make([][]interface{},len(data))
	for k,v := range data{
		m := make(map[string]string)
		err := json.Unmarshal([]byte(v["json_content"]),&m)
		if err != nil {
			utilities.WriteLog("/api/controller/waybillController.go:145  运单财务记录内字段json解析失败!","ERROR")
		}
		line := make([]interface{},len(row))
		for key,value := range column {
			if key == "snapshotCreated_at" {
				line[value] = utilities.DateFormat(v["created_at"],"2006-01-02T15:04:05Z")
				continue
			}
			if key == "created_at"{
				line[value] = utilities.DateFormat(m[key],"2006-01-02T15:04:05Z")
				continue
			}
			line[value] = m[key]
		}
		list[k] = line
	}
	return row,list
}
