<template>
  <!-- 添加或修改项目信息对话框 -->
  <el-drawer v-model="visible" size="90%" direction="rtl" :close-on-press-escape="false">
    <div class="page-container form-container">
      <div class="form-btns-container" style="display: flex; align-items: center;">
        <span class="title-label"><el-icon>
            <Document />
          </el-icon>
          计划详情</span>
      </div>
      <!-- 主表 -->
      <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="产品描述：">
          <el-input placeholder="请输入产品描述" v-model.trim="queryParams.productDescription" style="width: 150px" clearable
            @keyup.enter="handleQuerydaywork" @keydown.enter.prevent />
        </el-form-item>
        <el-form-item label="批次号：">
          <el-input placeholder="请输入批次号" v-model.trim="queryParams.lotCode" style="width: 130px" clearable
            @keyup.enter="handleQuerydaywork" @keydown.enter.prevent />
        </el-form-item>
        <el-form-item label="工段：">
          <el-select-v2 v-model="queryParams.deptId" :options="deptList" placeholder="请选择工段" style="width: 140px" />
        </el-form-item>
        <el-form-item label="是否回退：">
          <el-select v-model="queryParams.isFallback" placeholder="请选择是否回退">
            <el-option v-for="item in yes_no" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否废品回用：">
          <el-select v-model="queryParams.isWasteRecycling" placeholder="请选择是否废品回用">
            <el-option v-for="item in yes_no" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="info" icon="Search" @click="handleQuerydaywork">搜索</el-button>
        </el-form-item>
        <div style="margin-top: 20px;">
          <el-button type="primary" @click="handleFallback">回退</el-button>
          <el-button type="primary" @click="handleWasteRecycling">添加废品回用</el-button>
          <el-button type="primary" @click="handleQuerydaywork">分批</el-button>
          <el-button type="primary" @click="handleQuerydaywork">单批单改</el-button>
          <el-button type="primary" @click="handleQuerydaywork">多批单改</el-button>
        </div>
      </el-form>
      <!-- 列表区 -->
      <div class="el-table-container">
        <el-table ref="dayworkTable" v-loading="loading" row-key="id" @selection-change="handleSelectionChange"
          height="100%" :data="lotList">
          <el-table-column type="selection" width="40" align="center" />
          <el-table-column label="客户简称" prop="companyAlias" width="120" align="center" />
          <el-table-column label="生产计划单号" width="100" prop="productionPlanNo" align="center" />
          <el-table-column label="批次号" prop="lotCode" width="130" align="center" />
          <el-table-column label="产品描述" prop="productDescription" align="center" />
          <el-table-column label="图纸版本" width="80" prop="technologyVersion" align="center" />
          <el-table-column label="投产量" width="100" prop="productionQuantity" align="center" />
          <el-table-column label="下达日期" prop="createTime" width="100" align="center">
            <template #default="scope">
              <span>{{
    proxy.moment(scope.row.createTime).format("YYYY-MM-DD")
  }}</span>
            </template>
          </el-table-column>
          <el-table-column label="当前工段" width="100" prop="deptName" align="center" />
          <el-table-column label="当前工序" width="120" prop="processAlias" align="center" />
          <el-table-column label="回退" width="60" prop="isFallback" align="center">
            <template #default="scope">
              <dict-tag :options="yes_no" :value="scope.row.isFallback" />
            </template>
          </el-table-column>
          <el-table-column label="废品回用" width="70" prop="isWasteRecycling" align="center">
            <template #default="scope">
              <dict-tag :options="yes_no" :value="scope.row.isWasteRecycling" />
            </template>
          </el-table-column>
          <el-table-column label="批废" width="70" prop="isWaste" align="center">
            <template #default="scope">
              <dict-tag :options="yes_no" :value="scope.row.isWaste" />
            </template>
          </el-table-column>
          <el-table-column label="母批号" prop="fromCode" align="center" />
          <el-table-column fixed="right" label="操作" align="center" width="140px">
            <template #default="scope">
              <el-button v-if="scope.row.isWaste == 0" link type="danger" icon="Delete"
                @click="handlelotWaste(scope.row)">批废
              </el-button>
              <el-button v-else link type="primary" icon="View" @click="handleChecklotWaste(scope.row)">查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <lot-waste-dialog ref="lotWasteDialogRef" @handleSaveSuccess="open(currentProductionPlan)" />
      <waste-recycling-dialog ref="wasteRecyclingDialogRef"></waste-recycling-dialog>

    </div>
  </el-drawer>
</template>
<script setup>
import {
  getDayworkInfo, getDeptList
} from "@/api/business/daywork.js";
import lotWasteDialog from './dayworkWasteDialog.vue'
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { yes_no } = proxy.useDict("yes_no");
/** 表单抽屉 页变量 */
const currentProductionPlan = ref({});
const loading = ref(false);
const lotList = ref([])
const visible = ref(false);
const deptList = ref([])
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  lotCode: "",
  deptId: null,
  isFallback: "",
  isWasteRecycling: "",
});
const queryDeptParams = ref({
  lineNumber: '',
  productionPlanNo: ''
})

/***********************  方法区  ****************************/
/** 打开抽屉 */
const open = (row) => {
  currentProductionPlan.value = row;
  queryParams.value.lineNumber = row.lineNumber
  queryParams.value.productionPlanNo = row.productionPlanNo
  queryDeptParams.value.lineNumber = row.lineNumber
  queryDeptParams.value.productionPlanNo = row.productionPlanNo
  getDeptList(queryDeptParams.value).then(res => {
    if (res.code == 200) {
      deptList.value = res.data
    }
  })
  //获取工段信息
  getDayworkList()
  visible.value = true;
};
/**获取生产计划列表 */
function getDayworkList() {
  loading.value = true;
  getDayworkInfo(queryParams.value).then(res => {
    if (res.code == 200) {
      lotList.value = res.rows
      loading.value = false
    }
  })
}
/**搜索 */
function handleQuerydaywork() {
  getDayworkList()
}
/**批废 */
function handlelotWaste(row) {
  //flag为true时代表批废
  row.flag = false
  proxy.$refs.lotWasteDialogRef.open(row)
}
/**查看 */
function handleChecklotWaste(row) {
  row.flag = true
  proxy.$refs.lotWasteDialogRef.open(row)
}
/**
 * 对话框关闭 事件
 */
function close() {
  visible.value = false;
}

/**搜索栏清空 */
function reset() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    lotCode: "",
    deptId: null,
    isFallback: "",
    isWasteRecycling: ""
  }
  proxy.resetForm("queryRef");
}

/** 取消按钮 */
const handleCancel = () => {
  visible.value = false;
};

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