import {
  goodsPurchaseItemAddCode,
  goodsPurchaseCodeList,
  goodsPurchaseItemDelCode
} from '@/api/goodsPurchasedStored.js'
export default {
  data() {
    return {}
  },
  methods: {
    // 获取条码列表
    getGoodsPurchaseCodeList() {
      if (this.formData.id) {
        goodsPurchaseCodeList({
          pageNum: 1,
          pageSize: -1,
          params: [{ param: 'c.goods_purchase_id', compare: '=', value: this.formData.id }]
        }).then(res => {
          this.formData.codeInfoList = res.data.records
        })
      }
    },
    // 添加条码信息
    addCodeInfo() {
      this.formData.codeInfoList.unshift({
        brandId: '',
        brandName: '',
        mainId: '',
        mainName: '',
        smallId: '',
        smallName: '',
        goodsMaterialId: '',
        goodsMaterialName: '',
        specsName: '',
        goodsMaterialItemType: '',
        goodsMaterialItemId: '',
        goodsMaterialItemName: '',
        uniqueCode: '',
        code: '',
        codeQty: '',
        goodsPurchaseItemId: '',
        goodsPurchaseId: this.formData.id
      })
      this.isEditIndex = 0
    },
    addCode(row) {
      if (this.isEditIndex > -1) {
        this.$refs.formRef.validateField(this.getCodeVfyKey(), (valid, invalidFields, errLabels) => {
          if (valid && this.eidtCodeItems()) {
            goodsPurchaseItemAddCode({
              codeList: [{ ...row }]
            }).then(res => {
              this.isEditIndex = -1
              this.getGoodsPurchaseCodeList()
            })
          }
        })
      }
    },
    delCode(row, index) {
      if (row.id) {
        goodsPurchaseItemDelCode({
          codeId: row.id,
          id: row.goodsPurchaseId
        }).then(res => {
          this.getGoodsPurchaseCodeList()
        })
      } else {
        this.formData?.codeInfoList?.splice(index, 1)
        if (index == this.isEditIndex) {
          this.isEditIndex = -1
        }
      }
    }
  }
}
