<template>
  <p-card title="二次拼箱">
    <template slot="extra">
      <a-space> <a-button type="primary" @click="chcekConfirm">二次拼箱</a-button>
      <a-button type="primary" @click="resetEvent">重置</a-button>
      </a-space>
    </template>

    <div class="plg-inner-form-mb">
      <a-form
        class="plg-login"
        :form="numform"
        :label-col="{ span: 2 }"
        :wrapper-col="{ span: 5 }"
        @submit.prevent="hanlderSubmit"
      >
        <a-form-item label="拼箱条码">
          <a-input
            class="input"
            type="text"
            autocomplete="false"
            @pressEnter="entergoods"
            placeholder="请输入拼箱条码"
            v-decorator="['barcode', { rules: [{ required: true, message: '请输入数量' }], validateTrigger: ' ' }]"
          >
          </a-input>
        </a-form-item>
      </a-form>
    </div>
    <p-table
      :scroll="{ x: true }"
      :columns="columns"
      :row-selection="rowSelection"
      :data-source="data"
      @clickRow="clickRow"
      bordered
    >
    </p-table>
  </p-card>
</template>

<script>
// 设置默认仓库
// import { setwarehouse } from '@/views/kzyWMS/SearchModal/mixins_warehouse.js'
import { columns } from './config/outerCheck'
import { secondaryLCLquery, secondaryLCLpost } from './api'
import { mapGetters } from 'vuex'
export default {
  name: 'InnerCheck',
  // mixins: [setwarehouse],
  components: {},
  data() {
    return {
      data: [],
      columns,
      numform: this.$form.createForm(this),
      selectedRows: []
    }
  },
  created() {},
  mounted() {},
  computed: {
    ...mapGetters(['userInfo']),
    rowSelection() {
      let _this = this
      return {
        onSelectAll(selected, selectedRows, changeRows) {
          var checked = selected
          var key = changeRows.map(item => item.id)
        },
        onSelect(record, selected, selectedRows, nativeEvent) {
          var checked = selected
          var key = record.id
          console.log(selectedRows)
        },
        onChange(selectedRowKeys, selectedRows) {
          _this.selectedRows = selectedRows
          console.log(selectedRowKeys)
        }
      }
    }
  },
  methods: {
    hanlderSubmit(event, form) {
      event.preventDefault()
      this.numform.validateFields((error, values) => {
        if (!error) {
          secondaryLCLquery({ barcode: values.barcode }).then(res => {
            if (res.success) {
              // this.data.push(res.data[0])
              if(res.data.length == 0) {
                this.$message.error('无效商品条码')
                return
              }
              if (this.data.length > 0) {
                let flag = this.data.every(item => {
                  return item.barcode != res.data[0].barcode
                })
                if(flag) {
                  this.data.push(res.data[0])
                } else {
                  this.$message.error('请不要输入重复商品条码')
                }
              } else {
                this.data.push(res.data[0])
              }
              this.numform.resetFields()
            }
          })
        }
      })
    },
    entergoods(e) {
      console.log(e)
    },
    clickRow(form, rowKey, data) {
      console.log(data)
      picktaskList({ barcodeId: data.barcodeId }).then(res => {
        if (res.success) {
          this.data = res.data
        }
      })
    },
    chcekConfirm() {
      if (this.data.length > 1) {
        let params = this.data.reduce((res,item) => {
          res.push(item.id)
          return res
        },[])
        secondaryLCLpost(params).then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.data = []
          } else {
            this.$message.error(res.message)

          }
        })
      } else {
         this.$message.error('至少选择两条进行拼单')

      }
    },
    resetEvent() {
      this.numform.resetFields()
      this.data = []
    }
  }
}
</script>
