<template>
    <div>
        <!-- <el-card :body-style="cardBodyStyle">
            <el-form :inline="true">
                            </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="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="matlProp1" label="外矿1配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp2" label="外矿2配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp3" label="外矿3配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp4" label="外矿4配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp5" label="外矿5配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp6" label="外矿6配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp7" label="外矿7配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp8" label="外矿8配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp9" label="外矿9配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="matlProp10" label="外矿10配比" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>


                <el-table-column label="操作" header-align="center" align="center" width="200px" 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-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="dialogName" v-model="dialogVisible" width="900px"
                   top="5vh"  @open="openDialog" :before-close="handleCancelSave">
            <el-form :model="form" label-width="80px" label-position="right" inline ref="ruleFormRef" :rules="rules">
                <el-form-item label="外矿1配比"  prop="matlProp1" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp1" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿2配比"  prop="matlProp2" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp2" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿3配比"  prop="matlProp3" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp3" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿4配比"  prop="matlProp4" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp4" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿5配比"  prop="matlProp5" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp5" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿6配比"  prop="matlProp6" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp6" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿7配比"  prop="matlProp7" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp7" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿8配比"  prop="matlProp8" style="white-space: nowrap;"  >
                    <el-input v-model="form.matlProp8" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿9配比"  prop="matlProp9" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp9" clearable style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="外矿10配比"  prop="matlProp10" style="white-space: nowrap;" >
                    <el-input v-model="form.matlProp10" clearable style="width: 150px"></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({
   })
   const tableData = ref([])
   const handleSearch = () => {
       currentPage.value = 1
       getTableData()
   }

   const getTableData = () => {
       var url = 'pla/PropMatlManu/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({
        matlProp1: '', 
        matlProp2: '', 
        matlProp3: '', 
        matlProp4: '', 
        matlProp5: '', 
        matlProp6: '', 
        matlProp7: '', 
        matlProp8: '', 
        matlProp9: '', 
        matlProp10: ''
    })
    const ruleFormRef = ref()
const rules = reactive({
    matlProp1: [{ required: true, message: '请输入外矿1配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp2: [{ required: true, message: '请输入外矿2配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp3: [{ required: true, message: '请输入外矿3配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp4: [{ required: true, message: '请输入外矿4配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp5: [{ required: true, message: '请输入外矿5配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp6: [{ required: true, message: '请输入外矿6配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp7: [{ required: true, message: '请输入外矿7配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp8: [{ required: true, message: '请输入外矿8配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp9: [{ required: true, message: '请输入外矿9配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }],
    matlProp10: [{ required: true, message: '请输入外矿10配比', trigger: 'change' | 'blur' },
    { pattern: /^[0-9]$/, message: '请输入0-9的整数', trigger: 'blur' | 'change' }]
})

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


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

    const openDialog=()=>{
    }

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