<template>
    <div>
        <el-card :body-style="cardBodyStyle">
            <el-form :inline="true" status-icon>
                <el-row :span="24">
                    <el-col :span="5">
                        <el-form-item label="库房名称" prop="storeNm">
                            <el-input v-model="query.storeNm" placeholder="请输入" clearable style="width: 200px;">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item label="物料名称" prop="itemNm">
                            <el-input v-model="query.itemNm" placeholder="请输入" clearable style="width: 200px;">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item label="物料类型" prop="itemType">
                            <el-select v-model="query.itemType" style="width: 200px" placeholder="物料类型" clearable>
                                <el-option v-for="item in dictList.item_sort3" :key="item.dicCode" :label="item.dicName"
                                    :value="item.dicCode" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item label="日期" prop="transactionDate">
                            <el-date-picker v-model="query.transactionDate" type="datetimerange" align="right"
                                unlink-panels range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
                                value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss" style="width: 300px;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-card>
        <ButtonBar area="top" @search="handleSearch"></ButtonBar>
        <el-card :body-style="cardBodyStyle">
            <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange">
                <el-table-column type="selection" fixed></el-table-column>
                <el-table-column type="index" label="序号" align="center" width="60">
                    <template #default="scope">
                        <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="compCd" label="公司代码" width="160" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="deptCd" label="部门代码" width="160" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemCd" label="物料编码" width="160" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemNm" label="物料名称" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemType" label="物料类型" width="100" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="periodIn" label="业务单据号" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="qty" label="主数量" width="180" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="uom" label="计量单位" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="storeCd" label="库房号" width="160" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="areaCd" label="区号" width="160" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="stackCd" label="垛位号" width="160" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="transactionType" label="业务类型" width="260" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="qtyWet" label="湿基量" width="200" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="transactionDate" label="事务处理时间" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="transactionTable" label="相关表" width="180" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="transactionId" label="相关表ID" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="salOrdNo" label="销售订单号" width="160" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="salOrdLn" label="销售订单行号" width="160" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="purOrdNo" label="采购订单号" width="160" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="purOrdLn" label="采购订单行号" width="260" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="prdPlanNo" label="生产计划号" width="200" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="tradePartnerId" label="客商ID" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="tradePartnerCode" label="客商编码" width="180" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="tradePartnerName" label="客商名称" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="remarks" label="备注" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>

                <!-- <el-table-column label="操作" header-align="center" align="center" width="180px" fixed="right">
                    <template #default="scope">
                        <el-button plain type="primary" @click="handleEdit(scope.row)">查看</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
        </el-card>
        <el-card :body-style="cardBodyStyle">
            <el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes" :total="totalRows"
                background="background" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
                @current-change="handleCurrentChange">
            </el-pagination>
        </el-card>
        <el-dialog :close-on-click-modal="false" draggable title="编辑" v-model="dialogVisible" width="1200px" top="5vh"
            @open="openDialog">
            <el-form :model="form" label-width="80px" label-position="right" inline ref="ruleFormRef" :rules="rules">
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="success" @click="handleSave(ruleFormRef)">保存</el-button>
                    <el-button type="danger" @click="handleCancelSave">取消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import ButtonBar from '@/components/ButtonBar.vue'

import {
    ref,
    reactive,
    onMounted,
    getCurrentInstance
} from 'vue'

const {
    proxy
} = getCurrentInstance()

import tools from '@/tools'
import { ElMessage } from 'element-plus';

const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)

onMounted(() => {
    dictData()
    handleSearch()
})

const query = ref({
    transactionDate: '',
    storeNm: '',
    itemNm: '',
    itemType: '',
})
const tableData = ref([])
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}

const getTableData = () => {
    var url = 'sto/TransRecord/query'
    var param = {
        page: currentPage.value,
        rows: pageSize.value,
        params: JSON.stringify(query.value)
    }
    proxy.$axios.post(url, param).then(response => {
        if (response.data.code == 0) {
            tableData.value = response.data.data.records
            totalRows.value = response.data.data.total
        } else {
            ElMessage({
                message: '操作失败!' + response.data.msg,
                type: 'error'
            })
        }
    })
}

const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([5, 10, 20, 30, 40])
const pageSize = ref(5)
const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    getTableData()
}
const dialogVisible = ref(false)
const form = ref({})
const ruleFormRef = ref()
const rules = reactive({})


const handleSave = (ruleFormRef) => {
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'sto/Onhand/save'
            var param = {
                json: JSON.stringify(form.value),
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    form.value = response.data.data
                    ElMessage({
                        message: '保存成功',
                        type: 'success',
                    })
                    getTableData()
                    dialogVisible.value = false
                } else {
                    // ElMessage.error('操作失败！' + response.data.msg)
                    ElMessage({
                        message: '操作失败' + response.data.msg,
                        type: 'error'
                    })
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}
const handleCancelSave = () => {
    dialogVisible.value = false
}
const handleEdit = (row) => {
    dialogVisible.value = true
    form.value = row
}


// 查询数据字典
const dictList = ref({})
const dictData = () => {
    var url = 'framework/Common/querySelectorList'
    var param = {
        type: JSON.stringify(['MEASURING_UNIT', 'item_sort3', 'SUBMIT_FLAG'])
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            dictList.value = response.data.data
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })

}

</script>

<style scoped>

</style>
