<template>
    <div>
        <div class="communal-list">
            <div class="communal-title">
                <div>数据类型板块数据</div>
                <div class="communal-box">
                    <el-input @input="Inquireclick()" placeholder="请输入数据类型名称" v-model="input" clearable
                        style="width: 350px;">
                    </el-input>
                    <el-button type="primary" style="margin-left: 10px;" @click="AddSetDataType">新增</el-button>
                </div>
            </div>
            <template>
                <el-table :data="tableDatas.slice((currentPage - 1) * pageSize, currentPage * pageSize)" border
                    style="width: 100%" v-loading="loading" element-loading-text="拼命加载中...">
                    <el-table-column prop="num" label="序 号" width="55">
                        <template slot-scope="scope">
                            {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="stName" label="类型名称">
                    </el-table-column>
                    <el-table-column prop="name" label="板块名称">
                    </el-table-column>
                    <el-table-column prop="cnName" label="添加人">
                    </el-table-column>
                    <el-table-column prop="createTime" label="添加时间">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="upDate(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="del(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>
            <div class="block">
                <el-pagination align='center' @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="currentPage" :page-sizes="[10, 12, 15, 20]" :page-size="pageSize"
                    layout="total, sizes, prev, pager, next" :total="tableDatas.length">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="typeName" :visible.sync="AddSetDataTypeVisible" width="30%" :before-close="handleClose" :close-on-click-modal="false">
            <div>
                <el-form :model="OperationData" :rules="rules" ref="OperationData" label-width="100px"
                    class="demo-ruleForm">
                    <el-form-item label="数据类型" prop="sTid">
                        <el-select v-model="OperationData.sTid" filterable placeholder="请选择">
                            <el-option v-for="item in setDataType" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="板块名称" prop="name">
                        <el-input placeholder="请输入内容" v-model="OperationData.name">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="备 注" prop="remark">
                        <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="OperationData.remark"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="AddSetDataTypeVisible = false">取 消</el-button>
                <el-button type="primary" @click="AddBtn">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            loading: false,
            tableDatas: [],
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 12,// 每页的数据条数
            input: '',
            token: '',
            userId: 0,
            typeName: "添加数据类型",

            OperationData: {
                status: 1,
                id: 0,
                name: "",
                sTid: '',
                createUserId: 0,
                remark: ""
            },
            AddSetDataTypeVisible: false,
            rules: {
                name: [
                    { required: true, message: '板块名称', trigger: 'blur' },
                ],
                sTid: [
                    { required: true, message: '请选择数据类型', trigger: 'blur' },
                ],
            },
            setDataType: [

            ]
        }
    },
    methods: {
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val;
        },

        //关闭修改框
        handleClose(done) {
            done();
        },
        SetData() {
            this.loading = true
            var url = "/api/System/QuerySetDataInfo"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    pageIndex: 0,
                    pageSize: 0,
                    STid: 0
                }
            }).then(function (res) {
                console.log(res)

                if (res.data.code == 200) {

                    that.tableData = res.data.data;
                    that.tableDatas = that.tableData;
                    if (that.tableDatas.slice((that.currentPage - 1) * that.pageSize, that.currentPage * that.pageSize).length == 0) {
                        if (that.currentPage > 1) {
                            that.currentPage = that.currentPage - 1;
                        }
                    }
                }
                that.loading = false
            }).catch(function (error) {
                that.loading = false
                that.$message.error("网络错误，请稍后重试");
            });
        },
        //下拉框数据
        SetDataType() {
            this.loading = true
            var url = "/api/System/QuerySetDataType"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    pageIndex: 0,
                    pageSize: 0,
                    name: "",
                    cnName: ""
                }
            }).then(function (res) {
                console.log(res)

                if (res.data.code == 200) {

                    res.data.data.forEach(function (item, index) {
                        that.setDataType.push({
                            id: item.id,
                            name: item.name
                        });
                    })
                }

            }).catch(function (error) {

            });
        },
        //搜索框处理
        Inquireclick() {
            var newarr = [];
            if (this.input == "") {
                newarr = this.tableData;
            } else {
                for (var i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].name.indexOf(this.input) != -1) {
                        newarr.push(this.tableData[i]);
                    }
                }
            }
            this.tableDatas = newarr;
            this.currentPage = 1;
        },
        //点击修改
        upDate(index, row) {

            this.AddSetDataTypeVisible = true;
            this.typeName = "修改数据板块"
            this.OperationData.status = 2
            this.OperationData.id = row.id
            this.OperationData.name = row.name
            this.OperationData.sTid = row.sTid
            this.OperationData.createUserId = row.createUserId
            this.OperationData.remark = row.remark

        },
        //点击添加
        AddSetDataType() {
            this.OperationData = {
                status: 1,
                id: 0,
                name: "",
                sTid: '',
                createUserId: 0,
                remark: ""
            },
                this.AddSetDataTypeVisible = true;
            this.typeName = "添加数据板块"
        },
        AddBtn() {
            this.$refs.OperationData.validate((valid) => {
                if (valid) {
                    var that = this

                    that.OperationData.CreateUserId = this.userId

                    var url = "/api/System/OperationSetData"
                    that.$axios({
                        method: 'post',
                        url: url,
                        headers: {
                            Authorization: 'Bearer ' + that.token
                        },
                        data: that.OperationData
                    }).then(function (res) {
                        if (res.data.code == 200) {
                            that.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            that.loading = true;
                            that.AddSetDataTypeVisible = false;
                            that.SetData()
                        } else {
                            that.$message.error(res.data.msg);
                        }
                    })
                }
            })
        },
        //点击删除
        del(index, row) {
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var url = "/api/System/DelSetData"
                var that = this
                this.$axios({
                    method: 'post',
                    url: url,
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    },
                    data: {
                        Id: row.id,
                        DeleteUserId: this.userId
                    }
                }).then(function (res) {
                    console.log(res)

                    if (res.data.code == 200) {

                        that.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        that.SetData();
                    } else {
                        that.$message.error('删除失败！');
                    }
                    that.loading = false
                }).catch(function (error) {
                    that.loading = false
                    that.$message.error("网络错误，请稍后重试");
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '操作已取消！'
                });
            });
        }
    },
    mounted() {

        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        this.SetData();
        this.SetDataType();
    }
}
</script>
<style>
.communal-list {
    background-color: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #0005;
    border-radius: 10px;
}

.communal-title {
    display: flex;
    font-size: 17px;
    font-weight: 600;
    color: #555;
    margin-top: 8px;
    margin-bottom: 10px;
    justify-content: space-between;
    align-items: center;
}

.communal-box {
    display: flex;
}

.communal-box>button {
    margin-left: 10px;
    padding: 8px 20px;
}
</style>