<!--品质待检看板-->
<!--页面渲染区域-->
<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
    <el-row class="page-search" justify="space-between" align="bottom">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="80px" label-position="left">
        <el-row :gutter="20">
          <el-col :span="5" class="col_height">
            <el-form-item label="数量" prop="params.SumCount">
              <el-Input v-model="queryParams.params.SumCount" disabled clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6" class="col_height">
            <el-form-item label="组织" prop="params.ERPFACTORY">
              <el-select filterable v-model="queryParams.params.ERPFACTORY" placeholder="下拉选择组织" style="width: 240px"
                         @change="GetERPLocationForList">
                <el-option v-for="item in ERPFactoryList" :key="item.ERPFACTORY" :label="item.DESCRIPTION"
                           :value="item.ERPFACTORY" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="col_height">
            <el-form-item label="仓库" prop="params.ERPLOCATION">
              <el-select v-model="state.ChangeERPLocation" multiple clearable collapse-tags placeholder="请选择仓库"
                         popper-class="custom-header" :max-collapse-tags="1" style="width: 240px"
                         @visible-change="GetLocationListForSomeERPLocation" filterable>
                <template #header>
                </template>
                <el-option v-for="item in ERPLocationList" :key="item.ENUMVALUE" :label="item.DESCRIPTION"
                           :value="item.ENUMVALUE" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1"></el-col>
          <el-col :span="5" class="col_height">
            <el-form-item label="物料编号" prop="params.MATERIALSPECNAME">
              <el-Input v-model="queryParams.params.MATERIALSPECNAME" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="page-search-btns">
        <el-button type="primary" @click="handleQueryInfo">搜索</el-button>
      </div>
    </el-row>
    <!--table 区域-->
    <div class="vue-element-page-table">
      <div class="page-table-operate">
        <div class="page-table-title">
          <span>BOX历史信息</span>
        </div>
      </div>
      <el-table border v-loading="loading" :data="state.MaterialDetailList" highlight-current-row row-key="id"
        style="width: 100%" :height="tableHeight">
        <template v-for="(col, index) in Column" :key="index">
          <el-table-column v-if="!col.hide" :label="col.header" show-overflow-tooltip>
            <template #default="scope">
              <span v-if="col.field === 'status'">
                <span v-if="scope.row[col.field]" class="table-status-active">启用</span>
                <span v-else class="table-status-inActive">禁用</span>
              </span>
              <span v-else>{{ scope.row[col.field] }}</span>
            </template>
          </el-table-column>
        </template>
        <template v-slot:empty>
          <svg-icon icon-class="noData" width="160px" height="160px" />
        </template>
      </el-table>
      <div>
        <pagination v-if="total > 0" :total="total" v-model:pageNumTo="queryParams.pageNum"
          v-model:pageSizeTo="queryParams.pageSize" @pagination="handleQuery" />
      </div>
    </div>
  </div>
</template>
<script lang="ts">
export default {
  name: 'materialQualityInspectedQuery'
};
</script>
<!--script区域-->
<script lang="ts" setup>
/*引入区域*/
import { reactive, ref, onMounted, toRefs, getCurrentInstance, nextTick } from "vue";
import { ElForm, ElMessageBox, ElTable } from "element-plus";
import { getQueryList, getQueryPageList } from "@/api/common";
import {areAllNotEmpty, msToDate} from '@/utils/CommonUtil';
import { getORG } from '@/api/auth';
import { bTableHeight } from "../../../../composables/calcTableHeight";

const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(140);
/*对象生成赋值区域*/

const queryFormRef = ref(ElForm);
const size = ref<'default' | 'large' | 'small'>('small');//formdata时间设置
const state = reactive({
  queryParams: {
    queryId: "GetInspectedList",
    version: "WEB10007",
    params: {
      SITENAME: localStorage.getItem("siteName"),
      MATERIALSPECNAME: "",
      ERPFACTORY: "",
      ERPLOCATION: "",
      //显示部分
      sum: 0,
      sumPREOQA: 0,
      SumCount: 0
    },
    pageNum: 1,
    pageSize: 20
  },
  queryParamsERPFactory: {
    queryId: "GetErpFactoryList",
    version: "WEB00001",
    params: {
      SITENAME: localStorage.getItem("siteName") || 'SDK',
    },
    pageNum: 1,
    pageSize: 10
  },
  queryParamsERPLocation: {
    queryId: "GetErpLocationList",
    version: "WEB00001",
    params: {
      SITENAME: localStorage.getItem("siteName") || 'SDK',
      ERPFACTORYNAME: '',
    },
    pageNum: 1,
    pageSize: 10
  },
  operateParams: {
    date: new Date(),
  },
  Column: [] as any,
  total: 0,
  loading: false,
  BoxHistoryList: [] as any,
  ERPFactoryList: [] as any,
  ERPLocationList: [] as any,
  MaterialDetailList: [] as any,
  ChangeERPLocation : [] as any,
});
const {
  queryParams,
  Column,
  total,
  loading,
  BoxHistoryList,
  operateParams,
  MaterialDetailList,
  ERPFactoryList,
  ERPLocationList,
  queryParamsProcessGroupName,
  ProcessGroupNameList,

} = toRefs(state);
/*钩子函数区域*/
onMounted(() => {
  state.Column = [
    { header: "物料编号", field: "MATERIALSPECNAME", hide: false },
    { header: "标签", field: "MATERIALPACKINGNAME", hide: false },
    { header: "批次", field: "CHARGE", hide: false },
    { header: "组织", field: "ERPFACTORY", hide: false },
    { header: "仓库", field: "ERPLOCATION", hide: false },
    { header: "货位", field: "LOCATIONNAME", hide: false },
    { header: "物料规格", field: "DESCRIPTION", hide: false },
    // { header: "抬头文本", field: "RECEIVEACTNO", hide: false },
    { header: "供应商名称", field: "SUPPLIERNAME", hide: false },
    { header: "物料总数量", field: "MATERIALQUANTITY", hide: false },
    { header: "单位", field: "MATERIALUNIT", hide: false },
    // { header: "质检批", field: "MATERIALPROCESSGROUPNAME", hide: false },
    { header: "最后操作的注解", field: "LASTEVENTCOMMENT", hide: false },
    { header: "入库时间", field: "RECEIVETIME", hide: false },
    { header: "入库天数", field: "RKDATE", hide: false },
    { header: "检验周期", field: "INSPECTION_CYCLE", hide: false },
    { header: "待检天数", field: "OQADATE", hide: false },
    // { header: "Reason 加急质检标志", field: "REMARK", hide: false },



  ];
  let pwd: any = localStorage.getItem('remeberPwd');
  // state.orgNo = JSON.parse(pwd).orgNo;
  // state.queryParams.params.ERPFACTORY = JSON.parse(pwd).orgNo;
  ERPFactoryQuery();
  // ERPLocationQuery();
  updateTableHeight();
  window.addEventListener('resize', handleResize);
  
});
function handleQueryInfo() {
  state.queryParams.pageNum = 1
  handleQuery()
}
/*其它函数区域*/
function handleQuery() {
  state.MaterialDetailList = []
  state.queryParams.params.ERPLOCATION = ''
  if (state.ChangeERPLocation[0] != '')
    for (let i = 0; i < state.ChangeERPLocation.length; i++) {
      state.queryParams.params.ERPLOCATION = state.queryParams.params.ERPLOCATION + state.ChangeERPLocation[i] + ','
    }
    state.MaterialDetailList = []
  getQueryPageList(state.queryParams)
    .then((res: any) => {
      if(res.data.list.length ==0 ){
        ElMessageBox.alert('查无结果', '提醒框', {
          confirmButtonText: 'OK',
        })
      } else {
        state.MaterialDetailList = res.data.list;
        state.total = res.data.total;
        InAdvanceQuery();
      }
    })
    .catch(() => { });
}

function GetLocationListForSomeERPLocation(value: any) {
  if (!areAllNotEmpty(state.queryParams.params.ERPFACTORY)) {
    proxy.$ElMessage.warning('当前还未选择组织');
    state.ChangeERPLocation = []
    state.ERPLocationList = []
    return
  }
  if (!areAllNotEmpty(state.ChangeERPLocation)) {
    return
  }
  if (!value) {
    let ERPLOCATIONList = state.ChangeERPLocation;
    getQueryList({
      queryId: "GetLocationListForSomeERPLocation",
      version: "00001",
      params: {
        SITENAME: localStorage.getItem("siteName") || 'SDK',
        ERPLOCATION: ERPLOCATIONList.length ? ERPLOCATIONList.join(','): ""
      },
    })
            .then((res: any) => {
              state.LocationList = res.data;
              state.LocationList.unshift({
                STORAGENAME: ''
              });
            })
            .catch(() => { });
  }

}

//提前查询数量
function InAdvanceQuery() {
  getQueryList(state.queryParams)
    .then((res: any) => {
      console.log(res.data.length)
      if (res.data.length > 0) {
        for (let i = 0; i < res.data.length; i++) {
          console.log(res.data[i].MATERIALQUANTITY)
          state.queryParams.params.SumCount += res.data[i].MATERIALQUANTITY
        }
      }
    })
    .catch(() => { });
}
//ERP库位查询
function ERPLocationQuery() {
  state.queryParamsERPLocation.params.ERPFACTORYNAME = state.queryParams.params.ERPFACTORY
  getQueryList(state.queryParamsERPLocation)
          .then((res: any) => {
            state.ERPLocationList = res.data;
            // state.ERPLocationList.unshift({
            //   ENUMVALUE: ''
            // });
          })
          .catch(() => { });
}
//ERP工厂查询
function ERPFactoryQuery() {
  getORG({ orgNo: null})
          .then((res: any) => {
            state.ERPFactoryList = res.resultObj;
            state.ERPFactoryList.unshift({
              ERPFACTORY: ''
            });
          })
          .catch(() => { });
}
function GetERPLocationForList(value: any) {
  ERPLocationQuery();
}


</script>
<!--style 区域-->
<style>
.col_height {
  padding-top: 3.5px;
  padding-bottom: 3.5px;
}
</style>