<template>
    <div>
        <el-card :body-style="cardBodyStyle">
            <el-form :inline="true">
                <el-form-item label="物料名称" style="margin-bottom: 5px;" prop="itemNm">
                    <el-input v-model="query.itemNm" placeholder="请输入" clearable></el-input>
                </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="itemCd" label="物料编码" width="130" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemNm" label="物料名称" width="130" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="mois" label="水分值" width="130" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>


                <el-table-column label="操作" header-align="center" align="center" width="200px">
                    <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="编辑" v-model="dialogVisible" width="400px" top="5vh"
            @open="openDialog">
            <el-form :model="form" label-width="80px" label-position="right" inline ref="ruleFormRef" :rules="rules">
                <el-col :span="8">
                    <el-form-item label="物料编码" prop="itemCd">
                        <el-input v-model="form.itemCd" readonly style="width: 200px;"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="物料名称" prop="itemNm">
                        <el-input v-model="form.itemNm" readonly style="width: 200px;">
                            <template #append>
                                <el-button :icon="Search" @click="itemDialog" />
                            </template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-form-item label="水分值" prop="mois">
                    <el-input v-model="form.mois" 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>


        <el-dialog :close-on-click-modal="false"  v-model="itemDialogVisible" width="460px" top="5vh"
            @open="itemOpenDialog" @close="itemCloseDialog">
            <el-table :data="itemTableData" border highlight-current-row  >
                <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="itemCode" label="物料编码" width="130" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="itemNm" label="物料名称" width="130" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column label="操作" header-align="center" align="center" width="100px">
                    <template #default="scope">
                        <el-button plain @click="choose(scope.row)">选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>


        <!-- <Popup_base_wl ref="PopupRef" @receive-from-child="retu"></Popup_base_wl> -->

    </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()
})




/**
 *   查询物料
 */
// import Popup_base_wl from "@/views/bas/Popup_base_wl.vue";
import { Search } from '@element-plus/icons-vue'

// const PopupRef = ref(null)
// const dataIndex = ref(0)
// const itemDialog = (index) => {
//     console.log("index", index)
//     dataIndex.value = index
//     PopupRef.value.open()
// }
// const retu = (val) => {
//     console.log("val", val)
//     form.value.itemCd = val.itemCd
//     form.value.itemNm = val.itemNm
// }
const itemDialogVisible = ref(false)
const itemTableData = ref([])
const itemOpenDialog = () => {
    itemDialogVisible.value = true
    var url = 'prd/ItemCode/query'
    var param = {
        page: 1,
        rows: 99999,
        params: JSON.stringify({})
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            itemTableData.value = response.data.data.records
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}

const itemDialog = () => {
    itemDialogVisible.value = true
}
const itemCloseDialog = () => {
    itemDialogVisible.value = false
}
const choose = (row) => {
    itemDialogVisible.value = false
    form.value.itemCd = row.itemCode
    form.value.itemNm = row.itemNm
}

/**
 *   查询物料
 */




/**
 *   查询
 */




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

const getTableData = () => {
    var url = 'pla/PropWkSet/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(20)
const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    getTableData()
}
/**
 * 分页
 */


/**
 * 新增
 */
const dialogVisible = ref(false)
const form = ref({
    itemCd: '',
    itemNm: '',
    mois: ''
})
const ruleFormRef = ref()
const rules = reactive({
    itemCd: [{
        required: true,
        message: '请输入',
        trigger: 'change' | 'blur'
    }],
    itemNm: [{
        required: true,
        message: '请输入',
        trigger: 'change' | 'blur'
    }],
    mois: [{
        required: true,
        message: '请输入',
        trigger: 'change' | 'blur'
    }, { pattern: /^[0-9]{1,5}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur'| 'change'}]
})

const handleAdd = () => {
    form.value = {
        itemCd: '',
        itemNm: '',
        mois: ''
    }
    dialogVisible.value = true
}
/**
 * 新增
 */


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

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'pla/PropWkSet/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 = 'pla/PropWkSet/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/PropWkSet/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>