<!--
  功能：功能描述  入库单查询
  作者：陶庆峰
-->
<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="ERPFACTORY">
                            <el-select v-model="queryParams.ERPFACTORY" placeholder="下拉选择" style="width: 160px">
                                <el-option v-for="item in state.formDataRefData" :key="item.ERPFACTORY" :label="item.DESCRIPTION"
                                    :value="item.ERPFACTORY" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <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="RECEIVEREQUESTSTATE">
                            <el-select v-model="queryParams.RECEIVEREQUESTSTATE" placeholder="下拉选择" style="width: 160px">
                                <el-option v-for="item in state.orderStatus" :key="item.LABEL" :label="item.LABEL"
                                    :value="item.VALUE" />
                            </el-select>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :span="4">-->
<!--                      <el-form-item label="仓库" prop="ERPLOCATION">-->
<!--                        <el-Input v-model="queryParams.ERPLOCATION" clearable />-->
<!--                      </el-form-item>-->
<!--                    </el-col>-->
                    <el-col :span="4">
                      <el-form-item label="料号" prop="MATERIALSPECNAME">
                        <el-Input v-model="queryParams.MATERIALSPECNAME" clearable />
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item label="品名" prop="DESC_CN">
                        <el-Input v-model="queryParams.DESC_CN" clearable />
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <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="6">
                      <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-row>
            </el-form>
            <div class="page-search-btns">
                <el-button type="primary" @click="handleQueryInfo">搜索</el-button>
                <el-button @click="resetQuery">重置</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-button type="primary" @click="exportExcel">导出</el-button>
                </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-button type="primary" @click="exportExcel_d">导出</el-button>
                </div>
            </div>
            <el-table border v-loading="loading_d" :data="list_d" highlight-current-row row-key="id" height="200px"
                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: 'StockIn'
};
</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 { getORG } from '@/api/auth';
import { bTableHeight } from "../../../../composables/calcTableHeight";

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


const state = reactive({
    formDataRefData: [] as any,
    orderStatus: [{
        LABEL:"未完成",
        VALUE:"Created"
    },{
        LABEL:"完成",
        VALUE:"Completed"
    }],
    queryParams: {
        RECEIVEREQUESTNAME: '',
        ERPFACTORY: '',
        RECEIVEREQUESTSTATE: "",
        ERPLOCATION : '',
        FROMDATE : '',
        TODATE : '',
        DESC_CN: "",
        MATERIALSPECNAME: ""
    },
    page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
    },
    loading: false,
    list: [],
    column: [] as any,

    active_row: {},

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

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

onMounted(() => {
    state.column = [
        { header: "组织", field: "ERPFACTORY", hide: false },
        { header: "组织(CN)", field: "ORGNAME", hide: false },
        { header: "订单编号", field: "RECEIVEREQUESTNAME", hide: false },
        { header: "料号", field: "MATERIALSPECNAME", hide: true },
        { header: "品名", field: "DESC_CN", hide: true },
        { header: "出入库类型编码", field: "RECEIVEREQUESTTYPE", hide: false },
        { header: "订单状态", field: "RECEIVEREQUESTSTATE", hide: false },
        { header: "供应商编号", field: "SUPPLIERNO", hide: false },
        { header: "单据日期" , field: "ORDERDATE", hide: false},
        { header: "供应商名称", field: "SUPPLIERNAME", hide: true },
        { header: "创建日期", field: "CREATETIME", hide: true },
        { header: "最后操作名称", field: "LASTEVENTNAME", hide: true },
        { header: "最后操作时间", field: "LASTEVENTTIME", hide: true },
        { header: "最后操作人", field: "LASTEVENTUSER", hide: true },
        { header: "最后操作注释", field: "LASTEVENTCOMMENT", hide: true },
    ]
    state.column_d = [
        { header: "工厂", field: "SITENAME", hide: true },
        { header: "订单编号", field: "RECEIVEREQUESTNAME", hide: false },
        { header: "行号", field: "RECEIVEREQUESTDETAILNAME", hide: false },
        { header: "发出仓库", field: "ERPLOCATION", hide: false },
        { header: "接收仓库", field: "ERPRECEIVELOCATION", hide: true },
        { header: "料号", field: "MATERIALSPECNAME", hide: false },
        { header: "品名", field: "DESC_CN", hide: false },
        { header: "物料规格", field: "DESCRIPTION ", hide: false },
        { header: "SDK规格", field: "SPECNAME", hide: false },
        { header: "阶段", field: "ms.PHASE", hide: false },
        { header: "请求数量", field: "REQUESTQUANTITY", hide: false },
        { header: "接受数量", field: "RECEIVEDQUANTITY", hide: false },

        { header: "组织", field: "ERPFACTORY", hide: true },
        { header: "退货标识", field: "RETURNFLAG", hide: true },
        { header: "注释", field: "DESCRIPTION", hide: true },
        { header: "Hold状态", field: "HOLDSTATE", hide: true },
        { header: "计划日期", field: "PLANDATE", hide: true },
        { header: "创建人", field: "CREATEUSER", hide: true },
        { header: "创建时间", field: "CREATETIME", hide: true },
        { header: "最后操作名称", field: "LASTEVENTNAME", hide: true },
        { header: "最后操作时间", field: "LASTEVENTTIME", hide: true },
        { header: "最后操作人", field: "LASTEVENTUSER", hide: true },
        { header: "最后操作注释", field: "LASTEVENTCOMMENT", hide: true },
    ]
    handleQuery()
    hangERPFactoryQuery()
    updateTableHeight();
    window.addEventListener('resize', handleResize);
});

function hangERPFactoryQuery() {
    getORG({ orgNo: null}).then((res: any) => {
        state.formDataRefData = res.resultObj
    })

}
function handleQueryInfo() {
  state.page.pageNum = 1
  state.page_d.pageNum = 1
  handleQuery()
}
function handleQuery() {
    state.loading = true
    state.list = []
    state.list_d = []
    queryFormRef.value.validate((isValid: boolean) => {
        if (isValid) {
            getQueryPageList({
                queryId: "GetMaterialReceiveRequestList",
                version: "10002",
                params: {
                    userId: localStorage.getItem('userId'),
                    ...state.queryParams
                },
                pageNum: state.page.pageNum,
                pageSize: state.page.pageSize,
            }).then((res: any) => {
                state.loading = false
              if(res.data.list.length ==0 ){
                ElMessageBox.alert('查无数据', '提醒框', {
                  confirmButtonText: 'OK',
                })
              } else {
                state.list = res.data.list;
                for(let i=0;i<state.list.length;i++)
                {
                  var dates = new Date(state.list[i].ORDERDATE).toJSON();
                  state.list[i].ORDERDATE=new Date(+new Date(dates) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
                  console.log( state.list[i].ORDERDATE);
                }
                state.page.total = res.data.total;
                state.loading = false
                state.active_row = state.list[0]
                if (state.active_row) {
                  handleQuery_d()
                }
              }

            });
        }
    });
}

function exportExcel() {
    getQueryList({
        queryId: "GetMaterialReceiveRequestList",
        version: "10001",
        params: {
            userId: localStorage.getItem('userId'),
            ...state.queryParams
        }
    }).then((res: any) => {
        if (res.data.length > 0) {
            exportDataToExcel(res.data, '入库单列表')
        } else {
            proxy.$ElMessage.warning('当前查询没有数据可以导出');
        }
    });
}

function handleQuery_d() {
    state.loading_d = true
    getQueryPageList({
        queryId: "GetMaterialReceiveRequestDetailList",
        version: "10001",
        params: {
            userId: localStorage.getItem('userId'),
            DESC_CN: state.queryParams.DESC_CN,
            MATERIALSPECNAME: state.queryParams.MATERIALSPECNAME,
            ...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 exportExcel_d() {
    getQueryList({
        queryId: "GetMaterialReceiveRequestDetailList",
        version: "10001",
        params: {
            userId: localStorage.getItem('userId'),
            DESC_CN: state.queryParams.DESC_CN,
            MATERIALSPECNAME: state.queryParams.MATERIALSPECNAME,
            ...state.active_row
        },
    }).then((res: any) => {
        if (res.data.length > 0) {
            exportDataToExcel(res.data, '入库单详情列表')
        } else {
            proxy.$ElMessage.warning('当前查询没有数据可以导出');
        }
    });
}

function resetQuery() {
  state.queryParams.TODATE = '';
  state.queryParams.FROMDATE = '';
  state.queryParams.ERPLOCATION = '';
  state.queryParams.RECEIVEREQUESTSTATE = '';
  state.queryParams.RECEIVEREQUESTNAME = '';
  state.queryParams.ERPFACTORY = '';
    queryFormRef.value.resetFields();
    handleQuery();
}

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

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