import pick from 'lodash.pick';
import { mapGetters }  from  'vuex';
import request from '@/utils/request'
import {upcloud_base_wh} from "@/config/serviceSettings";

export const setwarehouse = {
	computed:{
		...mapGetters(['warehouse']),
	},
 mounted() {
    this.SetWarehouse()
  },
  methods: {
    SetWarehouse() {
      let warehouse = this.$store.state.user.warehouse
      if (Object.keys(warehouse).length > 0) {
        if (this.$refs.gridform) {
          this.$refs.gridform.form.setFieldsValue(
            pick(warehouse, 'warehouseId', 'warehouseCode', 'warehouseName')
          )
        } else if (this.$refs.search) {
          this.$refs.search.form.setFieldsValue(pick(warehouse, 'warehouseId', 'warehouseCode', 'warehouseName'))
        }
      }
	  this.SetBldg(); //设置默认建筑物
    },
	//  设置默认建筑物
	SetBldg(){
		this.bldgPage({warehouseId:this.$store.state.user.warehouse.warehouseId})
		.then(res=>{
			if(res.success){
				 if (this.$refs.search){
					this.$refs.search.form.setFieldsValue({
						bldgId   : res.data.list[0].bldgId,
						bldgCode : res.data.list[0].bldgCode,
						bldgName : res.data.list[0].bldgName,
					})
					 
				 } else if(this.$refs.gridform) {
					this.$refs.gridform.form.setFieldsValue({
						bldgId   : res.data.list[0].bldgId,
						bldgCode : res.data.list[0].bldgCode,
						bldgName : res.data.list[0].bldgName,
					})
				 }
			}
		})
	},
	bldgPage(params) {
		  return request({
		    url:upcloud_base_wh+'/wmsWhBldg/v1/page',
		    method: 'get',
		    params: params,
		  })
		},
  },
	watch:{
		warehouse:{
			handler:function(nVal,oVal){
					this.SetWarehouse();
				},
			deep:true,
		}
	},
	
}
