<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="queryParams" ref="queryRef" :inline="true"
        style="margin-right: 0px">
        <el-form-item class="section-title" label="盘点批次列表" />
        <el-form-item label="批次号：">
          <el-input placeholder="请输入批次号" v-model.trim="queryParams.lotCode" @keydown.enter.prevent clearable
            style="width: 130px" />
        </el-form-item>
        <el-form-item label="产品描述：">
          <el-input placeholder="请输入产品描述" v-model.trim="queryParams.productDescription" @keydown.enter.prevent clearable
            style="width: 130px" />
        </el-form-item>
        <el-form-item label="生产状态：">
          <el-select v-model="queryParams.isProductStatus" clearable placeholder="请选择生产状态" style="width: 145px">
            <el-option v-for="dict in inventory_production_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标识：">
          <el-select style="width: 160px" multiple v-model="queryParams.flags" collapse-tags collapse-tags-tooltip
            clearable placeholder="请选择标识">
            <el-option v-for="item in is_identification" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="section-title" label="请选择当前工段：">
          <el-select-v2 v-model="queryParams.deptId" :options="deptList" placeholder="请选择工段" style="width: 120px" />
        </el-form-item>
        <el-form-item label="盘点状态：">
          <el-select v-model="queryParams.isTaksStock" clearable placeholder="请选择盘点状态" style="width: 145px">
            <el-option v-for="dict in is_taks_stock" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left: 0">
          <el-button type="info" icon="Search" @click="handleQuery">搜索
          </el-button>
          <el-button type="info" icon="Download" @click="handleExport">导出未盘点批次
          </el-button>
          <el-button :disabled="queryParams.deptId == null || queryParams.deptId == '0'" type="primary" icon="Check"   v-hasPermi="['business:inventoryCheck:notProductionCheck']"
            @click="handleTaksLot">未生产批次盘点
          </el-button>
          <el-button v-if="detailInfo.status != 2" type="primary" icon="List" @click="showOutsourceCheckDialog">外协发出单盘点
          </el-button>
        </el-form-item>
      </el-form>
      <div class="el-table-container">
        <div class="el-table-inner-container">
          <el-table v-loading="loading" :data="dataList" size="small" border height="100%">
            <el-table-column label="行号" type="index" align="center" width="48" />
            <el-table-column label="批次号" prop="lotCode" align="center" width="120px">
              <template #default="scope">
                <el-button link type="primary" @click="handleColumnClick(scope.row.lotCode)"><span>{{ scope.row.lotCode
                    }}</span></el-button>
              </template>
            </el-table-column>
            <el-table-column label="产品描述" align="center" prop="productDescription" />
            <el-table-column label="箱号" align="center" prop="carrierName" width="110" />
            <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="deptName" width="90" />
            <el-table-column label="工序" align="center" prop="processAlias" width="110" />
            <el-table-column label="投入数" align="center" prop="prodNum" width="80" />
            <el-table-column label="盘点数量" align="center" prop="taksStockNum" width="90">
              <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="外协单号" align="center" prop="outsourceFormNo" width="110" />
            <el-table-column label="报工状态" align="center" prop="statusLabel" width="110">
              <template #default="scope">
               <span v-if="scope.row.statusLabel == 0">未开始</span>
               <span v-if="scope.row.statusLabel == 1">进行中</span>
               <span v-if="scope.row.statusLabel == 2">结束报工</span>
               <span v-if="scope.row.statusLabel == 3">工序已完成</span>
               <span v-if="scope.row.statusLabel == 4">待领取</span>
               <span v-if="scope.row.statusLabel == 6">已周转</span>
               <span v-if="scope.row.statusLabel == 7">已接收</span>
              </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">
                <el-select v-model="scope.row.isTaksStock" placeholder="请选择盘点状态" style="width: 160px"
                  v-if="scope.row.editStatus"
                  @change="(val) => { handleChangeTaksStock(val, scope.row, scope.$index) }">
                  <el-option v-for="dict in is_taks_stock" :key="dict.value" :label="dict.label"
                    :value="dict.value"></el-option>
                </el-select>
                <div v-else>
                  <dict-tag :options="is_taks_stock" :value="scope.row.isTaksStock" />
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="160" align="center" v-if="detailInfo.status != 2">
              <template #default="scope">
                <el-button link icon="Check" v-if="scope.row.editStatus" type="success"
                  @click="handleSave(scope.row)">保存</el-button>
                <el-button link icon="Edit" v-else type="warning" @click="handleEdit(scope.row, scope.$index)"
                  v-hasPermi="['business:inventoryCheck:editDetail']">编辑</el-button>
                <el-button link icon="Close" v-if="scope.row.editStatus" type="danger"
                  @click="handleCancel(scope.row, scope.$index)">取消</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

      </div>
      <!-- 分页 -->
      <paginationMax v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
    <OutsourceCheckDialog ref="outsourceCheckDialogRef" />
    <taksForm ref="taksFormRef" @handleSaveSuccess="getList" />
  </el-drawer>
</template>
<script setup>
import {
  listTaksStockLot, updateTaksStockLot, updateNoStartLot, exportNoStartLot,getProductionPlanDetail
} from "@/api/business/taksStockLot";
import { getDeptList } from "@/api/business/planDetailSubDetail.js";
import taksForm from "./notTaksForm.vue"
import { ref } from "vue";
import router from "@/router";
import OutsourceCheckDialog from "./OutsourceCheckDialog.vue";
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 outsourceCheckDialogRef = ref(null)

/** 表单抽屉 页变量 */
const loading = ref(false);
const visible = ref(false);
const detailInfo = ref({});
const total = ref(0)
const isDispatch = ref(false)
const deptList = ref([])
const dataList = ref([])
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 50,
  productDescription: "",
  lotCode: "",
  deptId: null,
  flags: null,
  isTaksStock: null
});

/****************************  方法区  ****************************/
/** 打开抽屉 */
const open = (row) => {
  visible.value = true;
  console.log(is_identification.value)
  is_identification.value = is_identification.value.filter(item => item.value != 0)
  reset();
  detailInfo.value = proxy.deepClone(row);
  getDeptList().then((response) => {
    deptList.value = response.data.rows;
    isDispatch.value = response.data.others.isDispatch;
    if (isDispatch.value) {
      deptList.value.unshift({ label: "全部", value: "0" });
    }
    queryParams.value.deptId = deptList.value[0].value;
    queryParams.value.takeStockPeriodId = row.id
    getList();
  });
}
function getList() {
  loading.value = true
  listTaksStockLot(queryParams.value).then(res => {
    res.rows.forEach(element => {
      element.isTaksStock = element.isTaksStock + ""
      element.taksStockNum = Number(element.taksStockNum)
    });
    dataList.value = res.rows;
    total.value = res.total;
    loading.value = false
  })
}
function handleEdit(row, index) {
  row.editStatus = true
  console.log(row);
  if (row.isTaksStock != 1) {
    row.taksStockNum = row.prodNum
    row.isTaksStock = "1"
  }
  console.log(row)
}
//一键盘点
// function handleTaksLot() {
//   console.log(queryParams.value.deptId)
//   updateNoStartLot({ takeStockPeriodId: queryParams.value.takeStockPeriodId, deptId: queryParams.value.deptId }).then(res => {
//     if (res.code == 200) {
//       proxy.$modal.msgSuccess("盘点成功");
//       getList()
//     }
//   })
// }
function handleTaksLot() {
  getProductionPlanDetail(queryParams.value).then(res=>{
    if(res.data.length>0){
      let detail ={}
      detail.deptId = queryParams.value.deptId
      detail.deptList = deptList.value
      detail.takeStockPeriodId = queryParams.value.takeStockPeriodId
      detail.status = detailInfo.value.status
      proxy.$refs.taksFormRef.open(detail)
    }else{
      proxy.$modal.msgError("当前工段无未生产的批次")
    }
  })
}
function handleSave(row) {
  updateTaksStockLot(row).then(res => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess("保存成功");
      getList()
    }
  })
}
/** 打开批次详情页 */
// function handleColumnClick(lotCode) {
//   router.push({ path: "/reviseBath/lotFormParticulars/" + lotCode });
// }
function handleColumnClick(lotCode) {
  // 拼接 URL
  const url = "/reviseBath/lotFormParticulars/" + lotCode;
  // 在新标签页打开 URL
  window.open(url, '_blank');
}
function handleChangeTaksStock(value, row, index) {
  if (value == 0) {
    dataList.value[index].taksStockNum = 0
  }
}
function handleChangeTaksStockNum(value, row, index) {
  if (value != 0) {
    dataList.value[index].isTaksStock = "1"
  }
}
function handleCancel(row, index) {
  dataList.value[index].editStatus = false
  getList()
}
function handleQuery() {
  getList();
}
/** 取消按钮 */
const cancel = () => {
  visible.value = false;
  reset();
};

/** 表单重置 */
const reset = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 50,
    productDescription: "",
    lotCode: "",
    deptId: null,
    flags: null,
    isTaksStock: null
  };
  dataList.value = [];
};

function handleExport() {
  exportNoStartLot({ takeStockPeriodId: queryParams.value.takeStockPeriodId, isTaksStock: 0 })
}

function showOutsourceCheckDialog() {
  outsourceCheckDialogRef.value.open(detailInfo.value)
}

/** 暴露给父组件的方法 */
defineExpose({
  open,
});
</script>
