<template>
  <div>
    <p-card>
      <div slot="title" style="display: inline-block; align-items: center; cursor: pointer" @click="clickEvent('back')">
        <a-icon type="arrow-left" style="color: #1990ff" class="mr-10" />
        {{ '商品红冲' }}
      </div>
      <div class="buttonlist" slot="extra">
        <div style="display:inline-block;width:70%">
          <span style="display:inline-block;margin-top:5px">整单红冲原因:</span>
          <a-input style="width: 50%;margin: 0 10px;" type="text" v-model="hcresult" />
        </div>
        <a-button :loading="Btnloading" key="submit2" type="primary" @click="handleOkreplenishment('2')">
          整单红冲
        </a-button>
      </div>
      <div>
        <p-table-edit-pro
          ref="hcTable"
          style="padding: 30px"
          :scroll="{ x: true }"
          :dataSource.sync="dataSource"
          :columns="singleColumnsEdit"
          :remoteUrl="params"
          :remoteData="params.params"
          :remoteParams="params.params"
          :remoteBefore="remoteBefore"
          @clickRow="clickRow"
          :hasRepairDataSource="true"
          :rowSelectType="false"
          rowKey="lineid"
        >
          <div slot="action" href="javascript:;" slot-scope="text, record">
            <a slot="action" style="margin: 0 5px" @click.stop="orderHC(record)">红冲</a>
          </div>
        </p-table-edit-pro>
      </div>
      <div>
        <p-table
          ref="logTable"
          :rowSelectType="false"
          :columns="redColumns"
          :scroll="{ x: true }"
          :doneBefore="tableDoneBefore"
          :loading="loading"
          :hasRepairDataSource="true"
          :data-source="innerData"
        >
        </p-table>
      </div>
    </p-card>
  </div>
</template>

<script>
import { diffOption } from '@/views/kzyWMS/utils/index.js'
import { modalItems, columns, singleColumns, singleColumnsEdit, redColumns } from '../config/create'
import { redomsbill, redomsItembill, redomsItembillQuery } from '../api/api.js'
import { upcloud_base_wh_outbound } from "@/config/serviceSettings";
export default {
  name: '',
  components: {},
  props: {
    orderModifyData: {
      type: Object,
      default() {
        return {}
      }
    },
    singleData: {
      type: Array,
      default() {
        return []
      }
    },
    billid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      singleColumns:singleColumns(this),
      singleColumnsEdit,
      redColumns,
      avlcolumns: null,
      tableDoneBefore: null,
      loading: false,
      rowKey: 'id',
      hcresult: '',
      dataSource: [],
      selectedRowKeys: [],
      selectedRows: [],
      innerData: [],
      Btnloading: false,
      params: {
        url: upcloud_base_wh_outbound+'/wave/billdbybillid',
        params: {},
        timeout: 30000,
        //  headers: {
        //   'Content-Type': 'application/JSON'
        // },
        method: 'post'
      },
    }
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfo
    }
  },
  watch: {},
  methods: {
    clickEvent() {
      this.$emit('clickEvent')
    },
    remoteBefore(res, remoteParams) {
      console.log(remoteParams, 'remoteParamsremoteParams')
      console.log(this.billid)
      remoteParams.data.billid = this.billid
      return remoteParams
    },
    hchanlderSubmit() {
      this.$refs.modelForm.form.validateFields((error, values) => {
        if (!error) {
          const params = {
            warehouseId: this.orderModifyData.warehouseId,
            cargoOwnerId: this.orderModifyData.consignorId,
            itemId: this.orderModifyData.itemId,
            manufacturerLotNum: values.manufacturerLotNum
          }
          getavlpagelist(params).then(res => {
            console.log(res)
          })
        }
      })
    },
    // 查询红冲数量
    QueryRedData(row) {
      const params = {
        billId:row.billid,
        lineBillId: row.lineid
      }
      this.loading = true
      redomsItembillQuery(params).then(res => {
         if (!res.success) {
          this.$exception(res)
          return
        }
        this.innerData = res.data
        this.loading = false
      })

    },
    clickRow(selectedRowKeys, record, row) {
      this.QueryRedData(row)
    },
    orderHC(data) {
      console.log(data)
      if (data.reditemQty <= 0) {
        this.$message.error('红冲数量必须大于0')
        return
      }
      if ((data.reditemQty%1) !== 0) {
        this.$message.error('红冲数量必为整数')
        return
      }
      if (data.reditemQty > data.itemQty) {
        this.$message.error('红冲数量不能大于商品数量')
        return
      }
      if (!data.reditemrelut) {
        this.$message.error('请输入红冲原因')
        return
      }
      const params = {
        billId: this.billid,
        billDId: data.lineid,
        itemId: data.itemid,
        qty: data.reditemQty,
        redReason: data.reditemrelut,
        empNo: this.userInfo.employeeNo,
        userName: this.userInfo.username,
        userId: this.userInfo.userId
      }
      redomsItembill(params).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          data.reditemQty = 0
          data.reditemrelut = ''
          this.$nextTick(()=>{
            this.dataSource = [...this.dataSource]
          })
          this.QueryRedData(data)
        } else {
          this.$exception(res)
        }
      })
    },
    handleOkreplenishment() {
      if (!this.hcresult) {
        this.$message.error('请输入红冲原因')
        return
      }
      this.Btnloading = true
      const params = {
        billId: this.billid,
        redReason: this.hcresult,
        empNo: this.userInfo.employeeNo,
        userName: this.userInfo.userName,
        userId: this.userInfo.userId
      }
      redomsbill(params).then(res => {
        if (res.success) {
          this.Btnloading = false
          this.$message.success(res.message)
          this.clickEvent()
          this.$refs.logTable.reload()
        } else {
          this.$exception(res)
        }
      })
    }
  }
}
</script>

<style></style>
