<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.lotCode"
            @keydown.enter.prevent
            clearable
            style="width: 130px"
          />
        </el-form-item>

        <el-form-item label="发出单号：">
          <el-input
            placeholder="请输入发出单号"
            v-model.trim="queryTaksParams.outsourceFormNo"
            @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 && detailInfo.status != 2"
            @click="handleInventory"
            >一键盘点
          </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%"
            @selection-change="handleSelectionChange"
          >
            <!-- <el-table-column type="selection" width="48" align="center" /> -->
            <el-table-column
              label="行号"
              type="index"
              align="center"
              width="48"
            />
            <el-table-column
              label="发出单号"
              align="center"
              prop="outsourceFormNo"
              width="110"
            />
            <el-table-column
              label="批次号"
              prop="lotCode"
              align="center"
              width="120px"
            >
            </el-table-column>
            <el-table-column
              label="产品描述"
              align="center"
              prop="productDescription"
            />
            <el-table-column
              label="生产状态"
              prop="isProductStatus"
              width="90"
              align="center"
            >
              <template #default="scope">
                <dict-tag
                  :options="inventory_production_status"
                  :value="scope.row.isProductStatus"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="投入数"
              align="center"
              prop="prodNum"
              width="80"
            />
            <el-table-column
              label="盘点数量"
              align="center"
              prop="taksStockNum"
              width="100"
            >
              <!-- <template #default="scope">
                <el-input-number
                  v-if="scope.row.editStatus"
                  v-model="scope.row.taksStockNum"
                  controls-position="right"
                  style="width: 100px"
                  :min="0"
                  :precision="0"
                  :max="99999"
                  @input="
                    (val) => {
                      handleChangeTaksStockNum(val, scope.row, scope.$index);
                    }
                  "
                />
                <div v-else>{{ scope.row.taksStockNum }}</div>
              </template> -->
            </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.fromLotId != 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 {
  getProductionPlanDetail,
  getNotTaksLot,
  getLotNumber,
  getOutsourceInfo,
  singleInventoryCheck,
  multiplyInventoryCheck,
  saveOutsourceInfo,
} from "@/api/business/taksStockLot";
import { ref } from "vue";
import router from "@/router";
import { ElMessageBox } from "element-plus";
const emit = defineEmits(["handleSaveSuccess"]);
const { proxy } = getCurrentInstance();
/** 字典 */
const { is_identification } = proxy.useDict("is_identification");
const { is_taks_stock } = proxy.useDict("is_taks_stock");
const { inventory_production_status } = proxy.useDict(
  "inventory_production_status"
);
/** 表单抽屉 页变量 */
const loading = ref(false);
const visible = ref(false);
const detailInfo = ref({});
const dataList = ref([]);
const total = ref(0);
const productionPlanDetaiList = ref([]);
const tableRef = ref(null);
const isInventoryMode = ref(true);
const deptList = ref([]);
const selections = ref([]);
/** 查询对象 */
const queryTaksParams = ref({
  pageNum: 1,
  pageSize: 50,
  productionPlanDetailId: null,
  deptId: null,
});

/****************************  方法区  ****************************/
/** 打开抽屉 */
const open = (row) => {
  visible.value = true;
  detailInfo.value = proxy.deepClone(row);
  reset();
  getList();
};
function getProductionPlanDetailList() {
  getProductionPlanDetail(queryTaksParams.value).then((res) => {
    if (res.data.length > 0) {
      productionPlanDetaiList.value = res.data;
      queryTaksParams.value.productionPlanDetailId = res.data[0].value;
      getList();
    } else {
      productionPlanDetaiList.value = [];
      queryTaksParams.value.productionPlanDetailId = null;
      dataList.value = [];
      proxy.$modal.msgError("当前工段无未生产的批次");
      return;
    }
  });
}

function handleSelectionChange(selection) {
  if (isInventoryMode.value) {
    selections.value = selection;
    console.log(selections.value);
  }
}

//一键盘点
function handleInventory() {

  //弹出确认框，确认用户是否确认盘点
  proxy.$modal
  .confirm("是否确认一键盘点,将会盘点所有未收回的批次？")
    .then(() => {
      loading.value = true;
      saveOutsourceInfo({ 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;
        }
      );
    })
}

function getList() {
  loading.value = true;

  console.log("盘点明细", detailInfo.value);
  queryTaksParams.value.takeStockPeriodId = detailInfo.value.id;
  getOutsourceInfo(queryTaksParams.value).then((res) => {
    if (res.code == 200) {
      dataList.value = res.rows;
      total.value = res.total;
      loading.value = false;
      console.log("外协信息", dataList.value);
    }
  });
}
function handleProductionPlanChange() {
  proxy.$refs.tableRef.clearSelection();
  getList();
}
//编辑
function handleEdit(row, index) {
  //判断是否能编辑
  //分批，废品回用，增批不算在允许盘点的个数内
  if (
    row.isWasteRecycling == 1 ||
    row.fromLotId != 0 ||
    row.isSuperaddition == 1
  ) {
    row.editStatus = true;
    row.taksStockNum = row.prodNum;
    row.isTaksStock = "1";
  } else {
    getLotNumber(queryTaksParams.value).then((res) => {
      if (res.data.allowLotNum > 0) {
        row.editStatus = true;
        console.log(row);
        row.taksStockNum = row.prodNum;
        row.isTaksStock = "1";
        console.log(row);
      } else {
        proxy.$modal.msgError(
          "当前常规未生产批次数量多于可盘点批次数量，不可编辑！"
        );
        return;
      }
    });
  }
}
//取消编辑
function handleCancel(row, index) {
  dataList.value[index].editStatus = false;
  getList();
}
//保存
function handleSave(row) {
  singleInventoryCheck(row).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess("盘点成功");
      if (res.data) {
        getList();
      } else {
        //重新查计划单
        getProductionPlanDetailList();
      }
    }
  });
}
function handleDeptChange() {
  proxy.$refs.tableRef.clearSelection();
  console.log(queryTaksParams.value);
  getProductionPlanDetailList();
}

/** 取消按钮 */
const cancel = () => {
  visible.value = false;
  reset();
  emit("handleSaveSuccess");
};

/** 表单重置 */
const reset = () => {
  const queryTaksParams = ref({
    pageNum: 1,
    pageSize: 50,
  });
};

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