<template>
    <div>
        <div class="communal-list">
            <div>
                <div class="communal-title">
                    <div>请示数据库</div>
                </div>
                <div style="display: flex;">
                    <div style="width: 60%;display: flex;flex-wrap: wrap;">
                        <div style="width:200px;margin-right: 10px;">
                            <el-input clearable placeholder="国家" v-model="Country">
                            </el-input>
                        </div>
                        <div style="width: 200px;margin-right: 10px;">
                            <el-input clearable placeholder="地区" v-model="Area">
                            </el-input>
                        </div>
                        <div style="width: 200px;margin-right: 10px;">
                            <el-input clearable placeholder="领域" v-model="Field">
                            </el-input>
                        </div>
                        <div style="width: 200px;margin-right: 10px;">
                            <el-button type="primary"  @click="QueryAskData">查询</el-button>
                        </div>
                    </div>
                    <div style="width: 40%;text-align: right;">
                        <el-select style="width:300px" v-model="DiId" placeholder="团组选择" clearable filterable>
                            <el-option v-for="item in delegationInfoList" :key="item.id" :label="item.teamName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                        <el-button type="primary" @click="AskDataProvinceFAOPFile" style="margin-left: 10px;">省外办请示</el-button>
                        <el-button type="primary" @click="AskDataCityFAOPFile" style="margin-left: 10px;">市外办请示</el-button>
                        <router-link :to="{ path: '/home/OpAskData' }">
                            <el-button type="primary" style="margin-left: 10px;">新增</el-button>
                        </router-link>
                    </div>
                </div>
            </div>
            <hr style='background-color:#5555; height:1px; border:none;margin: 10px 0;' />
            <template>
                <el-table :data="tableDatas.slice((currentPage - 1) * pageSize, currentPage * pageSize)" border
                    style="width: 100%" v-loading="loading" element-loading-text="拼命加载中..."
                    @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <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="deleName" label="所属团组">
                    </el-table-column>
                    <el-table-column prop="country" label="国家">
                    </el-table-column>
                    <el-table-column prop="area" label="地区">
                    </el-table-column>
                    <el-table-column prop="unitName" label="公务方名称">
                    </el-table-column>
                    <el-table-column prop="field" label="涉及领域">
                    </el-table-column>
                    <el-table-column prop="talkCase" label="请示范例">
                        <template slot-scope="r">
                            <div :title="r.row.talkCase" class="title">{{ r.row.talkCase }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="280">
                        <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>
    </div>
</template>
<script>
export default {
    data() {
        return {
            loading: false,
            tableDatas: [],
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 12,// 每页的数据条数
            input: '',
            token: '',
            userId: 0,
            DiId: '',
            Country: "",
            Area: "",
            Field: "",
            delegationInfoList: [],
            dataIdItem:[]
        }
    },
    methods: {
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val;
        },
        //页面初始化
        QueryAskData() {
            var url = "/api/Resource/QueryAskData"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    Country: that.Country,
                    Area: that.Area,
                    Field: that.Field,
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    that.delegationInfoList = res.data.data.delegationInfo;
                    that.DiId = that.delegationInfoList[0].id;
                    that.tableDatas = res.data.data.askDatd;
                    that.tableData = that.tableDatas
                    if (that.tableDatas.length != 0) {
                        if (that.tableDatas.slice((that.currentPage - 1) * that.pageSize, that.currentPage * that.pageSize).length == 0) {
                            if (that.currentPage > 1) {
                                that.currentPage = that.currentPage - 1;
                            }
                        }
                    }
                }
            }).catch(function (error) {
                that.$message.error("网络错误，请稍后重试");
            });
        },
        upDate(index, row) {
            this.$router.push({
                path: "/home/OpAskData",
                query: {
                    id: row.id
                }
            })

        },
        del(index, row) {
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var url = "/api/Resource/DelAskData"
                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.QueryOfficialActivitiesByDiId();
                    } else {
                        that.$message.error('删除失败！');
                    }
                    that.loading = false
                }).catch(function (error) {
                    that.loading = false
                    that.$message.error("网络错误，请稍后重试");
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '操作已取消！'
                });
            });
        },
        handleSelectionChange(val) {
            this.dataIdItem=[];
            for(let i=0;i<val.length;i++){
                this.dataIdItem.push(val[i].id)
            }
        },
        //省外办请示
        AskDataProvinceFAOPFile() {
            var url = "/api/Resource/AskDataProvinceFAOPFile"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    dataIdItem:that.dataIdItem,
                    diId:that.DiId,
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    window.open(res.data.data.url);
                }else{
                    that.$message.error(res.data.msg);
                }
            }).catch(function (error) {
                that.$message.error("错误！请联系休息部");
            });
        },
        //市外办请示
        AskDataCityFAOPFile() {
            var url = "/api/Resource/AskDataCityFAOPFile"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    dataIdItem:that.dataIdItem,
                    diId:that.DiId,
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    window.open(res.data.data.url);
                }else{
                    that.$message.error(res.data.msg);
                }
            }).catch(function (error) {
                that.$message.error("错误！请联系休息部");
            });
        },
    },

    mounted() {
        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        this.QueryAskData();
        //this.AirTicketResList()
    }
}
</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;
}

.title {
    width: 90%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
}
</style>