<template>
    <div class="page-pack">
        <div class="title-pack clear-float pl-15 pr-15">
            <div class="float-left">
                <span class="page-title-text">设备基础数据</span>
            </div>
            <div class="float-right page-refresh">
                <span @click="doRefreshClick" class="pointer">
                    <svg-icon icon-class="refresh"></svg-icon>
                    <span class="ml-10">刷新</span>
                </span>
            </div>
        </div>
        <div class="tool-pack clear-float pl-15 pr-15">
            <Search
                searchCode="SCADA_EQUIP_INFO"
                @search="doSearchClick"
                @reset="doResetClick">
            </Search>
            <div class="float-left">
                <el-button
                    size="small"
                    type="primary"
                    @click="doAddClick">
                    新增
                </el-button>
            </div>
        </div>
        <div class="general-list-main-pack pl-15 pr-15 pb-15">
            <div>
                <Table
                    :tableConfig="tableConfig"
                    @onPageSizeChange="onPageSizeChange"
                    @onCurrentPageChange="onCurrentPageChange">
                    <template slot="equipType">
                        <el-table-column
                            label="设备类型"
                            show-overflow-tooltip>
                            <template slot-scope="scope">
                                <span>{{resolveEquipType(scope.row.equipType)}}</span>
                            </template>
                        </el-table-column>
                    </template>
                    <template slot="operationColumn">
                        <el-table-column
                            fixed="right"
                            width="100"
                            label="操作">
                            <template slot-scope="scope">
                                <el-button
                                    type="text"
                                    @click="doEditClick(scope.row)">
                                    编辑
                                </el-button>
                                <el-button
                                    style="color: rgb(244, 116, 118)"
                                    type="text"
                                    @click="doDelectClick(scope.row)">
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </template>
                </Table>
            </div>
        </div>

        <!-- 新建设备对话框 -->
        <AddDeviceDialog
            ref="AddDeviceDialog"
            @doConfirmClick="doAddDeviceDialogConfirm">
        </AddDeviceDialog>

        <!-- 编辑设备对话框 -->
        <EditDeviceDialog
            ref="EditDeviceDialog"
            :editDeviceBasicValue="editDeviceBasicValue"
            @doConfirmClick="doEditDeviceDialogConfirm">
        </EditDeviceDialog>
    </div>
</template>

<script>
    import Table from '@/components/Table/index.vue'
    import Search from '@/components/Search'
    import AddDeviceDialog from './components/AddDeviceDialog'
    import EditDeviceDialog from './components/EditDeviceDialog'

    export default {
        name: 'DeviceBasic',
        components: {
            Search,
            Table,
            AddDeviceDialog,
            EditDeviceDialog
        },
        data () {
            return {
                // 设备分类数据
                equipTypeData: [],
                // 编辑设备基础数据
                editDeviceBasicValue: {},
                // 精准搜索关键字列表
                searchList: {},
                // 表格配置项
                tableConfig: {
                    height: 0,
                    data: [],
                    fieldList: [
                        { label: '车间', value: 'workshopName' },
                        { label: '设备类型', value: 'equipType', slot: 'equipType' },
                        { label: '设备编号', value: 'equipCode' },
                        { label: '设备名称', value: 'alias' },
                        { label: '设备厂商', value: 'equipFactory' }
                    ],
                    paginationToggle: true,
                    paginationConfig: {
                        total: 0,
                        currentPage: 1,
                        pageSize: 20
                    }
                }
            }
        },
        methods: {
            // 解析设备分类名称
            resolveEquipType (code) {
                for (let item of this.equipTypeData) {
                    if (item.itemCode === code) {
                        return item.itemName;
                    }
                }
            },
            // 点击刷新按钮
            doRefreshClick () {
                this.getTableList().then(response => {
                    this.$message.success('刷新成功！')
                })
            },
            // 点击搜索组件搜索按钮
            doSearchClick (searchValue) {
                this.searchList = searchValue
                this.tableConfig.paginationConfig.currentPage = 1
                // 获取表格数据
                this.getTableList()
            },
            // 点击搜索组件重置按钮
            doResetClick (searchValue) {
                this.searchList = { ...searchValue, filters: [] }
                this.tableConfig.paginationConfig.currentPage = 1
                this.tableConfig.paginationConfig.pageSize = 20
                // 获取表格数据
                this.getTableList()
            },
            // 表格组件分页数量改变事件
            onPageSizeChange (pageSize) {
                this.tableConfig.paginationConfig.currentPage = 1
                this.tableConfig.paginationConfig.pageSize = pageSize
                // 获取表格数据
                this.getTableList()
            },
            // 表格组件分页页数改变事件
            onCurrentPageChange (currentPage) {
                this.tableConfig.paginationConfig.currentPage = currentPage
                // 获取表格数据
                this.getTableList()
            },
            // 获取表格数据
            getTableList () {
                let queryParams = {
                    searchCode: this.searchList.searchCode || 'SCADA_EQUIP_INFO',
                    filters: this.searchList.filters || [],
                    pageSize: this.tableConfig.paginationConfig.pageSize,
                    pageNum: this.tableConfig.paginationConfig.currentPage
                }
                return new Promise((resolve, reject) => {
                    this.$fetch('basic-info-controller/equipPage-post', queryParams).then(response => {
                        if (response.list.length === 0 && response.pageNum !== 1) {
                            this.tableConfig.paginationConfig.currentPage -= 1
                            // 获取表格数据
                            this.getTableList()
                        } else {
                            this.tableConfig.data = _.cloneDeep(response.list)
                            this.tableConfig.paginationConfig.total = _.cloneDeep(response.total)
                            resolve()
                        }
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 点击删除按钮
            doDelectClick (deviceValue) {
                this.$confirm(`确定要删除设备编码为 ${deviceValue.equipCode} 的设备基础数据吗？`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    let queryParams = { equipId: deviceValue.id, hsah: 'equipId' }
                    this.$fetch('basic-info-controller/equipId-delete', queryParams).then(response => {
                        this.$message.success('删除成功！')
                        // 获取表格数据
                        this.getTableList()
                    })
                }).catch(() => {})
            },
            // 点击新增按钮
            doAddClick () {
                this.$refs.AddDeviceDialog.dialogToggle = true
            },
            // 点击新增设备对话框确认按钮
            doAddDeviceDialogConfirm () {
                // 获取表格数据
                this.getTableList()
            },
            // 点击编辑按钮
            doEditClick (deviceValue) {
                this.editDeviceBasicValue = _.cloneDeep(deviceValue)
                this.$refs.EditDeviceDialog.dialogToggle = true
            },
            // 点击编辑设备对话框确认按钮
            doEditDeviceDialogConfirm () {
                // 获取表格数据
                this.getTableList()
            },
            // 获取设备分类数据
            getSeviceSortSelectData () {
                let queryParams = { dicCode: 'DEVICE_SORT', hash: 'dicCode' };
                this.$fetch('dict-item-controller/dicCode-get', queryParams).then(response => {
                    this.equipTypeData = _.cloneDeep(response);
                });
            }
        },
        mounted () {
            setTimeout(() => {
                this.tableConfig.height = document.querySelector('.page-pack').offsetHeight - 210;
                // 获取设备分类数据
                this.getSeviceSortSelectData();
                // 获取表格数据
                this.getTableList();
            }, 0)
        }
    }
</script>

<style lang="scss" scoped>
    .page-pack {
        position: relative;
        min-height: 100%;
        width: 100%;
        border: 1px solid #d2d3d5;
        background-color: #fff;
        overflow-y: auto;

        .title-pack {
            height: 38px;
            line-height: 38px;
            border-bottom: 1px solid #d2d3d5;
        }

        .tool-pack {
            padding-top: 16px;
            padding-bottom: 16px;
            .tool-tag {
                height: 32px;
                line-height: 32px;
                font-size: 14px;
            }
        }

        .edit-button {
            color: #538FFD;
        }

        .status-circle {
            display: inline-block;
            width: 6px;
            height: 6px;
            border-radius: 6px;
            margin-right: 5px;
            vertical-align: middle;
        }
        .delete-button {
            color: #F47476;
        }

        .view-span {
            color: #1284fa;
            border-bottom: 1px solid #1284fa;
            &:hover {
                cursor: pointer;
            }
        }
        .el-dialog__body {
            padding: 30px 30px 10px 20px!important;
            .el-row {

                .el-col {
                    line-height: 35px;

                    .el-form-item {
                        width: 100%;

                        .el-form-item__content {
                            width: 100%;

                            .el-input {
                                font-size: 12px;
                                height: 32px;
                            }
                            .el-input__inner {
                                height: 32px;
                            }
                            .el-select {
                                width: 100%!important;
                                font-size: 12px;
                                height: 32px;
                            }
                            .el-date-editor {
                                width: 100%!important;
                            }
                        }
                    }
                }
            }
        }
    }
</style>
