<template>
  <el-drawer
    title="部分入库盘点"
    :with-header="false"
    v-model="visible"
    direction="rtl"
    size="100%"
  >
    <div class="form-container column-container">
      <div class="form-btns-container">
        <span class="title-label">
          <el-icon>
            <Document />
          </el-icon>
          <span>部分入库盘点</span>
        </span>
        <div class="close-btn" @click="cancel">
          <i class="fa fa-times" aria-hidden="true" />
          <!-- <span>关闭</span> -->
        </div>
      </div>
      <el-form
        class="list-search-container"
        :model="queryTaksParams"
        ref="queryRef"
        :inline="true"
        style="margin-right: 0px"
      >
        <!-- style="float: inline-end" -->
        <!-- && dataList.length > 0 -->
        <el-form-item label="入库单号：">
          <el-input
            placeholder="请输入入库单号"
            v-model.trim="queryTaksParams.inboundNumber"
            @keydown.enter.prevent
            clearable
            style="width: 130px"
          />
          </el-form-item>
          <el-form-item label="批次号：">
          <el-input
            placeholder="请输入批次号"
            v-model.trim="queryTaksParams.lotCode"
            @keydown.enter.prevent
            clearable
            style="width: 130px"
          />
        </el-form-item>

        <el-form-item label="包装箱号：">
          <el-input
            placeholder="请输入包装箱号"
            v-model.trim="queryTaksParams.barcode"
            @keydown.enter.prevent
            clearable
            style="width: 130px"
          />
        </el-form-item>

        <el-form-item style="margin-left: 0">
          <el-button type="info" icon="Search" @click="getList"
            >搜索
          </el-button>
          <el-button
            type="primary"
            style="background-color: #409eff"
            v-if="!loading"
            @click="handleInventory"
            >一键盘点
          </el-button>
          <el-button
            type="warning"
            @click="handleExport"
            >导出
          </el-button>
        </el-form-item>
      </el-form>
      <div class="el-table-container">
        <div class="el-table-inner-container">
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="dataList"
            size="small"
            border
            height="100%"
          >
            <!-- <el-table-column type="selection" width="48" align="center" /> -->
            <el-table-column
              label="行号"
              type="index"
              align="center"
              width="48"
            />
            <el-table-column
              label="入库单号"
              prop="inboundNumber"
              align="center"
              width="120px"
            ></el-table-column>
            <el-table-column
              label="批次号"
              prop="lotCode"
              align="center"
              width="120px"
            >
            </el-table-column>
            <el-table-column
              label="产品描述"
              align="center"
              prop="productDescription"
              width="280px"
            />
            <el-table-column
              label="投入数"
              align="center"
              prop="prodNum"
              width="80"
            />
            <el-table-column
              label="盘点数量"
              align="center"
              prop="taksStockNum"
              width="100"
            >
            </el-table-column>
            <el-table-column
              label="包装箱"
              align="center"
              prop="barcodes"
            >
            </el-table-column>
            <el-table-column label="标识" width="300" align="center">
              <template #default="scope">
                <el-tag
                  v-if="scope.row.isWaste == 1"
                  class="spacing"
                  type="danger"
                  >{{ "批废" }}</el-tag
                >
                <el-tag
                  v-if="scope.row.isAmend == 1"
                  class="spacing"
                  type="danger"
                  >{{ "工艺修改" }}</el-tag
                >
                <el-tag
                  v-if="scope.row.isWasteRecycling == 1"
                  class="spacing"
                  type="primary"
                  >{{ "废品回用" }}</el-tag
                >
                <el-tag
                  v-if="scope.row.fromId != 0"
                  class="spacing"
                  type="warning"
                  >{{ "分批" }}</el-tag
                >
                <el-tag
                  v-if="scope.row.isSuperaddition == 1"
                  class="spacing"
                  type="info"
                  >{{ "追增" }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              label="盘点状态"
              prop="isTaksStock"
              width="200"
              align="center"
            >
              <template #default="scope">
                <dict-tag
                  :options="is_taks_stock"
                  :value="scope.row.isTaksStock"
                />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <!-- 分页 -->
      <paginationMax
        v-show="total > 0"
        :total="total"
        v-model:page="queryTaksParams.pageNum"
        v-model:limit="queryTaksParams.pageSize"
        @pagination="getList"
      />
    </div>
  </el-drawer>
</template>
<script setup>
import {
  getAnyInboundInfo,
  saveAnyInboundInfo,
  anyInboundExport
} from "@/api/business/taksStockLot";
import { ref } from "vue";
const emit = defineEmits(["handleSaveSuccess"]);
const { proxy } = getCurrentInstance();
/** 字典 */
const { is_taks_stock } = proxy.useDict("is_taks_stock");
/** 表单抽屉 页变量 */
const loading = ref(false);
const visible = ref(false);
const detailInfo = ref({});
const dataList = ref([]);
const total = ref(0);
const tableRef = ref(null);
/** 查询对象 */
const queryTaksParams = ref({
  pageNum: 1,
  pageSize: 50,
  lotCode: null,
  inboundNumber:null,
  barcode: null,
});

/****************************  方法区  ****************************/
/** 打开抽屉 */
const open = (row) => {
  visible.value = true;
  detailInfo.value = proxy.deepClone(row);
  reset();
  getList();
};

//一键盘点
function handleInventory() {

  //弹出确认框，确认用户是否确认盘点
  proxy.$modal
  .confirm("是否确认一键盘点,将会盘点所有部分入库的批次？")
    .then(() => {
      loading.value = true;
      saveAnyInboundInfo({ takeStockPeriodId: detailInfo.value.id }).then(
        (res) => {
          if (res.code == 200) {
            proxy.$modal.msgSuccess("操作成功");
            loading.value = false;
            reset();
            getList();
          } else {
            proxy.$modal.msgError("操作失败");
          }
          loading.value = false;
        }
      ).catch(res => {
        loading.value = false;
      });
    })
}

function getList() {
  loading.value = true;

  console.log("盘点明细", detailInfo.value);
  queryTaksParams.value.takeStockPeriodId = detailInfo.value.id;
  getAnyInboundInfo(queryTaksParams.value).then((res) => {
    if (res.code == 200) {
      dataList.value = res.rows;
      total.value = res.total;
      loading.value = false;
    }
  });
}
function handleExport() {
  anyInboundExport({ takeStockPeriodId: detailInfo.value.id })
}
/** 取消按钮 */
const cancel = () => {
  visible.value = false;
  reset();
};

/** 表单重置 */
const reset = () => {
  queryTaksParams.value = ({
  pageNum: 1,
  pageSize: 50,
  lotCode: null,
  inboundNumber:null,
  barcode: null,
});
};

/** 暴露给父组件的方法 */
defineExpose({
  open,
});
</script>
<style scoped>
.el-message-box {
  max-width: none !important;
  width: 420px;
}
</style>
