<template>
    <div>
        <div class="communal-list">
            <div>

                <div class="communal-title">
                    <div>邀请公务费用</div>
                </div>
                <div style="display: flex;justify-content: space-between;">
                    <div style="width: 70%;display: flex;">
                        <el-select style="width:300px;margin-right:10px" v-model="DiId" placeholder="团组选择" clearable filterable
                            @change="AirTicketResSelectChange">
                            <el-option v-for="item in AirTicketResSelectData" :key="item.id" :label="item.teamName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                        <el-input style="width:400px;margin-right:10px" v-model="inviter" placeholder="请输入邀请方名称" clearable></el-input>
                        <el-date-picker style="width:400px;margin-right:10px;margin-bottom: 0px;" v-model="InviteTime" type="daterange" align="right"
                        unlink-panels format="yyyy-MM-dd" value-format="yyyy-MM-dd" range-separator="至"
                        start-placeholder="邀请开始日期" end-placeholder="邀请结束日期" :picker-options="pickerOptions">
                        </el-date-picker>
                        <el-button type="primary" @click="ConditionsQuery">查询</el-button>
                    </div>
                    <div style="width: 30%;text-align: right;margin-bottom: 0px;">
                        <el-button type="primary" style="margin-left: 10px;" @click="addIf">新增</el-button>
                    </div>
                </div>
                <div style="margin:10px 0;color:#606266;">
                    <span style="font-weight: bold;font-size:16px;">团队名称：</span>
                    <span style="color:#606266;">{{ delegationInfo.teamName }}&nbsp;&nbsp;&nbsp;</span>
                    <span style="font-weight: bold;font-size:16px;">客户：</span>
                    <span style="color:#606266;">{{ delegationInfo.clientName }}&nbsp;&nbsp;&nbsp; {{
}}&nbsp;&nbsp;&nbsp;</span>
                    <span style="font-weight: bold;font-size:16px;">出访国家：</span>
                    <span style="color:#606266;">{{ delegationInfo.visitCountry }}&nbsp;&nbsp;&nbsp;</span>
                    <span style="font-weight: bold;font-size:16px;">起止日期：</span>
                    <span style="color:#606266;">{{ delegationInfo.visitStartDate }}—{{ delegationInfo.visitEndDate
                    }}&nbsp;&nbsp;&nbsp;</span>
                    <span style="font-weight: bold;font-size:16px;">天数/人数：</span>
                    <span style="color:#606266;">{{ delegationInfo.visitDays }}天/{{ delegationInfo.visitPNumber }}人</span>
                </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="inviterArea" label="邀请方地区" width="120">
                    </el-table-column>
                    <el-table-column prop="inviter" label="邀请方" width="200">
                    </el-table-column>
                    <el-table-column prop="inviteTime" label="邀请时间" width="160">
                    </el-table-column>
                    <el-table-column prop="inviteCost,currency" label="邀请费用" width="120" sortable>
                        <template slot-scope="scope">
                            {{ scope.row.inviteCost|numFilter}} {{ scope.row.inviteCurrency }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="inviteCost,currency" label="快递费用" width="120" sortable>
                        <template slot-scope="scope">
                            {{ scope.row.sendCost|numFilter}} {{ scope.row.sendCurrency }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="inviteCost,currency" label="翻译费用" width="120" sortable>
                        <template slot-scope="scope">
                            {{ scope.row.translateCost|numFilter }} {{ scope.row.translateCurrency }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="inviteCost,currency" label="活动费" width="120" sortable>
                        <template slot-scope="scope">
                            {{ scope.row.eventsCost|numFilter }} {{ scope.row.eventsCurrency }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="inviteCost,currency" label="支付费用" width="120" sortable>
                        <template slot-scope="scope">
                            {{ scope.row.payMoney|numFilter }} {{ scope.row.paymentCurrency }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="isGoOfficaiaBussiness" label="是否到场" width="78">
                        <template slot-scope="isGoOfficaiaBussiness">
                            <span v-if="isGoOfficaiaBussiness.row.isGoOfficaiaBussiness == 1">是</span>
                            <span v-else>否</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="isAudit" label="是否审核" width="78">
                        <template slot-scope="isAudit">
                            <span v-if="isAudit.row.isAudit == 1">已通过</span>
                            <span v-else-if="isAudit.row.isAudit == 0">未审核</span>
                            <span v-else-if="isAudit.row.isAudit == 2">未通过</span>
                            <span v-else>自动审核通过</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="isPay" label="是否付款" width="78">
                        <template slot-scope="isPay">
                            <span v-if="isPay.row.isPay == 1">已付款</span>
                            <span v-else-if="isPay.row.isPay == 0">未付款</span>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="attachment" label="附件" width="122">
                    </el-table-column> -->
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button style="margin-left: 0px;" size="mini" @click="download(scope.$index, scope.row)"
                                v-show="scope.row.attachment">下载附件</el-button>

                            <el-button style="margin-left: 0px;" size="mini" @click="upDate(scope.$index, scope.row)">编辑</el-button>
                            <el-button style="margin-left: 0px;" size="mini" type="danger" @click="del(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>
            <div class="block" style="margin-top:15px;">
                <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 {
    // beforeRouteLeave(to, from, next) {
    //     console.log(to)
    //     console.log(from)
    //     if(to.name!='OpInvitationOfficialActivities'){
    //         from.meta.keepAlive = false;
    //     }
    //     next()
    // },
    data() {
        return {
            loading: false,
            tableDatas: [],
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 12,// 每页的数据条数
            input: '',
            token: '',
            userId: 0,
            DiIdst:'',
            DiId: '',
            inviter: "",
            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]);
                    }
                }]
            },
            InviteTime: '',
            startInviteTime: "",
            endInviteTime: "",
            AirTicketResSelectData: [],
            delegationInfo: {},
        }
    },
    methods: {
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val;
        },
        //团组下拉框
        AirTicketResSelect() {
            var url = "/api/Groups/DecreasePaymentsSelect"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    userId: that.userId,
                    ctId: 81
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    console.log(res);
                    that.AirTicketResSelectData = res.data.data.groupName;
                    if(that.DiIdst==undefined||that.DiIdst!=that.DiIdst){
                        that.DiId =that.AirTicketResSelectData[0].id;
                    }else{
                        that.DiId =that.DiIdst
                    }
                    that.AirTicketResSelectChange()
                    that.InvitationOfficialActivities()
                }
            }).catch(function (error) {
                that.$message.error("网络错误，请稍后重试");
            });
        },
        ConditionsQuery() {
            if (this.InviteTime != "" && this.InviteTime != undefined && this.InviteTime != null) {
                this.startInviteTime = this.InviteTime[0]
                this.endInviteTime = this.InviteTime[1]
            } else {
                this.startInviteTime = ""
                this.endInviteTime = ""
            }
            this.InvitationOfficialActivities();
        },
        AirTicketResSelectChange() {
            // this.$router.push({ query: {} });
            var url = "/api/Business/PostShareGroupInfo"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    id: that.DiId,
                }
            }).then(function (res) {
                if (res.data.code == 200) {

                    that.delegationInfo=res.data.data;
                    that.InvitationOfficialActivities();
                }
            }).catch(function (error) {
                that.$message.error(error);
            });
            // if (this.DiId != 0 && this.DiId != null) {
                
            //     for (let index = 0; index < this.AirTicketResSelectData.length; index++) {

            //         if (this.AirTicketResSelectData[index].id == parseInt(this.DiId)) {
            //             this.delegationInfo = this.AirTicketResSelectData[index];
            //             break;
            //         }
            //     }
            // }

        },

        //商邀费用信息绑定
        InvitationOfficialActivities() {
            var url = "/api/Groups/InvitationOfficialActivitiesList"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    diId: that.DiId,
                    inviter: that.inviter,
                    startInviteTime: that.startInviteTime,
                    endInviteTime: that.endInviteTime,
                    pageIndex: 0,
                    pageSize: 0
                }
            }).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/OpInvitationOfficialActivities",
                query: {
                    DiId: this.DiId,
                    id: row.id
                }
            })
        },
        addIf() {
            if (this.DiId != 0 && this.DiId != '' && this.DiId != undefined) {
                this.$router.push({
                    path: "/home/OpInvitationOfficialActivities",
                    query: {
                        DiId: this.DiId,
                    }
                })

            } else {
                this.$message('请选择团组在进行添加');
            }
        },
        del(index, row) {
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var that = this
                this.$axios({
                    method: 'post',
                    url: "/api/Groups/DelInvitationOfficialActivities",
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    },
                    data: {
                        Id: row.id,
                        DeleteUserId: this.userId
                    }
                }).then(function (res) {


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

                        that.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        // that.AirTicketResSelect();
                        that.AirTicketResSelectChange();
                    } else {
                        that.$message.error('删除失败！');
                    }
                    that.loading = false

                }).catch(function (error) {
                    that.loading = false
                    that.$message.error("网络错误，请稍后重试");
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '操作已取消！'
                });
            }); 
        },
        download(index, row) {
            var that = this
            this.$axios({
                method: 'get',
                url: "/api/Groups/InvitationOfficialActivitiesListDownFile/"+row.id,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
            }).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("网络错误，请稍后重试");
            });
            // if (row.filePath != "") {
            //     window.location.href = "http://132.232.92.186:24/Office/GrpFile/商邀相关文件/" + row.attachment
            // } else {
            //     this.$message.error("暂未上传附件");
            // }

        }
    },
    filters: {
        numFilter(value) {
            let realVal = "";
            if (!isNaN(value) && value !== "") {
            // 截取当前数据到小数点后两位,改变toFixed的值即可截取你想要的数值
            realVal = parseFloat(value).toFixed(2);
            } else {
            realVal = "--";
            }
            return realVal;
        },
        dataFilter(value){
            return value.spilt(' ')[0]
        }
    },

    mounted() {
        this.DiIdst = Number(this.$route.query.DiId);
        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        this.AirTicketResSelect();
        //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;
}
</style>