package controllers

import (
	"gdsm/models/dict"
	"gdsm/models/ec"
	"github.com/astaxie/beego"
	_ "github.com/mattn/go-sqlite3"
	"wb/cc"
	"wb/ctrl"
	"wb/ctrl/uibuilder"
	"wb/ii"
	"wb/om"
	"wb/st"
	"wb/ut"
)

type ProductController struct {
	ctrl.ItemController
}

func (this *ProductController) NestPrepare() {
	this.CtxItemInfo, _ = ii.ItemInfoMap["product"]
}
func (this *ProductController) UiAdd() {
	sn := ut.TUId()
	this.Data["sn"] = sn
	this.FillFormElement(uibuilder.BuildFormElement(this.CtxItemInfo, map[string]interface{}{"sn": sn}, map[string]string{}))
	this.UiAddItem(this.CtxItemInfo)
	this.TplName = "product/add.tpl"
}
func (this *ProductController) UiBomAdd() {
	sn := ut.TUId()
	this.Data["sn"] = sn
	this.FillFormElement(uibuilder.BuildFormElement(this.CtxItemInfo, map[string]interface{}{"sn": sn}, map[string]string{}))
	this.UiAddItem(this.CtxItemInfo)
	this.TplName = "product/bomadd.tpl"
}
func (this *ProductController) UiNewAdd() {
	sn := ut.TUId()
	this.Data["sn"] = sn
	this.FillFormElement(uibuilder.BuildFormElement(this.CtxItemInfo, map[string]interface{}{"sn": sn}, map[string]string{}))
	this.UiAddItem(this.CtxItemInfo)
	this.TplName = "product/newadd.tpl"
}
func (this *ProductController) UiUpdate() {
	itemName := this.CtxItemInfo.Name
	sn := this.GetString(cc.Col.Sn)
	if sn == "" {
		beego.Error("ui.Update", st.ParamSnIsNone)
		this.Ctx.WriteString(st.ParamSnIsNone)
		return
	}
	params := om.Params{ec.Col.Sn: sn}
	code, oldValueMap := om.Table(itemName).Get(params)
	if code == st.Success {
		this.SetDataWithoutDefaultStr(cc.UrlService, this.UrlItemUpdate, "/item/update/"+itemName)
		sn := oldValueMap.GetString("sn")
		this.Data["sn"] = sn
		name := oldValueMap.GetString("name")
		this.Data["name"] = name
		types := oldValueMap.GetString("type")
		this.Data["type"] = types
		specification := oldValueMap.GetString("specification")
		this.Data["specification"] = specification
		model := oldValueMap.GetString("model")
		this.Data["model"] = model
		unit := oldValueMap.GetString("unit")
		this.Data["unit"] = unit
		brand := oldValueMap.GetString("brand")
		this.Data["brand"] = brand
		sellingprice := oldValueMap.GetString("sellingprice")
		this.Data["sellingprice"] = sellingprice
		costprice := oldValueMap.GetString("costprice")
		this.Data["costprice"] = costprice
		internalnote := oldValueMap.GetString("internalnote")
		this.Data["internalnote"] = internalnote
		recordnotes := oldValueMap.GetString("recordnotes")
		this.Data["recordnotes"] = recordnotes
		statusMap := map[string]string{}
		this.FillValue(oldValueMap)
		this.FillFormElement(uibuilder.BuildFormElement(this.CtxItemInfo, oldValueMap, statusMap))
		this.TplName = "product/update.tpl"
	} else {
		beego.Error("UiUpdateItem error", st.ItemNotFound)
		this.Ctx.WriteString(st.ItemNotFound)
	}
}
func (this *ProductController) UiList() {
	this.TplName = "product/list.tpl"
}
func (this *ProductController) UiCostList() {
	this.TplName = "product/costlist.tpl"
}
func (this *ProductController) ItemList() {
	tableResult := this.GetTableList(this.CtxItemInfo, om.Params{})
	attList := dict.GetAttachmentList()
	for _, vm := range tableResult.Rows {
		sn := vm.GetString(ec.Col.Sn)
		if ut.IsStrInSlice(attList, sn) {
			vm["atmt"] = true
		} else {
			vm["atmt"] = false
		}
	}
	this.SendJson(&tableResult)
}