<!--
  功能：功能描述  空托盘出库
  作者：Libra
  邮箱：mingxichen@163.com
  时间：2024年04月12日 09:54:29
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：LIWENHU
  修改时间：2024/04/15 10:26:12   2024/05/18 9:38:23
-->
<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
    <div class="vue-element-page-table">
      <div class="page-table-operate">
        <div class="page-table-title">
          <div class="page-table-title-left">
            <span>托盘 信息</span>
          </div>
        </div>
      </div>
      <el-table border v-loading="palletTableConfig.loading" :data="state.TPRWList" highlight-current-row row-key="id"
        style="width: 100%" height="740px" @selection-change="handle">
        <el-table-column type="selection" width="40" />
        <template v-for="(col, index) in palletTableConfig.column" :key="index">
          <el-table-column v-if="!col.hide" :label="col.header" show-overflow-tooltip>
            <template #default="scope">
              <span>{{ scope.row[col.field] }}</span>
            </template>
          </el-table-column>
        </template>
      </el-table>
      <pagination v-if="state.total > 0" :total="state.total" v-model:pageNumTo="state.queryParams.pageNum"
        v-model:pageSizeTo="state.queryParams.pageSize" @pagination="handleQuery" />
    </div>

    <div class="vue-element-page-table flex">
      <div>
        <el-row class="page-search" justify="space-between" align="middle">
          <div class="page-search-btns">
            <el-button type="primary" @click="handTPStockOut" v-loading="state.loading"
              :disabled="state.button_state">确认出库</el-button>
          </div>
        </el-row>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
export default {
  name: "emptyTrayOut",
};
</script>
<script lang="ts" setup>
import { reactive, ref, onMounted, toRefs, getCurrentInstance, nextTick } from "vue";
import { ElForm, ElMessageBox, ElTable } from "element-plus";
import { getQueryList, BpelEvent, getQueryPageList } from '@/api/common';
const queryFormRef = ref(ElForm);
const portFormRef = ref(ElForm)
const state: any = reactive({
  TPRWList: [],
  button_state: false,
  loading: false,
  column3: [],
  queryParams: {
    queryId: "",
    version: "",
    params: {
      SITENAME: localStorage.getItem("siteName") == '' || 'SDK',//localStorage.getItem("siteName"),
    },
    pageNum: 1,
    pageSize: 10
  },
  ChangeParams: {
    header: {
      MESSAGENAME: '',
      LANGUAGE: 'Chinese',

    },
    body: {
      SITENAME: localStorage.getItem("siteName") == '' || 'SDK',//localStorage.getItem("siteName"),
      TRAYNAME: '',
      TRANSPORTCOMMANDTYPE: 'OUT',//搬送类型
      OUTDESTINATION: '',
      QUANTITY: 0,
      Flag: 'TP'//立库标志
    }
  },
})

const palletTableConfig = ref({
  loading: false,
  isAll: false,
  data: [],
  column: [
    { header: "工厂", field: "SITENAME", hide: false },
    { header: "托盘号", field: "DURABLENAME", hide: false },
    { header: "数量", field: "ASSIGNEDPACKINGQUANTITY", hide: false },
    { header: "库位", field: "LOCATIONNAME", hide: false },
  ]
})
onMounted(() => {
  // handleQuery();
});
const portTableConfig = ref({
  loading: false,
  data: [],
  column: [
    { header: "Port ID", field: "PORTID", hide: false },
    { header: "Port 状态", field: "PORTSTATUS", hide: false },
    { header: "任务数量", field: "TASKQTY", hide: false },
  ]
})
const handleQuery = () => {
  state.queryParams.queryId = 'GetDURABLENAME_LK'
  state.queryParams.version = '00001'
  getQueryPageList(state.queryParams).then((res: any) => {
    if(res.data.list.length == 0){
      ElMessageBox.alert('查无数据', '提醒框', {
        confirmButtonText: 'OK',
      })
      return;
    } else {
      state.TPRWList = res.data.list
      state.total = res.data.total
    }
  });
}
async function handTPStockOut() {
  if (state.column3.length > 0) {
    state.loading = true
    state.button_state = true
    state.ChangeParams.header.MESSAGENAME = 'TransportCommandRequest';
    let object = []
    let object2 = []
    for (let item of state.column3) {
      object.push(item.DURABLENAME)
      object2.push(item.LOCATIONNAME)
      state.QUANTITY += parseInt(item.ASSIGNEDPACKINGQUANTITY)
    }
    state.ChangeParams.body.TRAYNAME = object.join(';')
    state.ChangeParams.body.OUTDESTINATION = object2.join(';')
    await BpelEvent(state.ChangeParams).then((res: any) => {
      console.log(res)
      if (res.code == '500') {
        ElMessageBox.alert(res.data.msg, '提醒框', {
          confirmButtonText: 'OK',
        })
        state.loading = false
        state.button_state = false
      } else if (res.code == 'UndefinedCode') {
        ElMessageBox.alert(res.data.msg, '提醒框', {
          confirmButtonText: 'OK',
        })
        state.loading = false
        state.button_state = false
      } else if (res.code == '0') {
        ElMessageBox.alert('任务已经下发', '提醒框', {
          confirmButtonText: 'OK',
        })
        handleQuery()
        state.loading = false
        state.button_state = false
      }
    })
  } else {
    ElMessageBox.alert('请先选择下搬任务', '提醒框', {
      confirmButtonText: 'OK',
    })
    state.loading = false
    state.button_state = false
  }
  state.loading = false
  state.button_state = false
}

function handle(selection: any) {
  state.column3 = selection
}
</script>

<style scoped>
.flex {
  display: flex;
}

.flex>div {
  flex: 1;
}
</style>