<template>
    <div v-loading.fullscreen.lock="fullscreenLoading">
        <div class="communal-list">
            <div>
                <div class="communal-title">
                    <div>公务出访</div>
                </div>
                <div style="display: flex;">
                    <div style="width: 18%;">
                        <el-select v-model="DiId" placeholder="团组选择" clearable filterable
                            @change="delegationSelectChange">
                            <el-option v-for="item in delegationInfoList" :key="item.id" :label="item.teamName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </div>
                    <div style="width: 82%;text-align: right;">
                        <el-button @click="deriveclick" type="primary">导出</el-button>
                        <el-button @click="Getqswj(1)" type="primary">省外办请示文件</el-button>
                        <el-button @click="Getqswj(2)" type="primary">市外办请示文件</el-button>
                        &nbsp;
                        <router-link :to="{ path: '/home/OpOfficialActivities', query: { DiId } }">
                            <el-button type="primary">新增</el-button>
                        </router-link>
                    </div>
                </div>
                <div class="communal-box-title">
                    <span style="font-weight: bold;">团队名称：</span>
                    {{ delegationInfo.teamName }}&nbsp;&nbsp;&nbsp;
                    <span style="font-weight: bold;">客户：</span>
                    {{ delegationInfo.clientName }}&nbsp;&nbsp;&nbsp;
                    <span style="font-weight: bold;">出访国家：</span>
                    {{ delegationInfo.visitCountry }}&nbsp;&nbsp;&nbsp;
                    <span style="font-weight: bold;">起止日期：</span>
                    {{ delegationInfo.visitStartDate | capitalize }}~{{ delegationInfo.visitEndDate | capitalize
                    }}&nbsp;&nbsp;&nbsp;
                    <span style="font-weight: bold;">天数/人数：</span>
                    {{ delegationInfo.visitDays }}天/{{ delegationInfo.visitPNumber }}人
                </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="client" label="公务单位">
                    </el-table-column>
                    <el-table-column prop="date" label="公务时间">
                    </el-table-column>
                    <el-table-column prop="address" label="公务地址">
                    </el-table-column>
                    <el-table-column prop="contact" label="联系人">
                    </el-table-column>
                    <el-table-column prop="createUserName" label="记录者">
                    </el-table-column>
                    <!-- <el-table-column prop="nature" label="性质">
                    </el-table-column> -->
                    <el-table-column prop="officialFormName" label="公务形式">
                    </el-table-column>
                    <el-table-column prop="createTime" label="记录时间">
                    </el-table-column>
                    <el-table-column prop="confirmTheInvitation" label="复核确认" width="80">
                        <template slot-scope="scope">
                            <span style="font-size: 20px;"
                                :style="scope.row.confirmTheInvitation == 0 ? 'color: #555;' : 'color: #00cb00;'">
                                {{ scope.row.confirmTheInvitation == 0 ? '-' : '✓' }}
                            </span>

                        </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>
        <el-dialog title="导出公务出访" width="400px" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
            <el-form>
                <el-date-picker v-model="valuedate" type="daterange" align="right" unlink-panels range-separator="至"
                    start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
                </el-date-picker>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="Confirmexport">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            loading: false,
            tableDatas: [],
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 12,// 每页的数据条数
            input: '',
            token: '',
            userId: 0,
            DiId: '',
            diids: '',
            delegationInfoList: [],
            delegationInfo: {},
            dialogFormVisible: false,
            valuedate: '',
            fullscreenLoading: false,
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
        }
    },
    methods: {
        //处理日期
        datetime(val) {
            var date = new Date(val);
            var y = date.getFullYear();
            var m = date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1).toString();
            var d = date.getDate() >= 10 ? date.getDate() : '0' + (date.getDate()).toString();
            return y + '-' + m + '-' + d
        },
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val;
        },
        //团组下拉框
        OfficialActivitiesGroupNameList() {
            this.fullscreenLoading = true;
            var url = "/api/Resource/OfficialActivitiesGroupNameList"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 3,
                    pageIndex: 1,
                    pageSize: 10000,
                    groupName: '',
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    that.delegationInfoList = res.data.data;
                    if (that.diids != that.diids) {
                        that.DiId = that.delegationInfoList[0].id;
                        that.delegationInfo = that.delegationInfoList[0];
                    } else {
                        that.DiId = that.diids;
                    }
                    that.fullscreenLoading = false;
                    that.delegationSelectChange();
                    // that.QueryOfficialActivitiesByDiId();
                } else {
                    that.fullscreenLoading = false;
                }
            }).catch(function (error) {
                that.fullscreenLoading = false;
                that.$message.error("网络错误，请稍后重试");
            });
        },
        //团组下拉框
        GetGroupAllList() {
            this.fullscreenLoading = true;
            var url = "/api/Resource/GetGroupAllList"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    pageIndex: 1,
                    pageSize: 10000
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    that.delegationInfoList = res.data.data.delegation;
                    if (that.diids != that.diids) {
                        that.DiId = that.delegationInfoList[0].id;
                        that.delegationInfo = that.delegationInfoList[0];
                    } else {
                        that.DiId = that.diids;
                    }
                    that.fullscreenLoading = false;
                    that.delegationSelectChange();
                    // that.QueryOfficialActivitiesByDiId();
                } else {
                    that.fullscreenLoading = false;
                }
            }).catch(function (error) {
                that.fullscreenLoading = false;
                that.$message.error("网络错误，请稍后重试");
            });
        },
        //下载请示文件
        Getqswj(val) {
            var url = "/api/Resource/OfficialActivitiesFileDownload"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    fileType: val,
                    diId: that.DiId
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    window.open(res.data.data)
                } else {
                    that.$message.error(res.data.msg);
                }
            }).catch(function (error) {
                that.$message.error(error);
            });
        },
        delegationSelectChange() {
            for (let index = 0; index < this.delegationInfoList.length; index++) {
                if (this.delegationInfoList[index].id == this.DiId) {
                    this.delegationInfo = this.delegationInfoList[index];
                    break;
                }
            }
            this.QueryOfficialActivitiesByDiId();
        },
        //团组信息绑定
        QueryOfficialActivitiesByDiId() {
            this.tableData = [];
            this.tableDatas = [];
            var url = "/api/Resource/QueryOfficialActivitiesByDiId"
            var that = this
            if (that.DiId == "" && that.DiId == null && that.DiId == undefined) { that.DiId = 0 }
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    pageIndex: 0,
                    pageSize: 0,
                    diId: that.DiId
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    that.tableDatas = res.data.data;
                    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/OpOfficialActivities",
                query: {
                    DiId: this.DiId,
                    id: row.id
                }
            })

        },
        deriveclick() {
            this.dialogFormVisible = true;
        },
        Confirmexport() {
            console.log(this.datetime(this.valuedate[0]));

        },
        del(index, row) {
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var url = "/api/Resource/DelOfficialActivities"
                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: '操作已取消！'
                });
            });
        },
    },
    filters: {
        capitalize(val) {
            if (val) {
                return val.split(" ")[0]
            } else {
                return val
            }
        }
    },
    created() {
        this.diids = Number(this.$route.query.DiId);
        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        // this.GetGroupAllList();
        this.OfficialActivitiesGroupNameList();
    },
    mounted() {

        //this.AirTicketResList()
    }
}
</script>
<style>
.communal-list {
    background-color: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #0005;
    border-radius: 10px;
    min-height: 830px;
}

.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;
}

.communal-box-title {
    margin: 10px 0;
}

.communal-box-title span {
    font-size: 16px;
    font-weight: 600;
    color: #555;
}
</style>