import http from '../util/http'
import upload from '../util/uniAppUpload.js'
import store from '../store/store'
import * as types from '../store/mutations-types'
import {
	defaultPageSize
} from '../util/config'

/**
 * 登录接口
 * @param mobile 手机号码
 * @param password 手机号码
 * @returns {Promise}
 */
export const login = (username, password, sign='') => {
  return http('/system/pda-login', {
    username,
    password,
    sign
  }, {
    method: 'post',
    loading: true
  }, 'login').then((data) => {
    store.commit(types.SET_USER_TOKEN, data.token)
    // getUserInfo(data.token)
    // return data
  })
}

/**
 * 注册并登录接口
 * @param code 短信验证码
 * @param mobile 手机号码
 * @param password 密码
 * @returns {Promise}
 */
export const signUp = (code, mobile, password) => {
  return http('/app-api/member/reg', {
    code,
    mobile,
    password
  }, {
    method: 'post',
    loading: true
  }).then((data) => {
    store.commit(types.SET_USER_TOKEN, data.token)
    // getUserInfo(data.token)
    // return data
  })
}

/**
 * 发送手机验证码
 * @param mobile 手机号码
 * @param scene 发送场景(0注册1登录2修改密码3忘记密码),示例值(1)
 * @returns {Promise}
 */
export const sendSmsCode = (mobile, scene) => {
  return http('/app-api/member/send-sms-code', {
    mobile,
    scene
  }, {
    method: 'post',
    loading: true
  })
}

/**
 * 获取用户资料
 * @returns {Promise}
 */
export const getUserInfo = (userToken) => {
	userToken = userToken || store.getters.userToken()
	return http('/member/getMember', {
		token: userToken
	}, {
		method: 'post'
	}).then((data) => {
		let userInfo = {
			id: data.id, //id
			username: data.username, //用户名
			name: data.name_zh, //名称
			number: data.number, //工号
			mobile: data.phone, //手机
			originId: data.originId,
			destinationId: data.destinationId,
		}

		store.commit(types.SET_USER_INFO, userInfo)
	})
}

/**
 * 未入仓订单
 */
export const getWarehousingOrder = (page, keyword, originId = [], destinationId = []) => {
	return http('/order/getWarehousingOrder', {
		page: page,
		keyword: keyword,
		originId: originId,
		destinationId: destinationId,
	}, {
		method: 'post',
	})
}

/**
 * 订单商品
 */
export const getOrderGoods = (id) => {
	return http('/order/getOrderGoods', {
		id: id,
	}, {
		method: 'post',
		// loading: true
	})
}

/**
 * 订单入仓商品
 */
export const getOrderWarehouseGoods = (id) => {
	return http('/order/getOrderWarehouseGoods', {
		id: id,
	}, {
		method: 'post',
		// loading: true
	})
}

/**
 * 查询品牌&备案
 */
export const getBrand = (name) => {
	return http('/brand/getBrand', {
		name: name
	}, {
		method: 'post',
		loading: true
	})
}

/**
 * 查询商品表
 */
export const getGoods = (name, type) => {
	return http('/goods/getGoods', {
		name: name,
		type: type
	}, {
		method: 'post',
		loading: true
	})
}

/**
 * 商品入仓（添加or修改）
 */
export const editWarehouseGoods = (orderId, goodsId, warehouse, warehouseId = null,remarks = '',add_type) => {
	return http('/warehousing/editWarehousing', {
		orderId: orderId,
		goodsId: goodsId,
		warehouse: warehouse,
		warehouseId: warehouseId,
		remarks:remarks,
		add_type:add_type?1:0
	}, {
		method: 'post',
		loading: true
	})
}

/**
 * 新商品入仓
 */
export const addNewWarehouseGoods = (orderId, orderGoods, warehouse,remarks = '',add_type) => {
	return http('/warehousing/addNewGoodsWarehousing', {
		token: store.getters.userToken(),
		orderId: orderId,
		orderGoods: orderGoods,
		warehouse: warehouse,
		remarks:remarks,
		add_type:add_type?1:0
	}, {
		method: 'post',
		loading: true
	})
}

/**
 * 删除入仓商品
 */
export const delWarehouseGoods = (id) => {
	return http('/warehousing/delWarehousing', {
		id: id,
	}, {
		method: 'post',
		loading: true
	})
}

/**
 * 完成入仓
 */
export const orderWarehousing = (orderId,is_handler,handler_type,handler_desc,money,image_url,label,locationId,wareId,areaId) => {
	let formData={
		token: store.getters.userToken(),
		id: orderId,
		money:money,
		label:label,
		locationId:locationId,
		wareId:wareId,
		areaId:areaId
	};
	if(is_handler){
		formData.is_handler = 1;
		formData.handler_type = handler_type;
		formData.handler_desc = handler_desc;
		formData.image_url = image_url;
	}
	return http('/order/orderWarehousing',formData, {
		method: 'post',
		loading: true,
		// edit by knight 20210819 设置请求头
		headers: {'Content-Type':'application/json'},
		keepObject:true
	})
}

/**
 * 获取目的地
 */
export const getEndWarehouseFromStartId = (startId) => {
	return http('/warehouse/getEndWarehouseFromStartId', {
		startId: startId,
	}, {
		method: 'post',
	})
}

// 获得仓库列表
export function getWarehouseList(params) {
  return http('/ecw/warehouse/list', params, {
    method: 'GET',
  });
}

export function getCounty(params) {
	return http('/ecw/region/getListTree', params, {
		method: 'GET'
	})
}

/*************************** 装柜 *********************************/

/**
 * 包装类型
 */
export const getPackageTypeList = () => {
	return http('/order/packageTypeList', {

	}, {
		method: 'post',
	})
}

/**
 * 转异-原因
 */
export const getHandlerType = () => {
	return http('/warehousing/handler_type', {

	}, {
		method: 'post',
	})
}


/**
 * 上传图片
 */
export const updateImg = (file) => {
	return upload('/upload/thumb', file , {
		token: store.getters.userToken(),
	}, {
		method: 'post',
		loading: true
	}).then((data) => {
		return data;
	})
}

/**
 * 文件删除
 */
export const deleteImg = (path) => {
	return http('/upload/delete', {
		path:path,
		token: store.getters.userToken()
	}, {
		method: 'post',
		loading: true,
		errorHandle: false,
	})
}

/**
 * 获取审批人
 */
export const getApproval = () => {
	return http('/member/approval', {

	}, {
		method: 'post',
	})
}

/**
 * 获取材质
 */
export const getMaterialLlist = () => {
	return http('/Goods/getMaterialLlist', {

	}, {
		method: 'post',
	})
}

/**
 * 订单调仓接口
 */
export const transferWarehouses = (arrival_date,transfer_order) => {
	return http('/order/transferWarehouses', {
		token: store.getters.userToken(),
		arrival_date: arrival_date,
		transfer_order:transfer_order
	}, {
		method: 'post',
	})
}

/**
 * 仓位接口
 */
export const getLocationSelect = (orderId) => {
	return http('/order/getLocationSelect', {
		token: store.getters.userToken(),
		orderId:orderId
	}, {
		method: 'post',
	})
}

/**
 * 订单仓位修改
 */
export const editAOrderLocation = (orderId,wareId,areaId,locationId) => {
	return http('/Warehousing/editAOrderLocation', {
		token: store.getters.userToken(),
		orderId:orderId,
		locationId:locationId,
		wareId:wareId,
		areaId:areaId,
	}, {
		method: 'post',
	})
}

/**
 * 入仓单的打印接口
 */
export const getInboundData = (orderId,goodsId) => {
	return http('/Warehousing/getInboundData', {
		token: store.getters.userToken(),
		orderId:orderId,
		goodsId:goodsId
	}, {
		method: 'post',
		loading: true,
		errorHandle: false,
	})
}

/**
 * 打印机配置接口
 */
export const getPrinterConf = (orderId) => {
	return http('/Printer/getPrinterConf', {
		// token: store.getters.userToken(),
		// orderId:orderId
	}, {
		method: 'post'
	})
}

/**
 * 入仓时间
 */
export const getPrintSelectTime = (orderId,goodsId) => {
	return http('/Warehousing/getPrintSelectTime', {
		token: store.getters.userToken(),
		orderId:orderId,
		goodsId:goodsId
	}, {
		method: 'post',
		loading: true,
		errorHandle: false,
	})
}

/**
 * 发货人证件查询
 */
export const certificateSearch = (key) => {
	return http('/ecw/order/certificateSearch', {
		key
	}, {
		method: 'get',
		loading: true,
		errorHandle: false,
	})
}


/**
 * 获得出货分页
 */
export const shipmentBoxPage = (params) => {
	return http(`/shipment/box/page`, params, {
		method: 'GET',
	});
}