<!--
  功能：功能描述  发货单查询
  作者：陶庆峰
-->
<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" label-width="60px" label-position="left">
                <el-row :gutter="24" class="form-row">
                    <el-col :span="4">
                        <el-form-item label="转换单号" prop="RECEIVEREQUESTNAME">
                            <el-Input v-model="queryParams.RECEIVEREQUESTNAME" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="开始时间" prop="FROMDATE">
                            <el-date-picker v-model="queryParams.FROMDATE" value-format="YYYYMMDD" type="date"
                                placeholder="选择开始时间" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="结束时间" prop="TODATE">
                            <el-date-picker v-model="queryParams.TODATE" value-format="YYYYMMDD" type="date"
                                placeholder="选择结束时间" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" class="col_height">
                        <el-form-item label="单据状态" prop="params.STOCKSTATE">
                            <el-select filterable v-model="queryParams.STOCKSTATE" placeholder="下拉选择状态" style="width: 240px">
                                <el-option v-for="item in state.StockStateList" :key="item.ENUMVALUE" :label="item.DESCRIPTION"
                                           :value="item.ENUMVALUE" />
                            </el-select>
                        </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>
        <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="loading" :data="list" highlight-current-row row-key="id"
                style="width: 100%;" @row-click="selectRow">
                <template v-for="(col, index) in column" :key="index">
                    <el-table-column v-if="!col.hide" :label="col.header" :width="col.width" show-overflow-tooltip>
                        <template #default="scope">
                            <span>{{ scope.row[col.field] }}</span>
                        </template>
                    </el-table-column>
                </template>
            </el-table>
            <pagination v-if="page.total" :total="page.total" v-model:pageNumTo="page.pageNum"
                v-model:pageSizeTo="page.pageSize" @pagination="handleQuery" />
        </div>
        <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="loading_d" :data="list_d" highlight-current-row row-key="id"
                style="width: 100%;height:200px">
                <template v-for="(col, index) in column_d" :key="index">
                    <el-table-column v-if="!col.hide" :label="col.header" :width="col.width" show-overflow-tooltip>
                        <template #default="scope">
                            <span>{{ scope.row[col.field] }}</span>
                        </template>
                    </el-table-column>
                </template>
            </el-table>
            <pagination v-if="page_d.total" :total="page_d.total" v-model:pageNumTo="page_d.pageNum"
                v-model:pageSizeTo="page_d.pageSize" @pagination="handleQuery_d" />
        </div>

    </div>
</template>

<script lang="ts">
export default {
    name: "XTZHInvoice",
};
</script>

<script lang="ts" setup>
import { reactive, ref, onMounted, toRefs, getCurrentInstance } from "vue";
import { ElForm, ElMessageBox, ElTable } from 'element-plus'
import { getQueryPageList, getQueryList } from '@/api/common';
import exportDataToExcel from '@/utils/export-excel';
import { bTableHeight } from "../../../../composables/calcTableHeight";

const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(510);
const { proxy }: any = getCurrentInstance();

const queryFormRef = ref(ElForm);

const state = reactive({
    queryParams: {
        RECEIVEREQUESTNAME: '',
        FROMDATE: '',
        TODATE: '',
        STOCKSTATE: '',
    },
    page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
    },
    loading: false,
    list: [],
    column: [] as any,

    active_row: {},
    StockStateList : [
        {
            ENUMVALUE : "Created" ,
            DESCRIPTION : "创建"
        },
        {
            ENUMVALUE : "Completed" ,
            DESCRIPTION : "完成"
        }
    ],

    page_d: {
        pageNum: 1,
        pageSize: 10,
        total: 0
    },
    loading_d: false,
    list_d: [],
    column_d: [] as any,
});

const { loading, list, column, queryParams, page, page_d, loading_d, list_d, column_d, active_row } = toRefs(state)

onMounted(() => {
    state.column = [
        { header: "工厂", field: "SITENAME", hide: false },
        { header: "单号", field: "RECEIVEREQUESTNAME", hide: false },
        { header: "单据状态", field: "RECEIVEREQUESTSTATE", hide: false },
        { header: "创建时间", field: "CREATETIME", hide: false },
        { header: "创建人", field: "CREATEUSER", hide: false }
    ]

    state.column_d = [
        { header: "ERP明细ID", field: "DETAILID", hide: true },
        { header: "单号", field: "RECEIVEREQUESTNAME", hide: false },
        { header: "行号", field: "RECEIVEREQUESTDETAILNAME", hide: false },
        { header: "料号", field: "MATERIALSPECNAME", hide: false },
        { header: "SDK_ID", field: "SDK_ID", hide: true },
        { header: "规格", field: "SPECNAME", hide: false },
        { header: "阶段", field: "PHASE", hide: false },
        { header: "批号", field: "CHARGE", hide: false },
        { header: "单位", field: "MATERIALUNIT", hide: false },
        { header: "数量", field: "MATERIALQUANTITY", hide: false },
        { header: "生产日期", field: "MAKEDATE", hide: false },
        { header: "到日期", field: "EXPIRDATE", hide: false },
        { header: "库位", field: "ERPLOCATION", hide: false },
        { header: "货位", field: "LOCATIONNAME", hide: false},
        { header: "组织", field: "ERPFACTORY", hide: false },
        { header: "出库日期", field: "OUTDATE", hide: false },
        { header: "库管员", field: "WAREMANGER", hide: false },
        { header: "转换类型", field: "TYPE", hide: false },
        { header: "订单ID", field: "ERPID", hide: false },
        { header: "行ID", field: "ERPDETAILID", hide: false }
    ]
    handleQuery()
    updateTableHeight();
    window.addEventListener('resize', handleResize);
});
function handleQueryInfo() {
  state.page.pageNum = 1
  state.page_d.pageNum = 1
  handleQuery()
}
function handleQuery() {
    state.list_d = []
    state.loading = true
    queryFormRef.value.validate((isValid: boolean) => {
        if (isValid) {
            state.list = []
            getQueryPageList({
                queryId: "GetXTZHList",
                version: "10001",
                params: {
                    userId: localStorage.getItem('userId'),
                    ...state.queryParams
                },
                pageNum: state.page.pageNum,
                pageSize: state.page.pageSize,
            }).then((res: any) => {
              if(res.data.list.length == 0){
                // ElMessageBox.alert('查无数据', '提醒框', {
                //   confirmButtonText: 'OK',
                // })
                state.loading = false
              } else {
                state.list = res.data.list;
                state.page.total = res.data.total;
                state.loading = false
                state.active_row = state.list[0]
                if (state.active_row) {
                  handleQuery_d()
                }
              }
            });
        }
    });
}
function handleQuery_d() {
    state.loading_d = true
    getQueryPageList({
        queryId: "GetXTZDETAILHList",
        version: "10001",
        params: {
            userId: localStorage.getItem('userId'),
            ...state.active_row
        },
        pageNum: state.page_d.pageNum,
        pageSize: state.page_d.pageSize,
    }).then((res: any) => {
        state.list_d = res.data.list;
        state.page_d.total = res.data.total;
        state.loading_d = false
    });
}


function selectRow(row: any) {
    state.active_row = row
    handleQuery_d()
}
</script>

<style scoped lang="scss">
.form-row {
    padding-right: 100px;
    margin-bottom: 0;
}
</style>