<template>
  <div>
    <el-card class="el-card">
      <el-form :model="query" class="query-form" size="mini" :inline="true">
        <el-form-item class="query-form-item" label="条码:">
          <el-input v-model="query.code" placeholder="请扫描或输入条码" clearable="true"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button-group style="margin-left: 10px;display:flex;">
            <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="codeArray" style="width: 100%" highlight-current-row
                border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="条码" prop="code" show-overflow-tooltip></el-table-column>
        <el-table-column label="扫码数量" prop="count"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteCountCode(scope.row.id)"
            >减一
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="query.limit"
        :page.sync="query.page"
        @pagination="getCodeList"
      ></pagination>

    </el-card>
  </div>
</template>

<script>
import {getCountCodesList, deleteCode} from "@/api/inventory/invCountCodes";

export default {
  name: "InvCountCodes",
  props: {
    codeQuery: {
      type: Object,
      required: true,
    },
    getCountOrderDetail: {
      type: Function,
      required: true
    }
  },

  data() {
    return {
      query: {
        code: null,
        productId: null,
        orderIdFk: null,
        batchNo: null,
        page: 1,
        limit: 20
      },
      loading: true,
      codeArray: [],
      total: 0,
    }
  },
  methods: {
    onReset() {
      this.query = {
        code: null,
        productId: null,
        orderIdFk: null,
        batchNo: null,
        page: 1,
        limit: 20
      };
      this.getCodeList();
    },
    onSubmit() {
      this.query.page = 1;
      this.getCodeList();
    },
    getCodeList() {
       this.loading = true;
      this.query.relId = this.codeQuery.relId;
      this.query.orderIdFk = this.codeQuery.orderIdFk;
      this.query.batchNo = this.codeQuery.batchNo;
      getCountCodesList(this.query).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.codeArray = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.codeArray = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.codeArray = [];
        this.total = 0;
      })
    },
    deleteCountCode(row) {
      this.$confirm('是否删除此条码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          orderIdFk: row.orderIdFk,
          relId: row.relId,
          code: row.code
        };
        deleteCode(params).then((res) => {
          if (res.code === 20000) {
            this.$message.success("删除成功！");
            this.getCodeList();
            this.getCountOrderDetail();
          } else {
            this.$message.error(res.data.message);
          }
        })
      }).catch(() => {
      });
    },
  },
  created() {
    this.getCodeList();
  },
};
</script>

