/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		王英杰		 2024-1-24			1.00		    新建商品档案
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
const app = getApp()
const Constants = require('@/utils/Constants')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
  
    cardList: ['main', 'goods', "warehouse", 'price', 'other'],
    contentObj: {
      main: [{ //唯一编码
          code: 'onlyCode',
          type: 'textarea',
          title: mixins.$t("onlyCode"),
          readonly: true
        }, { //商品型号
          code: 'skuModel',
          type: 'textarea',
          title: mixins.$t("skuModel"),
          required: true,
        }, { //商品名称
          code: 'skuName',
          type: 'textarea',
          title: mixins.$t("skuName"),
          required: true,
        }, {
          code: 'file',
          type: 'uploader',
          title: '商品图片'
        },

      ],
      goods: [{ //商品品牌
          code: 'brandId',
          name: 'brandName',
          title: mixins.$t("goodsBrand"),
          type: 'choose', 
          urlKey:'goodsBrand' ,
          required: true,
          // changeObj: {
          //   pur: {
          //     hide: ['serviceCategories'],
          //     visible: ['brandId']
          //   },
          //   service: {
          //     hide: ['brandId'],
          //     visible: ['serviceCategories']
          //   }
        },
        { //商品种类
          code: 'categoryId',
          name: 'catName',
          title: mixins.$t("goodsCategory"),
          type: 'choose',
          urlKey:'goodsCategory' ,
          required: true,

        }, { //计量单位
          code: 'unitId',
          name: 'unitName',
          title: mixins.$t("unit"),
          type: 'choose',
          urlKey: 'unit',
          required: true,

        }, { //辅助计量单位
          code: 'subUnitId',
          name: 'subUnitName',
          title: mixins.$t("subUnitId"),
          type: 'choose',
          urlKey: 'unit',  

        }, { //商品系列
          code: 'seriesId',
          name: 'seriesName',
          title: mixins.$t("goodsSeries"),
          type: 'choose',
          urlKey: 'goodsSeries',


        }, { //商品规格 
          code: 'skuSpec',
          name: 'skuSpec',
          type: 'textarea',
          title: mixins.$t("sukSpec"),
        }
      ],
      warehouse: [{ //仓库名称
          code: 'whId',
          name: 'whName',
          title: mixins.$t("whName"),
          type: 'choose',
          urlKey: 'openingInventory',
        },
        { //库存批号
          code: 'iinventoryBatchNumber',
          type: 'textarea',
          title: mixins.$t("iinventoryBatchNumber"),
        },
        { //期初数量
          code: 'openingInventoryQty',
          type: 'number',
          sign:"",
          digits:0,
          title: mixins.$t("openingInventoryQty"),
        },
        { //期初成本价
          code: 'iinitialCostPrice',
          type: 'textarea',
          title: mixins.$t("iinitialCostPrice"),
        },

      ],
      price: [{ //采购价
        code: 'pricePurchase',
        type: 'number',
        title: mixins.$t("purchasePrice"),
      }, { //零售价
        code: 'priceStandard',
        type: 'number',
        title: mixins.$t("retailPrice"),
      }, { //批发价
        code: 'priceWholesale',
        type: 'number',
        title: mixins.$t("wholesalePrice"),
      }, { //销售限价
        code: 'priceLimited',
        type: 'number',
        title: mixins.$t("SaleSPriceLimit"),
      }, ],

      other: [{
        code: 'remarks',
        type: 'textarea',
        title: mixins.$t("remarks"),
      }]
    },
    // 路由
    routeObjName: 'goodsSku',
  },
  chooseData(e){
    console.log("t",e,this.data.formData);
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = e.detail.data.data
    if(code == "brandId"){ //商品品牌
      formData.brandId = data.id
      formData.brandName = data.name
    }
    if(code == "categoryId"){ //商品种类
      formData.categoryId = data.id
      formData.catName = data.name
    }
    if(code == "unitId"){ //计量单位
      formData.unitId = data.id
      formData.unitName = data.name
    } 
    if(code == "subUnitId"){ //补助计量单位
      formData.subUnitId = data.id
      formData.subUnitName = data.name
    } 
    if(code == "seriesId"){ //补助计量单位
      formData.seriesId = data.id
      formData.seriesName = data.name
    } 
    if(code == "whId"){ //补助计量单位
      formData.whId = data.id
      formData.whName = data.name
    } 
    this.setData({
      formData: JSON.stringify(formData)
    })
  },
  // 设置保存参数
  setParams(params) {
    let saveData = JSON.parse(this.data.formData)
    // 修改之后的 值 
    params = Object.assign({}, params, saveData);
    params.serviceCategories = (params.serviceCategories && params.serviceCategories.length > 0) ? params.serviceCategories.split(',') : []
    return params
  },

 
  /**
   * @desc : 保存数据服务
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  saveData() {
    console.log("参数",this.data.params); 
      if (this.data.formMode == Constants.formMode.edit) { //编辑接口
        return this.excute(this.data.service, this.data.service.update, this.data.params)
      } else { //新建接口
        return this.excute(this.data.service, this.data.service.insert, this.data.params);
      }
  },
  /**
   * @desc : 保存接口的 后续处理方法
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  handleData(data) {
    wx.navigateBack({
      delta: 1
    })
  },
   /**
   * @desc : 页面加载数据方法
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  loadInit() {
    if (this.data.formMode == Constants.formMode.edit) {
      wx.setNavigationBarTitle({
        title: mixins.$t('编辑商品档案'),
      })
    } else {
      wx.setNavigationBarTitle({
        title: mixins.$t('新建商品档案'),
      })
    }
  },
   /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setValuesByEdit(data) {
   // data.serviceCategories = (data.serviceCategories && data.serviceCategories.length > 0) ? data.serviceCategories.join(',') : undefined
    this.setData({
      formData: JSON.stringify(data)
    }) 

  },
})