package controllers

import (
	"fmt"
	"io"

	"github.com/astaxie/beego/server/web"

	"git.rosy.net.cn/jx-callback/business/jxcallback/orderman"
	"git.rosy.net.cn/jx-callback/business/jxstore/financial"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/legacymodel"
)

type FinancialController struct {
	web.Controller
}

// @Title 发送文件给门店
// @Description 发送文件给门店，调用GET方法得到浏览器端参考的上传HTML实现，userfiles
// @Param	token			header	string	true	"认证token"
// @Param	title			query	string	false	"消息标题"
// @Param	shopName		query	string	false	"平台菜市名称"
// @Param	isAsync			query	bool	false	"是否异步，缺省是同步"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendFilesToStores [post,get]
func (c *FinancialController) SendFilesToStores() {
	if c.Ctx.Input.IsGet() {
		w := c.Ctx.ResponseWriter
		// 上传页面
		w.Header().Add("Content-Type", "text/html")
		w.WriteHeader(200)
		html := `
<form enctype="multipart/form-data" action="/v2/financial/SendFilesToStores" method="POST">
    Send this file: <input name="userfiles" multiple accept="*" type="file" />
    <input type="submit" value="Send File" />
</form>
`
		io.WriteString(w, html)
	} else if c.Ctx.Input.IsPost() {
		c.callSendFilesToStores(func(params *tFinancialSendFilesToStoresParams) (retVal interface{}, errCode string, err error) {
			r := c.Ctx.Request
			files := r.MultipartForm.File["userfiles"]
			retVal, err = financial.SendFilesToStores(params.Ctx, files, params.Title, params.ShopName, params.IsAsync, params.Ctx.GetUserName())
			return retVal, "", err
		})
	}
}

// @Title 查询门店账单
// @Description 查询门店账单
// @Param	token			header	string	true	"认证token"
// @Param	storeID			query	int		true	"门店ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetStoreBills [get]
func (c *FinancialController) GetStoreBills() {
	c.callGetStoreBills(func(params *tFinancialGetStoreBillsParams) (retVal interface{}, errCode string, err error) {
		retVal, err = financial.GetStoreBills(params.Ctx, params.StoreID)
		return retVal, "", err
	})
}

// @Param	token			header	string	true	"认证token"

// @Title 显示门店最新账单
// @Description 显示门店最新账单
// @Param	storeID			query	int		true	"门店ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /ShowStoreLatestBill [get]
func (c *FinancialController) ShowStoreLatestBill() {
	var bills []*legacymodel.StoreBill
	w := c.Ctx.ResponseWriter
	w.Header().Add("Content-Type", "text/html")
	w.WriteHeader(200)
	htmlResult := ""
	html := `
	%s
	`
	c.callShowStoreLatestBill(func(params *tFinancialShowStoreLatestBillParams) (retVal interface{}, errCode string, err error) {
		bills, err = financial.GetStoreBills(params.Ctx, params.StoreID)
		retVal = bills
		if len(bills) == 0 {
			err = fmt.Errorf("门店%d没有账单", params.StoreID)
		} else {
			htmlResult = fmt.Sprintf(html, bills[0].Url)
		}
		return retVal, model.ErrorCodeIgnore, err
	})
	if htmlResult != "" {
		io.WriteString(w, htmlResult)
	}
}

// @Title 查询正向订单结账信息
// @Description 查询正向订单结账信息
// @Param	token			header	string	true	"认证token"
// @Param	orderID			query	string	false	"订单号，如果此项不为空，忽略其它所有查询条件"
// @Param	fromDate		query	string	false	"开始日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	toDate			query	string	false	"结束日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	vendorIDs		query	string	false	"订单所属厂商列表[1,2,3]，缺省不限制"
// @Param	storeIDs		query	string	false	"京西门店ID列表[1,2,3]，缺省不限制"
// @Param	statuss			query	string	false	"订单状态列表[1,2,3]，缺省不限制"
// @Param	cities			query	string	false	"城市code列表[1,2,3]，缺省不限制"
// @Param	offset			query	int		false	"结果起始序号（以0开始，缺省为0）"
// @Param	pageSize		query	int		false	"结果页大小（缺省为50，-1表示全部）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetOrdersFinancial [get]
func (c *FinancialController) GetOrdersFinancial() {
	c.callGetOrdersFinancial(func(params *tFinancialGetOrdersFinancialParams) (retVal interface{}, errCode string, err error) {
		retVal, err = orderman.FixedOrderManager.GetOrdersFinancial(params.Ctx, params.FromDate, params.ToDate, params.MapData, params.Offset, params.PageSize)
		return retVal, "", err
	})
}
