<!--
  功能：功能描述  优先级调整
  作者：Libra
  邮箱：mingxichen@163.com
  时间：2024年04月12日 09:54:29
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：LIWENHU
  修改时间：2024/04/15 10:26:12
-->
<template>
    <div class="vue-element-page-wrap" ref="tableContainer">
        <el-row class="page-search" justify="space-between" align="middle">
            <el-form ref="queryFormRef" :model="state" :inline="true">
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="库区" prop="state.queryParams.params">
                            <el-select v-model="state.queryParams.params.DESCRIPTION" placeholder="请选择库区"
                                style="width: 240px">
                                <el-option v-for="item in state.AREANAMELIST" :key="item.value" :label="item.label"
                                    :value="item.label" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>
            </el-form>
            <div class="page-search-btns">
                <el-button type="primary" @click="queryInfo">搜索</el-button>
            </div>
        </el-row>

        <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 :height="tableHeight" border v-loading.fullscreen.lock="state.loading" :data="state.TaskAREANAMELIST"
                highlight-current-row row-key="id" style="width: 100%" height="625px"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <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="handQuery" />
        </div>

        <div class="vue-element-page-table flex">
            <div>
                <el-row class="page-search" justify="space-between" align="middle">
                    <el-form ref="portFormRef" :model="portFormRefData" :inline="true" label-position="left">
                        <el-row :gutter="20">
                            <el-col :span="8">
                                <el-form-item label="优先顺序" prop="portFormRefData.SEARCHTYPE">
                                    <el-select v-model="state.ChangeParams.body.TRANSPORTSTATE" placeholder="Select"
                                        style="width: 240px">
                                        <el-option v-for="item in portFormRefData.FIRSTORDERLIST" :key="item.value"
                                            :label="item.label" :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div class="page-search-btns">
                        <el-button type="primary" @click="submit" :disabled="state.button_state" v-loading="state.loadings">确认</el-button>
                    </div>
                </el-row>
            </div>

        </div>
    </div>
</template>
<script lang="ts">
export default {
  name: "LibaryPriority",
};
</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';
import { IFTRUE } from '@/utils/CommonUtil'
import { bTableHeight } from "../../../composables/calcTableHeight";

const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(220);
const queryFormRef = ref(ElForm);
const portFormRef = ref(ElForm)
const state = reactive({//搜索类型
    SEARCHTYPE: '1',
    button_state: false,
    loadings : false,
    loading: false,
    total: 0,
    queryParams: {
        queryId: '',
        version: '',
        params: {
            AREANAME: [] as any,
            DESCRIPTION: '',
        },
        pageNum: 1,
        pageSize: 12
    },
    ChangeParams: {
        header: {
            MESSAGENAME: 'TransportCommandChange',
            LANGUAGE: 'Chinese',
            Flag: 'LK'//立库标志
        },
        body: {
            TRANSPORTCOMMANDID: '',//任务号
            TRANSPORTSTATE: '',
            SITENAME: 'SDK',
            TRANSPORTCOMMANDTYPE: ''
        }
    },
    AREANAMELIST: [
        { value: '', label: '' },
    ],
    TaskAREANAMELIST: [],
    TransportTaskChoose: [] as any,

})
const {
    SEARCHTYPE,
    queryParams,
    AREANAMELIST,
    TransportTaskChoose,
    loading
} = toRefs(state);
const portFormRefData = ref({
    FIRSTORDER: '',
    FIRSTORDERLIST: [
        { value: 'Normal', label: '正常' },
        { value: 'Prior', label: '优先' },
        { value: 'Cancel', label: '取消' },
        { value: 'Pause', label: '暂停' },
    ],
    STOCKSELECT: [],
    STOCKSELECTLIST: [
        { value: '1', label: 'TYPE1', children: [{ value: '1-1', label: 'TYPE1-1' }, { value: '1-2', label: 'TYPE1-2' }] },
        { value: '2', label: 'TYPE2', children: [{ value: '2-1', label: 'TYPE2-1' }, { value: '2-2', label: 'TYPE2-2' }] }
    ]
})

const palletTableConfig = ref({
    isAll: false,
    data: [],
    column: [
        { header: "任务号", field: "TRANSPORTCOMMANDID", hide: false },
        { header: "单号", field: "SHIPREQUESTDETAILNAME", hide: false },
        { header: "料号", field: "MATERIALSPEC", hide: false },
        { header: "批次号", field: "MATERIALPACKING", hide: false },
        { header: "数量", field: "MATERIALQUANTITY", hide: false },
        { header: "状态", field: "TRANSPORTSTATE_CN", hide: false },
        { header: "优先级", field: "PRIORITY", hide: false },
        { header: "搬送类型", field: "TRANSPORTCOMMANDTYPE", hide: false },
    ],
})

onMounted(() => {
    handQuery();
    // QueryAREANAME()
    updateTableHeight();
    window.addEventListener('resize', handleResize);
});
//选择搬送任务
function handleSelectionChange(selection: any) {
    console.log('调用了', selection)
    state.TransportTaskChoose = selection
}
function  queryInfo () {
    state.queryParams.pageNum = 1
    handleQuery()
}
//根据库区查任务
const handleQuery = () => {
  state.TaskAREANAMELIST = []
    state.queryParams.queryId = 'GetAREANAMEList_1'
    state.queryParams.version = '00002'
    getQueryPageList(state.queryParams)
        .then((res: any) => {
            console.log('11',res.data);
            if(res.data.list.length == 0){
              ElMessageBox.alert('查无数据', '提醒框', {
                confirmButtonText: 'OK',
              })
            } else {
              state.TaskAREANAMELIST = res.data.list;
              state.total = res.data.total;
            }
        })
        .catch(() => { });

}
//查询库区
const handQuery = () => {
    state.queryParams.queryId = 'GetAREANAMEList'
    state.queryParams.version = '00001'
    state.AREANAMELIST = [{ value: '', label: '' },];
    getQueryList(state.queryParams)
        .then((res: any) => {
            for (let i = 0; i < res.data.length; i++) {
                state.AREANAMELIST.push({ label: res.data[i].DESCRIPTION, value: res.data[i].AREANAME });
            }
        })
        .catch(() => { });
}
//查询任务
function QueryAREANAME() {

    state.queryParams.queryId = 'GetTaskAREANAMELIST'
    state.queryParams.version = '00001'
    getQueryPageList(state.queryParams)
        .then((res: any) => {
            console.log(res.data);
            state.TaskAREANAMELIST = res.data.list
            state.total = res.data.total
        })
        .catch((error: any) => {
            ElMessageBox.alert(error, '提醒框', {
                confirmButtonText: 'OK',
            })
        });
}
//确认提交按钮
const submit = async () => {

    if (state.TransportTaskChoose.length > 0) {
        try {
            state.loading = true
            state.button_state = true
          state.loadings = true;
            let column1 = [];
            let column2 = [];
            for (const item of state.TransportTaskChoose) {
                column1.push(item.TRANSPORTCOMMANDID);
                column2.push(item.TRANSPORTCOMMANDTYPE);
            }
            // 如果 column1 数组不为空，则将其转换为一个由“、”分隔的字符串  
            if (column1.length > 0) {
                state.ChangeParams.body.TRANSPORTCOMMANDID = column1.join(";");
            } else {
                // 如果 packingNames 为空，可以根据需要设置初始值，或者保持为空字符串  
                state.ChangeParams.body.TRANSPORTCOMMANDID = "";
            }
            // 如果 column1 数组不为空，则将其转换为一个由“、”分隔的字符串  
            if (column2.length > 0) {
                state.ChangeParams.body.TRANSPORTCOMMANDTYPE = column2.join(";");
            } else {
                // 如果 packingNames 为空，可以根据需要设置初始值，或者保持为空字符串  
                state.ChangeParams.body.TRANSPORTCOMMANDTYPE = "";
            }

            await BpelEvent(state.ChangeParams).then((res: any) => {
                if (IFTRUE(res)) {
                    QueryAREANAME()
                    state.loading = false;
                    state.button_state = false
                  state.loadings = false;
                } else {
                    ElMessageBox.alert(res.data.RETURNCODE + '->' + res.data.RETURNMESSAGE, '', {
                        confirmButtonText: 'OK',
                    })
                    state.button_state = false
                  state.loadings = false;
                }

            })
                .catch((error) => {
                    ElMessageBox.alert(error, '提醒框', {
                        confirmButtonText: 'OK',
                    })
                    QueryAREANAME()
                    state.button_state = false
                  state.loadings = false;
                });

        } catch (error) {
            ElMessageBox.alert("运行错误", '提醒框', {
                confirmButtonText: 'OK',
            })
            state.button_state = false
          state.loadings = false;
        } finally {
            state.loading = false
            state.button_state = false
          state.loadings = false;
        }
    } else {
        ElMessageBox.alert('请先选择任务', '提示框', {
            confirmButtonText: 'OK',
        })
        state.button_state = false
      state.loadings = false;
    }


}
</script>

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

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