<template>
    <div>
        <SearchBox ref="searchBarRef" :formName="search" :formData="query" @showPopupUser="showPop" showResetButton @reset="reset"/>
        <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">
                <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="operDt" label="生产日期" width="120"
                                         header-align="center" align="center"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="operShift" label="班次" width="80"
                                         header-align="center" align="center"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="operGrp" label="班组" width="80"
                                         header-align="center" align="center"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="dutyId" label="值班人" width="120"
                                         header-align="center" align="center"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="operRecd" label="运行记录" width="130"
                                         header-align="center" align="center"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="handOver" label="交班事项" width="130"
                                         header-align="center" align="center"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="tknrOperTm" label="浓缩机运转时间(小时)" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="tailPumpTm1" label="南尾矿泵运转时间(小时)" width="110"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="tailPumpTm2" label="北尾矿泵运转时间(小时)" width="110"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="filtPumpTm1" label="东滤液泵运转时间(小时)" width="110"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="filtPumpTm2" label="西滤液泵运转时间(小时)" width="110"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="cobaPumpTm1" label="东钴泵运转时间(小时)" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="cobaPumpTm2" label="西钴泵运转时间(小时)" width="100"
                                         header-align="center" align="right"
                                         show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column v-if="true" prop="cobaFilt" label="钴过滤运转时间(小时)" 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="1300px"
                   top="5vh"  @open="openDialog" :before-close="handleCancelSave">
            <el-form :model="form" label-width="80px"  label-position="right" inline ref="ruleFormRef" status-icon :rules="rules">
                <el-card>
                    <el-row >
                        <el-col span="6">
                            <el-form-item label="生产日期"  prop="operDt"  >
                                <el-date-picker v-model="form.operDt"  type="date" style="width: 190px;"
                                                 value-format="YYYY-MM-DD" format="YYYY-MM-DD"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col span="6">
                            <el-form-item label="班次" prop="operShift">
                                <el-select v-model="form.operShift" placeholder="请选择班次" style="width: 190px;">
                                    <el-option v-for="option in optionList" :key="option.dicCode" :label="option.dicCode" :value="option.dicCode"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col span="6">
                            <el-form-item label="班组"  prop="operGrp">
                                <el-select v-model="form.operGrp" placeholder="请选择班组" style="width: 190px;">
                                    <el-option v-for="option in optionGrpList" :key="option.dicCode" :label="option.dicCode" :value="option.dicCode"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col span="6">
                            <el-form-item label="值班人" prop="dutyId"  >
                                <el-input v-model="form.dutyId" clearable style="width: 190px"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row >
                        <el-form-item label="运行记录" prop="operRecd">
                            <el-input v-model="form.operRecd" style="width: 495px; font-size: 16px;" clearable :rows="2" show-word-limit maxlength="75" type="textarea"/>
                        </el-form-item>
                        <el-form-item label="交班事项" prop="handOver">
                            <el-input v-model="form.handOver" style="width: 495px; font-size: 16px;" clearable :rows="2" show-word-limit maxlength="75" type="textarea"/>
                        </el-form-item>
                    </el-row>
                </el-card>
                <el-card style="margin-bottom: 10px; margin-top: 10px;">
                    <el-row><el-tag style="font-size: 20px; margin-bottom: 20px; ">设备信息</el-tag></el-row>
                    <el-row :gutter="70">
                        <el-col :span="2"></el-col>
                        <el-col span="6">
                            <el-form-item label="浓缩机运转时间(小时)"  style="white-space: nowrap;" prop="tknrOperTm">
                                <el-input v-model="form.tknrOperTm" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                        
                        <el-col span="6">
                            <el-form-item label="钴过滤运转时间(小时)"  style="white-space: nowrap;" prop="cobaFilt">
                                <el-input v-model="form.cobaFilt" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                        
                        <el-col span="5">
                            <el-form-item label="南尾矿泵运转时间(小时)" style="white-space: nowrap;" prop="tailPumpTm1">
                                <el-input v-model="form.tailPumpTm1" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col span="5">
                            <el-form-item label="北尾矿泵运转时间(小时)" style="white-space: nowrap;" prop="tailPumpTm2">
                                <el-input v-model="form.tailPumpTm2" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row :gutter="70">
                        <el-col :span="2"></el-col>
                        <el-col span="6">
                            <el-form-item label="东滤液泵运转时间(小时)"  style="white-space: nowrap;" prop="filtPumpTm1">
                                <el-input v-model="form.filtPumpTm1" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                        
                        <el-col span="5">
                            <el-form-item label="西滤液泵运转时间(小时)"  style="white-space: nowrap;" prop="filtPumpTm2">
                                <el-input v-model="form.filtPumpTm2" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col span="5">
                            <el-form-item label="东钴泵运转时间(小时)" style="white-space: nowrap;" prop="cobaPumpTm1">
                                <el-input v-model="form.cobaPumpTm1" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col span="5">
                            <el-form-item label="西钴泵运转时间(小时)" style="white-space: nowrap;" prop="cobaPumpTm2">
                                <el-input v-model="form.cobaPumpTm2" clearable style="width: 110px"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-card>
            </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 Selector from '@/components/Selector.vue'
    import ButtonBar from '@/components/ButtonBar.vue'

    import {
        ref,
        reactive,
        onMounted,
        getCurrentInstance
    } from 'vue'

    const {
        proxy
    } = getCurrentInstance()

    import SearchBox from '@/components/SearchBox.vue'
    const searchBarRef = ref(null)

    import tools from '@/tools'
    const cardBodyStyle = ref(tools.style.card)
    const inputStyle = ref(tools.style.input)

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

     // 数据字典
     const optionList = ref([])
     const getDic = () => {
		var url = 'framework/Common/querySelector'
		var param = {
			type: 'PRD_OPER_SHIFT'
		}
		proxy.$axios.get(url, param).then(response => {
			if (response.data.code == '0') {
				optionList.value = response.data.data
			} else {
				ElMessage.error('操作失败！' + response.data.msg)
			}
		})
	}
    const optionGrpList = ref([])
    const getGrp = () => {
		var url = 'framework/Common/querySelector'
		var param = {
			type: 'PRD_OPER_GRP'
		}
		proxy.$axios.get(url, param).then(response => {
			if (response.data.code == '0') {
				optionGrpList.value = response.data.data
			} else {
				ElMessage.error('操作失败！' + response.data.msg)
			}
		})
	}

    /**
     *   查询
     */




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

   const getTableData = () => {
       var url = 'prd/TknrRecd/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({
        operDt: '', 
        operShift: '', 
        operGrp: '', 
        dutyId: '', 
        operRecd: '', 
        handOver: '', 
        tknrOperTm: '', 
        tailPumpTm1: '', 
        tailPumpTm2: '', 
        filtPumpTm1: '', 
        filtPumpTm2: '', 
        cobaPumpTm1: '', 
        cobaPumpTm2: '', 
        cobaFilt: ''
    })
    const ruleFormRef = ref()
    const rules = reactive({
        operDt: [{ required: true, message: '请输入生产日期', trigger: 'change' | 'blur' }], 
        operShift: [{ required: true, message: '请输入班次', trigger: 'change' | 'blur' }], 
        operGrp: [{ required: true, message: '请输入班组', trigger: 'change' | 'blur' }], 
        dutyId: [{ required: true, message: '请输入值班人', trigger: 'change' | 'blur' },
        { max: 20, message: '值班人超过有效长度', trigger: 'change' | 'blur' }],
        operRecd:[{ max: 75, message: '运行记录超过有效长度', trigger: 'change' | 'blur' }],
        handOver:[{ max: 75, message: '交班事项超过有效长度', trigger: 'change' | 'blur' }],
        tknrOperTm: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
        cobaFilt: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
        tailPumpTm1: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
        tailPumpTm2: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
        filtPumpTm1: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
        filtPumpTm2: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
        cobaPumpTm1: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur'| 'change'}],
        cobaPumpTm2: [{ pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur' | 'change'}],
        
    })

    const dialogName = ref('')
    const handleAdd = () => {
        form.value = {
            operDt: '', 
            operShift: '', 
            operGrp: '', 
            dutyId: '', 
            operRecd: '', 
            handOver: '', 
            tknrOperTm: '', 
            tailPumpTm1: '', 
            tailPumpTm2: '', 
            filtPumpTm1: '', 
            filtPumpTm2: '', 
            cobaPumpTm1: '', 
            cobaPumpTm2: '', 
            cobaFilt: ''
        }
        if (ruleFormRef.value) {
            ruleFormRef.value.resetFields()
        }
        dialogName.value = '新增'
        dialogVisible.value = true
    }
    /**
     * 新增
     */


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

    const openDialog=()=>{
    }

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