<template>
    <div>
        <el-card :body-style="cardBodyStyle">
            <el-card :body-style="cardBodyStyle">
                <SearchBox ref="SearchBoxRef" formName="车号" :formData="query" @PopupNo="PopupNo" @reset="reset">
                </SearchBox>
            </el-card>
        </el-card>
        <ButtonBar @search="handleSearch" @add="handleAdd" @remove="handleRemoveBatch" @edit="handleEditBatch">
        </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="true" prop="carno" label="车号" width="120" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="carTypeid" label="车型id" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carType" label="车型" width="120" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carFdjh" label="发动机号" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carPfbz" label="排放标准" width="100" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carBearing" label="轴承" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carStatus" label="车辆状态" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>


                <el-table-column v-if="true" 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="1200px" top="5vh"
            @open="openDialog">
            <el-form :model="form" label-width="80px" label-position="right" inline ref="ruleFormRef" :rules="rules">
                <el-form-item label="车号:" prop="carno">
                    <el-input v-model="form.carno" />
                </el-form-item>
                <!-- <el-form-item label="车型id:" prop="carno">
                    <el-input v-model="form.carTypeid" />
                </el-form-item> -->
                <el-form-item label="车型:" prop="carno">
                    <!-- <el-input v-model="form.carType" /> -->
                    <el-select v-model="form.carType" placeholder="请选择" size="large" style="width: 240px">
                        <el-option v-for="item in cxall" :key="item.id" :label="item.name" :value="item.name" />
                    </el-select>
                </el-form-item>
                <el-form-item label="发动机号:" prop="carno">
                    <el-input v-model="form.carFdjh" />
                </el-form-item>
                <el-form-item label="排放标准:" prop="carno">
                    <el-select v-model="form.carPfbz" placeholder="请选择" size="large" style="width: 240px">
                        <el-option v-for="item in pfbzall" :key="item.id" :label="item.name" :value="item.name" />
                    </el-select>
                </el-form-item>
                <el-form-item label="轴承:" prop="carno">
                    <!-- <el-input v-model="form.carBearing" /> -->
                    <el-select v-model="form.carBearing" placeholder="请选择" size="large" style="width: 240px">
                        <el-option v-for="item in zcall" :key="item.id" :label="item.name" :value="item.name" />
                    </el-select>
                </el-form-item>
                <el-form-item label="车辆状态:" prop="carno">
                    <!-- <el-input v-model="form.carBearing" /> -->
                    <el-select v-model="form.carStatus" placeholder="请选择" size="large" style="width: 240px">
                        <el-option v-for="item in clzt" :key="item.id" :label="item.name" :value="item.name" />
                    </el-select>
                </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)

const zcall = ref([
    {id:'1',name:'单轴'},
    {id:'2',name:'双轴'},
    {id:'3',name:'三轴'},
    {id:'4',name:'四轴'}
])
const clzt = ref([
    {id:'1',name:'维保'},
    {id:'2',name:'空闲'},
])
const cxall = ref([
    {id:'1',name:'敞车(C系列)'},
    {id:'2',name:'棚车(P系列)'},
    {id:'3',name:'罐车(G系列)'},
    {id:'4',name:'漏斗车(L系列)'},
    {id:'5',name:'自卸车'}
]
)
const pfbzall = ref([
    {id:'1',name:'新能源'},
    {id:'2',name:'国1'},
    {id:'3',name:'国2'},
    {id:'4',name:'国3'},
    {id:'5',name:'国4'},
    {id:'6',name:'国5'},
    {id:'7',name:'国6'}
])
/**
 * 头信息
 */

const deptCode = ref('')
const deptName = ref('')
const deptId = ref('')

/**
 * 头信息
 */

onMounted(() => {
    const temp = JSON.parse(localStorage.getItem('deptTree'))
    deptCode.value = temp[0].deptCode
    deptName.value = temp[0].deptName
    deptId.value = temp[0].id
    handleSearch()
})



/**
 *   查询
 */




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

const getTableData = () => {
    var url = 'tms/Incarinfo/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({
    carno: '',
    carTypeid: '',
    carType: '',
    carFdjh: '',
    carPfbz: '',
    carBearing: '',
    deptCode: '',
    deptName: '',
    deptId: '',
})
const ruleFormRef = ref()
const rules = reactive({
})

const handleAdd = () => {
    form.value = {
    }
    dialogVisible.value = true
}
/**
 * 新增
 */


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

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
    form.value.deptId = deptId.value
    form.value.deptCode = deptCode.value
    form.value.deptName = deptName.value
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'tms/Incarinfo/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 = 'tms/Incarinfo/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 = 'tms/Incarinfo/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>