<template>
    <div>
        <el-card :body-style="cardBodyStyle">
            <el-form :inline="true">
                <el-form-item label="生产日期" style="margin-bottom: 0px;">
                    <el-date-picker v-model="query.produceDt" type="date" style="width: 200px;"
                        value-format="YYYY-MM-DD" format="YYYY-MM-DD" placeholder="请选择生产时间"></el-date-picker>
                </el-form-item>
                <el-form-item label="生产班次" style="margin-bottom: 5px;" prop="prodShift">
                    <el-select v-model="query.prodShift" placeholder="请输入生产班次" clearable style="width: 200px;">
                        <el-option label="1" value="1" />
                        <el-option label="2" value="2" />
                        <el-option label="3" value="3" />
                    </el-select>
                </el-form-item>
            </el-form>
        </el-card>
        <ButtonBar @search="handleSearch" @add="handleAdd" @remove="handleRemoveBatch" @edit="handleEditBatch"
            @upload="handleUpload" @download="handleDownload" @import="handleImport" @export="handleExport"></ButtonBar>
        <el-card :body-style="cardBodyStyle">
            <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange" style="height: 500px">
                <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="produceDt" label="生产日期" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="prodShift" label="生产班次" width="90" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemType" label="物料类别" width="90" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemNm" label="物料名称" width="90" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemCode" label="物料编码" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="status" label="状态" width="80" header-align="center" align="center"
                    show-overflow-tooltip>
                    <template #default="scope">
                        <el-tag v-if="scope.row.status == '0'" type="success">未提交</el-tag>
                        <el-tag v-if="scope.row.status == '1'" type="danger">已提交</el-tag>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="grade" label="品位" width="80" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="gradeWc" label="水分" width="80" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>


                <el-table-column v-if="true" prop="wgtWet" label="入厂量湿量" width="100" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="wgtDry" label="入厂量干量" width="100" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <!-- <el-table-column v-if="true" prop="wgt" label="金属重量" width="100" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column> -->

                <el-table-column v-if="true" prop="wgtWetRc" label="入仓量湿量" width="130" header-align="center"
                    align="right" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="wgtDryRc" label="入仓量干量" width="100" header-align="center"
                    align="right" show-overflow-tooltip>
                </el-table-column>

                <el-table-column v-if="true" prop="wgtWetWf" label="外翻量湿量" width="130" header-align="center"
                    align="right" show-overflow-tooltip>
                </el-table-column>


                <el-table-column v-if="true" prop="wgtDryWf" label="外翻量干量" width="100" header-align="center"
                    align="right" show-overflow-tooltip>
                </el-table-column>



                <el-table-column label="操作" header-align="center" align="center" width="100px" fixed="right">
                    <template #default="scope">
                        <el-button plain type="warning" @click="HandleSubmit(scope.row)">提交</el-button>
                        <!-- <el-button plain type="danger" @click="handleDelete(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-item label="生产日期" prop="produceDt">
                    <el-date-picker v-model="form.produceDt" type="date" style="width: 200px;" value-format="YYYY-MM-DD"
                        format="YYYY-MM-DD"></el-date-picker>
                </el-form-item>
                <el-form-item label="生产班次" prop="prodShift">
                    <el-input v-model="form.prodShift" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="物料名称">
                    <el-input v-model="form.itemNm" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="入厂量湿量">
                    <el-input v-model="form.wgtWet" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="外翻量湿量">
                    <el-input v-model="form.wgtWetWf" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="入仓量湿量">
                    <el-input v-model="form.wgtWetRc" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="入厂量干量">
                    <el-input v-model="form.wgtDry" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="外翻量干量">
                    <el-input v-model="form.wgtDryWf" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="入仓量干量">
                    <el-input v-model="form.wgtDryRc" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="品位">
                    <el-input v-model="form.grade" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="水分">
                    <el-input v-model="form.gradeWc" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="物料编码">
                    <el-input v-model="form.itemCode" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="物料类型（金召金鼎正元外矿外购粉矿）">
                    <el-input v-model="form.itemType" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="0未提交1已提交">
                    <el-input v-model="form.status" clearable style="width: 200px"></el-input>
                </el-form-item>
            </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'
const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)

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




/**
 *   查询
 */




const query = ref({
    // produceDt: proxy.$dayjs().format('YYYY-MM-DD HH:mm:ss'),
    produceDt: '',
    prodShift: ''
})
const tableData = ref([])
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}

const getTableData = () => {
    var url = 'prd/Weight/query'
    var param = {
        page: currentPage.value,
        rows: pageSize.value,
        params: JSON.stringify(query.value)
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            tableData.value = response.data.data.records
            totalRows.value = response.data.data.total
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
/**
 *   查询
 */

/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([5, 10, 20, 30, 40])
const pageSize = ref(10)
const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    getTableData()
}
/**
 * 分页
 */


/**
 * 新增
 */
const dialogVisible = ref(false)
const form = ref({
    produceDt: '',
    prodShift: '',
    itemNm: '',
    wgtWet: '',
    wgtWetWf: '',
    wgtWetRc: '',
    wgtDry: '',
    wgtDryWf: '',
    wgtDryRc: '',
    grade: '',
    gradeWc: '',
    itemCode: '',
    itemType: '',
    status: ''
})
const ruleFormRef = ref()
const rules = reactive({
    produceDt: [{
        required: true,
        message: '请输入生产日期',
        trigger: 'change' | 'blur'
    }],
    prodShift: [{
        required: true,
        message: '请输入生产班次 1夜班，2白班，3中班',
        trigger: 'change' | 'blur'
    }],
})

const handleAdd = () => {
    form.value = {
        produceDt: '',
        prodShift: '',
        itemNm: '',
        wgtWet: '',
        wgtWetWf: '',
        wgtWetRc: '',
        wgtDry: '',
        wgtDryWf: '',
        wgtDryRc: '',
        grade: '',
        gradeWc: '',
        itemCode: '',
        itemType: '',
        status: ''
    }
    dialogVisible.value = true
}
/**
 * 新增
 */


/**
 * 保存、修改、删除
 */

const openDialog = () => {
}

const userMessage = ref({
    userCode: localStorage.getItem('userCode'),
    userId: localStorage.getItem('userId'),
    userName: localStorage.getItem('userName'),
    deptId: JSON.parse(localStorage.getItem('dept'))[0].id,
    deptName: JSON.parse(localStorage.getItem('dept'))[0].deptName,
    deptCode: JSON.parse(localStorage.getItem('dept'))[0].deptCode,
})

const HandleSubmit = (row) => {
        var url = 'prd/Weight/submit'
        var param = {
            json: JSON.stringify(row),
            userMessage: JSON.stringify(userMessage.value)
        }
        proxy.$axios.post(url, param).then(response => {
            if (response.data.code == 0) {
                ElMessage({
                    message: '提交成功',
                    type: 'success',
                })
                getTableData()
            } else {
                ElMessage({
                    message: '操作失败' + response.data.msg,
                    type: 'error'
                })
            }
        })
}
const handleSave = (ruleFormRef) => {
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'prd/Weight/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 handleDelete = (row) => {
    ElMessageBox.confirm(
        '确定删除本条记录?',
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
    }
    )
        .then(() => {
            var url = 'prd/Weight/remove'
            var param = {
                id: row.id
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getTableData()
                } else {
                    ElMessage({
                        type: 'error',
                        message: '删除失败' + response.data.msg
                    });
                }
            });
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

/**
 * 保存、修改、删除
 */

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

const handleRemoveBatch = () => {
    console.log('handleRemoveBatch')

    if (multipleSelection.value.length < 1) {
        ElMessage({
            type: 'warning',
            message: '请选择要删除的数据!'
        })
        return
    }

    ElMessageBox.confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        var ids = []
        multipleSelection.value.forEach((item, index) => {
            ids.push(item.id)
        })
        var url = 'prd/Weight/removeBatch'
        var param = {
            ids: JSON.stringify(ids)
        }
        proxy.$axios.post(url, param).then(response => {
            if (response.data.code == '0') {
                ElMessage({
                    type: 'success',
                    message: '操作成功!',
                });
                getTableData()
                dialogVisible.value = false
            } else {
                ElMessage({
                    type: 'error',
                    message: '操作失败!' + response.data.msg
                });
            }
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

const handleEditBatch = () => {
    console.log('handleEditBatch')
    if (multipleSelection.value.length != 1) {
        ElMessage({
            type: 'warning',
            message: '请选择一条数据编辑!'
        })
        return
    }
    handleEdit(multipleSelection.value[0])
}

const handleUpload = () => {
    console.log('handleUpload')
}

const handleDownload = () => {
    console.log('handleDownload')
}

const handleImport = () => {
    console.log('handleImport')
}

const handleExport = () => {
    console.log('handleExport')
}

</script>

<style></style>