<template>
    <div>
        <el-row style="margin-top: 5px;">
            <el-button type="primary" plain @click="handleSearch">查询</el-button>
            <el-button type="success" plain @click="handleAdd">新增</el-button>
            <el-button type="danger" plain @click="handleRemoveBatch">删除</el-button>
        </el-row>
        <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange"
            style="margin-top: 5px;">
            <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="tfeVal" label="TFe" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="sVal" label="S" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="cuVal" label="Cu" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="pVal" label="P" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="tio2Val" label="TiO2" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="sio2Val" label="SiO2" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="mgoVal" label="MgO" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="caoVal" label="CaO" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="al2o3Val" label="Al2O3" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="feoVal" label="FeO" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="mnVal" label="Mn" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="znVal" label="Zn" width="80" header-align="center" align="right"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="finnVal" label="细度值" width="80" header-align="center" align="center"
                show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="moisVal" label="水分值" width="80" header-align="center" align="center"
                show-overflow-tooltip>
            </el-table-column>


            <el-table-column label="操作" header-align="center" align="center" width="170px" fixed="right">
                <template #default="scope">
                    <el-button plain type="warning" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button plain type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </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: 5px;">
        </el-pagination>

        <el-dialog :close-on-click-modal="false" draggable :title="dialogName" v-model="dialogVisible" width="1000px" top="5vh"
            @open="openDialog">
            <el-form :model="form" label-width="80px" label-position="right" inline ref="ruleFormRef" :rules="rules">
                <!-- <el-form-item label="外矿试验数据管理主表ID" prop="pid">
                    <el-input v-model="form.pid" clearable style="width: 200px"></el-input>
                </el-form-item> -->
                <el-form-item label="TFe" prop="tfeVal">
                    <el-input v-model="form.tfeVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="S" prop="sVal">
                    <el-input v-model="form.sVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="Cu" prop="cuVal">
                    <el-input v-model="form.cuVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="P" prop="pVal">
                    <el-input v-model="form.pVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="TiO2" prop="tio2Val">
                    <el-input v-model="form.tio2Val" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="SiO2" prop="sio2Val">
                    <el-input v-model="form.sio2Val" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="MgO" prop="mgoVal">
                    <el-input v-model="form.mgoVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="CaO" prop="caoVal">
                    <el-input v-model="form.caoVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="Al2O3" prop="al2o3Val">
                    <el-input v-model="form.al2o3Val" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="FeO" prop="feoVal">
                    <el-input v-model="form.feoVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="Mn" prop="mnVal">
                    <el-input v-model="form.mnVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="Zn" prop="znVal">
                    <el-input v-model="form.znVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="细度值" prop="finnVal">
                    <el-input v-model="form.finnVal" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="水分值" prop="moisVal">
                    <el-input v-model="form.moisVal" 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 props = defineProps({
    tabsValue: {
        type: Array,
    },
})


/**
 *   查询
 */




const query = ref({
    pid: ''
})
const tableData = ref([])
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}

const getTableData = () => {
    if (props.tabsValue.length != 1) {
        ElMessage({
            type: 'warning',
            message: '请选择一条外矿试验数据!'
        })
        return
    }
    query.value.pid = props.tabsValue[0].id
    var url = 'pla/OreTestNbe/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(5)
const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    getTableData()
}
/**
 * 分页
 */


/**
 * 新增
 */
const dialogVisible = ref(false)
const form = ref({
    pid: '',
    tfeVal: '',
    sVal: '',
    cuVal: '',
    pVal: '',
    tio2Val: '',
    sio2Val: '',
    mgoVal: '',
    caoVal: '',
    al2o3Val: '',
    feoVal: '',
    mnVal: '',
    znVal: '',
    finnVal: '',
    moisVal: ''
})
const ruleFormRef = ref()
const rules = reactive({

    tfeVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    sVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    cuVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    pVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    tio2Val: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    sio2Val: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    mgoVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    caoVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    al2o3Val: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    feoVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    mnVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    znVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    finnVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    moisVal: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
    
})

const dialogName = ref('')
const handleAdd = () => {
    form.value = {
        pid: '',
        tfeVal: '',
        sVal: '',
        cuVal: '',
        pVal: '',
        tio2Val: '',
        sio2Val: '',
        mgoVal: '',
        caoVal: '',
        al2o3Val: '',
        feoVal: '',
        mnVal: '',
        znVal: '',
        finnVal: '',
        moisVal: ''
    }
    if (props.tabsValue.length != 1) {
        ElMessage({
            type: 'warning',
            message: '请选择一条外矿试验数据!'
        })
        return
    }
    form.value.pid = props.tabsValue[0].id
    dialogName.value = '新增'
    dialogVisible.value = true
}
/**
 * 新增
 */


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

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'pla/OreTestNbe/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) => {
    dialogName.value = '编辑'
    dialogVisible.value = true
    form.value = row
}
const handleDelete = (row) => {
    ElMessageBox.confirm(
        '确定删除本条记录?',
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
    }
    )
        .then(() => {
            var url = 'pla/OreTestNbe/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/OreTestNbe/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>