<template>
  <div>
    <el-form :model="query" class="query-form" size="mini">
      <el-row>
        <el-col span="14">
          <el-form-item class="query-form-item" label="UDI码:" label-width="80px">
            <el-input v-model="query.code" placeholder="请扫描或输入UDI码" clearable="true" style="width: 100%"></el-input>
          </el-form-item>
        </el-col>
        <el-col span="6">
          <el-form-item>
            <el-button-group style="margin-left: 15px">
              <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-col>
      </el-row>

    </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="UDI码" prop="code" show-overflow-tooltip width="300"></el-table-column>
      <el-table-column width="160" label="DI/物资编码" prop="nameCode" show-overflow-tooltip></el-table-column>
      <el-table-column width="120" label="批次号" prop="batchNo"></el-table-column>
      <el-table-column label="扫码数量" prop="count" width="90"></el-table-column>
      <el-table-column label="生产日期" width="120" prop="produceDate"></el-table-column>
      <el-table-column label="失效日期" width="120" prop="expireDate" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click.native.stop="deleteCountCode(scope.row)"
          >减一
          </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>
  </div>
</template>

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

export default {
  name: "InvCountCodes",
  props: {
    codeQuery: {
      type: Object,
      required: true,
    },
    orderIdFk: {
      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,
        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.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,
          productId: row.productId,
          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>

