import {
  goodsPurchaseRetCodeList,
  goodsPurchaseRetItemAddCode,
  goodsPurchaseRetItemDelCode
} from '@/api/merchandisePurchaseReturn.js'
export default {
  data() {
    return {}
  },
  methods: {
    getGoodsPurchaseRetCodeList() {
      if (this.formData.id) {
        goodsPurchaseRetCodeList({
          pageNum: 1,
          pageSize: -1,
          params: [{ param: 'c.goods_purchase_ret_id', compare: '=', value: this.formData.id }]
        }).then(res => {
          this.formData.codeInfoList = res.data.records
          console.log(this.formData.codeInfoList)
        })
      }
    },
    // 添加条码信息
    addCodeInfo() {
      this.formData.codeInfoList.unshift({
        brandId: '',
        brandName: '',
        mainId: '',
        mainName: '',
        smallId: '',
        smallName: '',
        goodsMaterialId: '',
        goodsMaterialName: '',
        specsName: '',
        goodsMaterialItemType: '',
        goodsMaterialItemId: '',
        goodsMaterialItemName: '',
        uniqueCode: '',
        code: '',
        codeQty: '',
        goodsPurchaseRetItemId: '',
        goodsPurchaseRetId: 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()) {
            goodsPurchaseRetItemAddCode({
              codeList: [{ ...row }]
            }).then(res => {
              this.isEditIndex = -1
              this.getGoodsPurchaseRetCodeList()
            })
          }
        })
      }
    },
    delCode(row, index) {
      if (row.id) {
        goodsPurchaseRetItemDelCode({
          codeId: row.id,
          id: row.goodsPurchaseRetId
        }).then(res => {
          this.getGoodsPurchaseRetCodeList()
        })
      } else {
        this.formData?.codeInfoList?.splice(index, 1)
        if (index == this.isEditIndex) {
          this.isEditIndex = -1
        }
      }
    }
  }
}
