<template>
    <div v-loading.fullscreen.lock="fullscreenLoading">
        <div class="currency-all">
            <div class="currency-head">
                <div class="head-ipt">
                    <div class="name-ipt">
                        <label>团组名称:</label>
                        <el-select style="width:250px" @change="chkg" v-model="state1" filterable placeholder="请选择">
                            <el-option
                              v-for="item in restaurants"
                              :key="item.id"
                              :label="item.groupName"
                              :value="item.id">
                            </el-option>
                          </el-select>
                    </div>
                    <div class="preserve-btn">
                        <el-button class="preserve" @click="Savedata()" type="primary">保存数据</el-button>
                    </div>
                </div>
                <div class="currency-info">
                    <div class="currency-info-li">
                        <label>团 号：</label>
                        <span>{{groupInfo.salesQuoteNo}}</span>
                    </div>
                    <div class="currency-info-li">
                        <label>客 户：</label>
                        <span>{{groupInfo.clientUnit}}</span>
                    </div>
                    <div class="currency-info-li">
                        <label>出访国家：</label>
                        <span>{{groupInfo.visitCountry}}</span>
                    </div>
                    <div class="currency-info-li">
                        <label>起止日期：</label>
                        <span>{{groupInfo.tontractTime|filter_time}} ~ {{groupInfo.visitDate|filter_time}}</span>
                    </div>
                    <div class="currency-info-li">
                        <label>天数/人数：</label>
                        <span>{{groupInfo.visitDays}}天/{{groupInfo.visitPNumber}}人</span>
                    </div>
                </div>
            </div>
            <div class="currency-com">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="酒店预订" name="hotel">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in hotelCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                    <!-- <el-tab-pane label="行程" name="itinerary">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in itineraryCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane> -->
                    <el-tab-pane label="车/导游地接" name="guide">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in guideCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="签证" name="visa">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in visaCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="邀请/公务活动" name="business">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in inviteCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="团组客户保险" name="insurance">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in insuranceCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="机票预订" name="ticket">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in ticketCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="其他款项" name="payments">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in paymentsCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                    <!-- <el-tab-pane label="酒店早餐" name="breakfast">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in breakfastCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane> -->
                    <el-tab-pane label="费用超支" name="overspend">
                        <div class="ipt-inif">
                            <div class="ipt" v-for="(item,index) in overspendCurrencyArr" :key="index">
                                <label>{{item.currencyName}}({{item.currencyCode}})：</label>
                                <el-input v-model="item.rate" oninput="value=value.replace(/[^\d.]/g,'')" placeholder="请输入内容"></el-input>
                            </div>
                        </div>
                    </el-tab-pane>
                </el-tabs>
            </div>
        </div>
    </div>
</template>
<script>
import { co } from '@fullcalendar/core/internal-common';
export default {
    data() {
      return {
        userid:'',
        restaurants: [],
        state1: '',
        activeName: 'hotel',
        hotelCurrencyArr:[],//酒店预订
        itineraryCurrencyArr:[],//行程
        guideCurrencyArr:[],//车/导游地接
        visaCurrencyArr:[],//签证
        inviteCurrencyArr:[],//>邀请/公务活动
        insuranceCurrencyArr:[],//团组客户保险
        ticketCurrencyArr:[],//机票预订
        paymentsCurrencyArr:[],//团组增减款项
        breakfastCurrencyArr:[],//酒店早餐
        overspendCurrencyArr:[],//超支费用
        groupInfo:{},
        allcurrency:[],
        hotel:{},//酒店预订
        itinerary:{},//行程
        guide:{},//车/导游地接
        visa:{},//签证
        invite:{},//>邀请/公务活动
        insurance:{},//团组客户保险
        ticket:{},//机票预订
        payments:{},//团组增减款项
        breakfast:{},//酒店早餐
        overspend:{},//超支费用
        fullscreenLoading:false//加载
      };
    },
    methods: {
      //面包页
      handleClick(tab, event) {
        console.log(tab, event);
      },
      //选择团组
      chkg(val){
        console.log(val)
        this.state1=val
        this.GetCurrency()
      },
      //获取团组汇率
      GetCurrency(){
        this.fullscreenLoading=true;
        var url = "/api/Financial/GetGroupRateDataSources"
        var that = this
        this.$axios({
            method: 'post',
            url: url,
            headers: {
                Authorization: 'Bearer ' + this.token
            },
            data: {
                portType: 1,
                pageIndex: 0,
                pageSize: 0,
            }
        }).then(function (res) {
            if(res.data.code!=200){
                that.fullscreenLoading=false;
                that.$message({
                    message: res.data.msg,
                    type: 'warning'
                });
                return;
            }
            that.fullscreenLoading=false;
            that.restaurants = res.data.data.groupData
            // for (let i = 0; i < res.data.data.teamRateData.length; i++) {
            //     res.data.data.teamRateData[i]["rate"] = "0.0000";
            // }
            that.hotelCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.itineraryCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.guideCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.visaCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.inviteCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.insuranceCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.ticketCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.paymentsCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.breakfastCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            that.overspendCurrencyArr = JSON.parse(JSON.stringify(res.data.data.teamRateData));
            if(that.state1==''){
                that.state1=res.data.data.groupData[0].id;
            }
            console.log(res.data.data.teamRateData)
            that.GetGroupRateInfo();
        })
      },
      //获取默认团组汇率
      GetGroupRateInfo(){
        var url = "/api/Financial/GetGroupRateInfo"
        var that = this
        this.$axios({
            method: 'post',
            url: url,
            headers: {
                Authorization: 'Bearer ' + this.token
            },
            data: {
                portType: 1,
                pageIndex: 0,
                pageSize: 0,
                diid:that.state1
            }
        }).then(function (res) {
            if(res.data.code!=200){
                return
            }
            that.groupInfo=res.data.data.groupInfo;
            console.log(res)
            that.allcurrency=res.data.data.teamRateData;
            for(let j=0;j<that.allcurrency.length;j++){
                if(that.allcurrency[j].cTableId == 76) {
                    that.hotel=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 77) {
                    that.itinerary=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 79) {
                    that.guide=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 80) {
                    that.visa=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 81) {
                    that.invite=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 82) {
                    that.insurance=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 85) {
                    that.ticket=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 98) {
                    that.payments=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 751) {
                    that.breakfast=that.allcurrency[j]
                }
                if(that.allcurrency[j].cTableId == 1015) {
                    that.overspend=that.allcurrency[j]
                }
            }
            console.log(that.hotel)
            for(let x=0;x<that.hotelCurrencyArr.length;x++){
                for(let y=0;y<that.hotel.teamRates.length;y++){
                    if(that.hotelCurrencyArr[x].currencyCode==that.hotel.teamRates[y].currencyCode){
                        that.hotelCurrencyArr[x].rate=that.hotel.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.itineraryCurrencyArr.length;x++){
                for(let y=0;y<that.itinerary.teamRates.length;y++){
                    if(that.itineraryCurrencyArr[x].currencyCode==that.itinerary.teamRates[y].currencyCode){
                        that.itineraryCurrencyArr[x].rate=that.itinerary.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.guideCurrencyArr.length;x++){
                for(let y=0;y<that.guide.teamRates.length;y++){
                    if(that.guideCurrencyArr[x].currencyCode==that.guide.teamRates[y].currencyCode){
                        that.guideCurrencyArr[x].rate=that.guide.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.visaCurrencyArr.length;x++){
                for(let y=0;y<that.visa.teamRates.length;y++){
                    if(that.visaCurrencyArr[x].currencyCode==that.visa.teamRates[y].currencyCode){
                        that.visaCurrencyArr[x].rate=that.visa.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.inviteCurrencyArr.length;x++){
                for(let y=0;y<that.invite.teamRates.length;y++){
                    if(that.inviteCurrencyArr[x].currencyCode==that.invite.teamRates[y].currencyCode){
                        that.inviteCurrencyArr[x].rate=that.invite.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.insuranceCurrencyArr.length;x++){
                for(let y=0;y<that.insurance.teamRates.length;y++){
                    if(that.insuranceCurrencyArr[x].currencyCode==that.insurance.teamRates[y].currencyCode){
                        that.insuranceCurrencyArr[x].rate=that.insurance.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.ticketCurrencyArr.length;x++){
                for(let y=0;y<that.ticket.teamRates.length;y++){
                    if(that.ticketCurrencyArr[x].currencyCode==that.ticket.teamRates[y].currencyCode){
                        that.ticketCurrencyArr[x].rate=that.ticket.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.paymentsCurrencyArr.length;x++){
                for(let y=0;y<that.payments.teamRates.length;y++){
                    if(that.paymentsCurrencyArr[x].currencyCode==that.payments.teamRates[y].currencyCode){
                        that.paymentsCurrencyArr[x].rate=that.payments.teamRates[y].rate
                    }
                }
            }

            for(let x=0;x<that.breakfastCurrencyArr.length;x++){
                for(let y=0;y<that.breakfast.teamRates.length;y++){
                    if(that.breakfastCurrencyArr[x].currencyCode==that.breakfast.teamRates[y].currencyCode){
                        that.breakfastCurrencyArr[x].rate=that.breakfast.teamRates[y].rate
                    }
                }
            }
            for(let x=0;x<that.overspendCurrencyArr.length;x++){
                for(let y=0;y<that.overspend.teamRates.length;y++){
                    if(that.overspendCurrencyArr[x].currencyCode==that.overspend.teamRates[y].currencyCode){
                        that.overspendCurrencyArr[x].rate=that.overspend.teamRates[y].rate
                    }
                }
            }
            console.log(that.allcurrency)
        })
      },
      //保存数据
      Savedata(){
        var hotelCurrencyArrer=[]
        for(let a =0;a<this.hotelCurrencyArr.length;a++){
            if(this.hotelCurrencyArr[a].rate!=0){
                delete this.hotelCurrencyArr[a].id
                hotelCurrencyArrer.push(this.hotelCurrencyArr[a])
            }
        }
        var itineraryCurrencyArrer=[]
        for(let s =0;s<this.itineraryCurrencyArr.length;s++){
            if(this.itineraryCurrencyArr[s].rate!=0){
                delete this.itineraryCurrencyArr[s].id
                itineraryCurrencyArrer.push(this.itineraryCurrencyArr[s])
            }
        }
        var guideCurrencyArrer=[]
        for(let d =0;d<this.guideCurrencyArr.length;d++){
            if(this.guideCurrencyArr[d].rate!=0){
                delete this.guideCurrencyArr[d].id
                guideCurrencyArrer.push(this.guideCurrencyArr[d])
            }
        }
        var visaCurrencyArrer=[]
        for(let f =0;f<this.visaCurrencyArr.length;f++){
            if(this.visaCurrencyArr[f].rate!=0){
                delete this.visaCurrencyArr[f].id
                visaCurrencyArrer.push(this.visaCurrencyArr[f])
            }
        }
        var inviteCurrencyArrer=[]
        for(let g =0;g<this.inviteCurrencyArr.length;g++){
            if(this.inviteCurrencyArr[g].rate!=0){
                delete this.inviteCurrencyArr[g].id
                inviteCurrencyArrer.push(this.inviteCurrencyArr[g])
            }
        }
        var insuranceCurrencyArrer=[]
        for(let h =0;h<this.insuranceCurrencyArr.length;h++){
            if(this.insuranceCurrencyArr[h].rate!=0){
                delete this.insuranceCurrencyArr[h].id
                insuranceCurrencyArrer.push(this.insuranceCurrencyArr[h])
            }
        }
        var ticketCurrencyArrer=[]
        for(let j =0;j<this.ticketCurrencyArr.length;j++){
            if(this.ticketCurrencyArr[j].rate!=0){
                delete this.ticketCurrencyArr[j].id
                ticketCurrencyArrer.push(this.ticketCurrencyArr[j])
            }
        }
        var paymentsCurrencyArrer=[]
        for(let k =0;k<this.paymentsCurrencyArr.length;k++){
            if(this.paymentsCurrencyArr[k].rate!=0){
                delete this.paymentsCurrencyArr[k].id
                paymentsCurrencyArrer.push(this.paymentsCurrencyArr[k])
            }
        }
        var breakfastCurrencyArrer=[]
        for(let l =0;l<this.breakfastCurrencyArr.length;l++){
            if(this.breakfastCurrencyArr[l].rate!=0){
                delete this.breakfastCurrencyArr[l].id
                breakfastCurrencyArrer.push(this.breakfastCurrencyArr[l])
            }
        }
        var overspendCurrencyArrer=[]
        for(let l =0;l<this.overspendCurrencyArr.length;l++){
            if(this.overspendCurrencyArr[l].rate!=0){
                delete this.overspendCurrencyArr[l].id
                overspendCurrencyArrer.push(this.overspendCurrencyArr[l])
            }
        }

        console.log(hotelCurrencyArrer)
        var url = "/api/Financial/PostGroupRateUpdate"
        var that = this
        var data= {
                portType: 1,
                pageIndex: 0,
                pageSize: 0,
                createUserId:that.userid,
                diId:that.state1,
                teamRateUpdateInfos:[
                    {
                        id:3750,
                        cTable:76,
                        teamRates:hotelCurrencyArrer
                    },
                    {
                        id:3751,
                        cTable:77,
                        teamRates:itineraryCurrencyArrer
                    },
                    {
                        id:3752,
                        cTable:79,
                        teamRates:guideCurrencyArrer
                    },
                    {
                        id:3753,
                        cTable:80,
                        teamRates:visaCurrencyArrer
                    },
                    {
                        id:3754,
                        cTable:81,
                        teamRates:inviteCurrencyArrer
                    },
                    {
                        id:3755,
                        cTable:82,
                        teamRates:insuranceCurrencyArrer
                    },
                    {
                        id:3756,
                        cTable:85,
                        teamRates:ticketCurrencyArrer
                    },
                    {
                        id:3757,
                        cTable:98,
                        teamRates:paymentsCurrencyArrer
                    },
                    {
                        id:3758,
                        cTable:751,
                        teamRates:breakfastCurrencyArrer
                    },
                    {
                        id:3759,
                        cTable:1050,
                        teamRates:overspendCurrencyArrer
                    },
                ]
            }
            console.log(JSON.stringify(data))
        this.$axios({
            method: 'post',
            url: url,
            headers: {
                Authorization: 'Bearer ' + this.token
            },
            data: {
                portType: 1,
                pageIndex: 0,
                pageSize: 0,
                createUserId:that.userid,
                diId:that.state1,
                teamRateUpdateInfos:[
                    {
                        id:0,
                        cTable:76,
                        teamRates:hotelCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:77,
                        teamRates:itineraryCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:79,
                        teamRates:guideCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:80,
                        teamRates:visaCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:81,
                        teamRates:inviteCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:82,
                        teamRates:insuranceCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:85,
                        teamRates:ticketCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:98,
                        teamRates:paymentsCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:751,
                        teamRates:breakfastCurrencyArrer
                    },
                    {
                        id:0,
                        cTable:1015,
                        teamRates:overspendCurrencyArrer
                    },
                    
                ]
            }
        }).then(function (res) {
            console.log(res)
            if(res.data.code==200){
                that.$message({
                    message: res.data.msg,
                    type: 'success'
                });
            }
        })
      }
    },
    filters:{
        filter_time(value){
            if(value){
                return value.split(' ')[0];
            }else{
                return value
            }
        }
    },
    mounted() {
        this.userid=JSON.parse(localStorage.getItem('userinif')).userInfo.userId;
        this.GetCurrency();
    }
}
</script>
<style>
    .currency-all{
        background-color: #fff;
        padding: 10px;
        box-shadow: 0 0 5px #0005;
        border-radius: 10px;
        height: 100%;
        min-height: 840px;
    }
    .currency-info{
        display: flex;
    }
    .currency-info-li{
        margin-right: 30px;
    }
    .currency-info-li label{
        color: #606266;
        font-size: 15px;
        font-weight: 600;
    }
    .currency-info-li span{
        color: #606266;
        font-size: 14px;
    }
    .name-ipt label{
        color: #606266;
        font-size: 15px;
        font-weight: 600;
    }
    .head-ipt{
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;
    }
    .currency-head{
        margin-bottom: 10px;
    }
    .ipt-inif{
        display: flex;
        flex-wrap: wrap;
    }
    .ipt-inif .el-input__inner{
        width: 150px;
    }
    .ipt{
        display: flex;
        align-items: center;
        margin-bottom: 10px;
    }
    .ipt label{
        width: 150px;
        text-align: end;
        font-size: 12px;
        font-weight: 600;
    }
    .ipt .el-input{
        width: 150px;
    }
</style>