<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" @click="addData" style="margin-left: 10px;">新增</el-button>
                </div>
            </div>
            <template>
                <el-table :data="tableDatas" 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="name" label="名称">
                    </el-table-column>
                    <el-table-column prop="bank" label="开户行">
                    </el-table-column>
                    <el-table-column prop="account" 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="total">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="title" :visible.sync="OpVisible" width="30%" :before-close="handleClose"
            :close-on-click-modal="false">
            <div>
                <el-form :model="OpData" :rules="rules" ref="OpData" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="名称" prop="name">
                        <el-input placeholder="请输入名称" v-model="OpData.name">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="开户行" prop="bank">
                        <el-input placeholder="请输入开户行" v-model="OpData.bank">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="账户" prop="account">
                        <el-input placeholder="请输入账户" v-model="OpData.account">
                        </el-input>
                    </el-form-item>

                    <el-form-item label="备 注" prop="remark">
                        <el-input type="textarea" :rows="3" placeholder="请输入备注" v-model="OpData.remark"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="OpVisible = false">取 消</el-button>
                <el-button type="primary" @click="OpDateBtn">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            loading: false,
            tableDatas: [],
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 12,// 每页的数据条数
            total: 0,
            input: '',
            token: '',
            userId: 0,
            OpVisible: false,
            title: '添加代理出票合作方',
            OpData: {
                status: 0,
                id: 0,
                name: "",
                account: "",
                bank: "",
                createUserId: 0,
                remark: ""
            },

            rules: {
                name: [
                    { required: true, message: '请输入合作方名称', trigger: 'blur' },
                ],
                account: [
                    { required: true, message: '请输入合作方开户账户', trigger: 'blur' },
                ],
                bank: [
                    { required: true, message: '请输入合作方开户银行', trigger: 'blur' },
                ],
            },
        }
    },
    methods: {
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
            this.QueryAirTicketAgent();
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val;
            this.QueryAirTicketAgent();
        },
        QueryAirTicketAgent() {
            this.loading = true
            var url = "/api/Resource/QueryAirTicketAgent"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    PageIndex: that.currentPage,
                    PageSize: that.pageSize
                }
            }).then(function (res) {
                console.log(res)

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

                    that.tableData = res.data.data.pageSource;
                    that.pageSize = res.data.data.pageSize;
                    that.currentPage = res.data.data.pageIndex;
                    that.total = res.data.data.pageCount;
                    that.tableDatas = that.tableData;

                    if (that.tableDatas.length == 0) {
                        if (that.currentPage > 1) {
                            that.currentPage = that.currentPage - 1;
                            that.QueryAirTicketAgent();
                        }
                    }
                }
                that.loading = false
            }).catch(function (error) {
                that.loading = false
                that.$message.error("网络错误，请稍后重试");
            });
        },
        //搜索框处理
        Inquireclick() {
            var newarr = [];
            if (this.input == "") {
                newarr = this.tableData;
                this.total = this.tableData.length;
            } 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.total = newarr.length;
            }
            this.tableDatas = newarr;
            this.currentPage = 1;
        },

        upDate(index, row) {
            this.OpVisible = true;
            this.title = '修改代理出票合作方资料'
            this.OpData.status = 2
            this.OpData.id = row.id
            this.OpData.name = row.name
            this.OpData.account = row.account
            this.OpData.bank = row.bank
            this.OpData.createUserId = this.userId
            this.OpData.remark = row.remark
        },
        addData() {
            this.title = '添加代理出票合作方资料'
            this.OpVisible = true;
            this.OpData.status = 1
            this.OpData.id = 0
            this.OpData.createUserId = this.userId
        },
        OpDateBtn() {
            this.$refs.OpData.validate((valid) => {
                if (valid) {
                    var that = this;
                    var url = "/api/Resource/OpAirTicketAgent"
                    that.$axios({
                        method: 'post',
                        url: url,
                        headers: {
                            Authorization: 'Bearer ' + that.token
                        },
                        data: that.OpData
                    }).then(function (res) {
                        console.log(res)

                        if (res.data.code == 200) {
                            that.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            that.OpVisible = false;
                            that.QueryAirTicketAgent();
                        } else {
                            that.$message.error(res.data.msg,);
                        }
                    })
                }
            })
        },

        del(index, row) {
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var url = "/api/Resource/DelAirTicketAgent"
                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.QueryAirTicketAgent();
                    } 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.QueryAirTicketAgent();
    }
}
</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>