<template>
    <div>
        <el-card :body-style="cardBodyStyle">
        <el-tabs type="border-card">
            <el-tab-pane label="配比结果">
                    <el-row :gutter="20">
                        <el-col span="1"></el-col>
                        <el-form :inline="true">
                            <el-form-item label="成分目标编号" style="margin-bottom: 5px;" prop="propCd">
                                <el-input v-model="query.propCd" placeholder="成分目标编号" clearable></el-input>
                            </el-form-item>
                        </el-form>
                        <el-button type="primary" plain @click="handleSearch">查询</el-button>
                        <el-button type="info" plain @click="autoRatioCompute">配矿计算</el-button>
                    </el-row>
                    <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange"
                        @row-dblclick="handlePropNoDblClick">
                        <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="false" prop="" label="ID" width="120" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="false" prop="createEmpId" label="创建人id" width="300" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="false" prop="createDtm" label="创建时间" width="240" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="false" prop="modifyEmpId" label="修改人id" width="300" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="false" prop="modifyDtm" label="修改时间" width="240" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="false" prop="cancleEmpId" label="删除人ID" width="300" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="false" prop="cancleDtm" label="删除时间" width="240" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="false" prop="cancleYn" label="删除标记" width="240" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="propCd" label="成分目标编号" width="130" header-align="center"
                            align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp1" label="外矿1配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp2" label="外矿2配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp3" label="外矿3配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp4" label="外矿4配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp5" label="外矿5配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp6" label="外矿6配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp7" label="外矿7配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp8" label="外矿8配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp9" label="外矿9配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp10" label="外矿10配比" width="107" header-align="center"
                            align="right" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="propNo" label="配比编号" width="140" header-align="center"
                            align="center" show-overflow-tooltip>
                            <template #default="scope">
                                <span @dblclick="handlePropNoDblClick(scope.row)">{{ scope.row.propNo }}</span>
                            </template>
                        </el-table-column>
                    </el-table>
                    <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" style="margin-top: 10px;">
                    </el-pagination>
                
            </el-tab-pane>
        </el-tabs>
    </el-card>
        <el-tabs type="border-card">
            <el-tab-pane label="配比成分结果">
            <el-table :data="chemTableData" 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 + (chemCurrentPage - 1) * chemPageSize + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="false" prop="" label="ID" width="120" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="createEmpId" label="创建人id" width="300" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="createDtm" label="创建时间" width="240" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="modifyEmpId" label="修改人id" width="300" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="modifyDtm" label="修改时间" width="240" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="cancleEmpId" label="删除人ID" width="300" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="cancleDtm" label="删除时间" width="240" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="cancleYn" label="删除标记" width="240" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="propNo" label="配比编号" width="140" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="matlNm" label="原矿名称" width="140" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="tfeVal" label="TFe" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="sVal" label="S" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="cuVal" label="Cu" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="pVal" label="P" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="tio2Val" label="TiO2" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="sio2Val" label="SiO2" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="mgoVal" label="MgO" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="caoVal" label="CaO" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="al2o3Val" label="Al2O3" width="109" header-align="center"
                    align="right" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="mnVal" label="Mn" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="znVal" label="Zn" width="109" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
            </el-table>
            <!-- <el-pagination :current-page="chemCurrentPage" :page-size="chemPageSize" :page-sizes="chemPageSizes"
                :total="chemTotalRows" background="background" layout="total, sizes, prev, pager, next, jumper"
                @size-change="chemHandleSizeChange" @current-change="chemHandleCurrentChange" style="margin-top: 10px;">
            </el-pagination> -->
    </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script setup>
import ButtonBar from '@/components/ButtonBar.vue'
import { ElLoading } from 'element-plus'

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 handlePropNoDblClick = (row) => {
    chemQuery.value = row;
    chemHandleSearch();
};

//配比成分结果
const chemQuery = ref({
    propNo: ''
})
const chemTableData = ref([])
const chemHandleSearch = () => {
    chemCurrentPage.value = 1
    getChemTableData()
}
const chemCurrentPage = ref(1)
const chemTotalRows = ref(0)
const chemPageSizes = ref([5, 10, 20, 30, 40])
const chemPageSize = ref(5)
const chemHandleSizeChange = (val) => {
    chemPageSize.value = val
    getChemTableData()
}
const chemHandleCurrentChange = (val) => {
    chemCurrentPage.value = val
    getChemTableData()
}
const getChemTableData = () => {
    var url = 'pla/PropMatlRslt/queryResults'
    var param = {
        json: JSON.stringify(chemQuery.value)
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            chemTableData.value = response.data.data
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}


/**
 *   查询
 */
const query = ref({
    propCd: ''
})
const tableData = ref([])
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}

const getTableData = () => {
    var url = 'pla/PropMatlRslt/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 autoRatioCompute = () => {
    const loading = ElLoading.service({
        lock: true,
        text: '配矿计算中请稍等',
        background: 'rgba(0, 0, 0, 0.7)',
    })
    var url = 'pla/PropMatlRslt/autoRatioCompute'
    var param = {
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            ElMessage.success('配矿成功！')
            loading.close()
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
            loading.close()
        }
    })
}

/**
 * 分页
 */
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({
    matlProp1: '',
    matlProp2: '',
    matlProp3: '',
    matlProp4: '',
    matlProp5: '',
    matlProp6: '',
    matlProp7: '',
    matlProp8: '',
    matlProp9: '',
    matlProp10: '',
    propCd: '',
    propNo: ''
})
const ruleFormRef = ref()
const rules = reactive({
})

const handleAdd = () => {
    form.value = {
        matlProp1: '',
        matlProp2: '',
        matlProp3: '',
        matlProp4: '',
        matlProp5: '',
        matlProp6: '',
        matlProp7: '',
        matlProp8: '',
        matlProp9: '',
        matlProp10: '',
        propCd: '',
        propNo: ''
    }
    dialogVisible.value = true
}
/**
 * 新增
 */


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

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'pla/PropMatlRslt/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)
            ElMessage.error('请输入有效数据')
        }
    })
}
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 = 'pla/PropMatlRslt/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 = 'pla/PropMatlRslt/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])
}


</script>

<style></style>