<template>
    <div>
        <div style="width:600px;float: left; margin: 10px;">
            <!-- <el-input v-model="query" placeholder="请输入查询条件" style="width :300px;float: left;"></el-input> -->
            <el-select v-model="query" clearable>
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button type="primary" @click="handleAdd">新增</el-button>
        </div>
        <el-table v-loading="loading" :data="tableData" stripe border style="width: 99%;">
            <el-table-column prop="id" label="数量" width="120" v-if="isShow">
            </el-table-column>
            <el-table-column prop="dicCode" label="编码" width="120" show-overflow-tooltip align="right" header-align="center">
            </el-table-column>
            <el-table-column prop="dicName" label="名称" width="120" show-overflow-tooltip align="right" header-align="center">
            </el-table-column>
            <el-table-column prop="dicType" label="类型" width="120" show-overflow-tooltip align="right" header-align="center" :formatter="formatDicType">
            </el-table-column>
            <el-table-column prop="orderBy" label="排序" width="120" show-overflow-tooltip align="right" header-align="center">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                       :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalRows">
        </el-pagination>

        <el-dialog title="操作" :visible.sync="dialogVisible" width="500px">
            <el-form :model="sysDic" label-position="right" label-width="80px">
                <el-form-item label="ID" v-if="isShow">
                    <el-input v-model="sysDic.id" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="编码">
                    <el-input v-model="sysDic.dicCode" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="名称">
                    <el-input v-model="sysDic.dicName" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <!-- <el-input v-model="sysDic.dicType" placeholder=""></el-input> -->
                    <el-select v-model="sysDic.dicType" clearable>
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="sysDic.orderBy" placeholder=""></el-input>
                </el-form-item>
    <el-form-item>
        <el-button type="success" @click="handleSave">保存</el-button>
        <el-button @click="handleCancelSave">取消</el-button>
    </el-form-item>
            </el-form>
        </el-dialog>


    </div>
</template>
<script>
    import axios from '@/axios';
    export default {
        data() {
            return {
                tableData: [],
                tableProperty: [],
                currentPage: 1,
                totalRows: 0,
                pageSizes: [10, 50, 100, 200],
                pageSize: 10,
                query:'',
                sysDic: {
                    id: '',
                    dicCode: '',
                    dicName: '',
                    dicType: '',
                    orderBy: '',
                },
                isShow:false,
                loading: true,
                dialogVisible: false,
                options:[{
                    value: 'TALLY_PEOPLE',
                    label: '吊装工'
                    }
                ],
            }
        },
        mounted() {
            this.getTableData()
            this.getTableProperty()
        },
        methods: {
            formatDicType(row, column, cellValue){
                if (row.dicType == "TALLY_PEOPLE"){
                    return "吊装工"
                }
            },
            //查询table数据
            getTableData() {
                var url = 'SysDic/query.do'
                var param = {
                    page: this.currentPage,
                    rows: this.pageSize,
                    dicType: this.query,
                }
                axios.get(url, param).then(response => {
                    if (response.data.code == '0') {
                    this.tableData = response.data.data.list
                    this.totalRows = response.data.data.total
                } else {
                    this.$message({
                        type: 'error',
                        message: '操作失败!' + response.data.msg
                    });
                }
                this.loading = false
            });
            },
            //查询table列
            getTableProperty() {
                //console.log("获取table数据getTableData")
                var url = 'CodeMachine/queryTableProperty.do'
                var param = {
                    bussinessName:'SYS_DIC'
                }
                axios.get(url, param).then(response => {
                    this.tableProperty = response.data
            });
            },

            // 修改每页行数
            handleSizeChange(val) {
                this.pageSize = val
                this.getTableData()
            },
            // 修改当前页事件
            handleCurrentChange(val) {
                this.currentPage = val
                this.getTableData()
            },
            //新增按钮事件
            handleAdd() {
                this.dialogVisible = true
                this.sysDic = {
            id: '',
            dicCode: '',
            dicName: '',
            dicType: '',
            orderBy: '',
                }
            },
            // 编辑按钮事件
            handleEdit(row) {
                this.dialogVisible = true
                this.sysDic = row

            },
            // 删除按钮事件
            handleDelete(id) {
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var url = 'SysDic/remove.do'
                    var param = {
                        id: id
                    }
             axios.post(url, param).then(response => {
                    if (response.data.code == '0') {
                    this.$message({
                        type: 'success',
                        message: '删除成功!',
                    });
                    this.getTableData()
                } else {
                    this.$message({
                        type: 'error',
                        message: '删除失败!' + response.data.msg
                    });

                }
            });
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
            },
            //保存取消按钮事件   关闭编辑对话框
            handleCancelSave(row) {
                this.dialogVisible = false
            },
            //保存按钮事件
            handleSave(row) {
                var url = 'SysDic/save.do'
                var json = JSON.stringify(this.sysDic)
                var param = {
                    json: json
                }
                axios.post(url, param).then(response => {
                    if (response.data.code == '0') {
                        this.$message({
                            type: 'success',
                            message: '操作成功!',
                        });
                        this.getTableData()
                        this.dialogVisible = false
                    } else {
                        this.$message({
                            type: 'error',
                            message: '操作失败!' + response.data.msg
                        });
                    }
                });
            },

            // 查询按钮事件
            search() {
                this.getTableData()
            }
        },
        
    }
</script>

<style>
    .el-select-dropdown .el-scrollbar .el-scrollbar__wrap
    {
        overflow: scroll!important;
    }
	.el-upload-list__item-name{
		background-color: #9f9;
	}
</style>