<template>
  <div>
    <el-table v-loading="loading" :data="codeArray" style="width: 100%;" max-height="350" height="350"
              :row-style="rowStyle"
              border
              ref="multipleTable">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="序号" type="index" width="55"></el-table-column>
      <el-table-column
        label="UDI码"
        prop="code"
        width="250"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="生产日期"
        prop="produceDate"
        width="120"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="失效日期"
        prop="expireDate"
        width="120"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="批次号"
        prop="batchNo"
        width="120"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="序列号"
        prop="serialNo"
        width="120"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="供应商"
        prop="supName"
        width="180"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="扫码数量"
        prop="count"
        width="100"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="实际数量"
        prop="reCount"
        width="100"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click.native.stop="deleteCode(scope.$index, scope.row)"
          >减一
          </el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.stop="bindRl(scope.row)"
          >选择产品
          </el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.stop="handleUnitClick(scope.row)"
          >选择供应商
          </el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.stop="editCode(scope.row)"
          >编辑
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="query.page"
        :limit.sync="query.limit"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import {deleteCodesTempById, errorCodeList} from "@/api/inout/order";

export default {
  name: "IoAddCodeDetail",
  props: {
    idQuery: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      loading: false,
      query: {
        code: "",
        corpOrderId: "",
        page: 1,
        limit: 10,
      },
      codeArray: [],
      total: 0,
      curRow: null,
      selectRlVisible: false,
      dialogTableVisible: false,
      editCodeVisible: false,
    }
  },
  methods: {
    getCodeList() {
      this.loading = true;
      this.query.orderId = this.idQuery.id;
      errorCodeList(this.query) //查找该单号下的所有条码
        .then((response) => {
          this.codeArray = response.data.list || [];
          this.total = response.data.total || 0;
          for (let i = 0; i < this.codeArray.length; i++) {
            this.codeArray[i].fromType = this.formData.fromType;
            this.codeArray[i].billType = this.formData.billType;
          }
          this.tableSelection();
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },

    deleteCode(index, row) {
      this.$confirm("是否确定移除一个条码?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.loading = true;
        let tQuery = {
          id: row.id
        }
        deleteCodesTempById(tQuery)
          .then((response) => {
            if (response.code === 20000) {
              this.getCodeList();
            } else {
              this.$message.error(response.message);
            }
            this.loading = false;
          });
      }).catch(() => {
      });
    },

    bindRl(val) {
      this.curRow = val;
      this.selectRlVisible = true;
    },

    handleUnitClick(row) {
      this.curRow = row;
      this.dialogTableVisible = true;
    },

    editCode(row) {
      this.editTitle = "编辑条码";
      this.codeDetail = row;
      this.editCodeVisible = true;
    },

    rowStyle({row, rowIndex}) {
      let rowBackground = {};
      if (!this.$isNotBlank(row.supId) || !this.$isNotBlank(row.relId)) {
        rowBackground.color = '#f60303';
      }
      return rowBackground;
    },
  },
  created() {

    this.getCodeList();
  },
}
</script>

<style scoped>

</style>
