<template>
    <div v-loading="fullscreenLoading">
        <div class="group-list">
            <div class="group-title">
                <div>团组列表</div>
                <div class="group-box">
                    <el-select v-model="value" clearable placeholder="请选择" @change="filterStatus(value)">
                        <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                    <el-input
                    style="width:200px;"
                    @input="Inquireclick()"
                    placeholder="请输入查询内容"
                    v-model="input"
                    clearable>
                    </el-input>
                    <el-button @click="addgroup()" type="primary">新增团组</el-button>
                </div>
            </div>
            <template>
                <el-table
                :data="groupDatas.slice((currentPage-1)*pageSize,currentPage*pageSize)"
                border
                style="width: 100%">
                    <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="tourCode"
                        width="100"
                        label="团 号">
                    </el-table-column>
                    <el-table-column
                        width="100" 
                        prop="salesQuoteNo"
                        label="销售报价号">
                    </el-table-column>
                    <el-table-column
                        prop="teamType"
                        label="团组类型"
                        width="150">
                    </el-table-column>
                    <!-- <el-table-column
                        prop="teamLev"
                        label="级 别"
                        width="80">
                    </el-table-column> -->
                    <el-table-column
                        prop="teamName"
                        label="团队名称"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="clientName"
                        label="客户名称"
                        width="100">
                    </el-table-column>
                    <el-table-column
                        prop="clientUnit"
                        label="客户单位">
                    </el-table-column>
                    <el-table-column
                        prop="visitDate"
                        :formatter="filteryear"
                        label="出访时间"
                        width="100">
                    </el-table-column>
                    <el-table-column
                        prop="visitDays"
                        label="出访天数"
                        width="80">
                    </el-table-column>
                    <el-table-column
                        prop="visitPNumber"
                        label="出访人数"
                        width="80">
                    </el-table-column>
                    <el-table-column
                        prop="jietuanOperator"
                        label="接团操作人"
                        width="110">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                          <el-button
                            size="mini"
                            title="编辑"
                            @click="handleEdit(scope.$index, scope.row)"><i class="el-icon-edit"></i></el-button>
                          <el-button
                            size="mini"
                            title="删除"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row,groupDatas)"><i class="el-icon-delete"></i></el-button>
                            <el-button v-if="scope.row.tourCode==''" size="mini" title="确认出团" type="primary" @click="confirmDeparture(scope.$index, scope.row)"><i class="el-icon-check"></i></el-button>
                            <el-button
                            v-if="scope.row.tourCode!=''"
                            size="mini"
                            title="已出团"
                            ><i class="el-icon-check"></i></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="groupDatas.length">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import { co, el } from '@fullcalendar/core/internal-common';
export default {
    data() {
        return {
            options: [
                {
                    value: '0',
                    label: '全部'
                }, {
                    value: '1',
                    label: '已完成'
                }, {
                    value: '2',
                    label: '未完成'
                }
            ],
            value: '0',
            groupDatas:[],
            groupData: [],
            groupDatass:[],
            currentPage: 1, // 当前页码
            pageSize: 12 ,// 每页的数据条数
            input:'',
            token:'',
            fullscreenLoading:false,
            userid:''
        }
    },
    methods:{
         //每页条数改变时触发 选择一页显示多少行
         handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val;
        },
        Inquireclick(){
            var newarr=[];
            if(this.input==""){
                if(this.value=="0"){
                    newarr=this.groupData;
                }else{
                    newarr=this.groupDatass;
                }
            }else{
                for(var i=0;i<this.groupDatas.length;i++){
                    if(this.groupDatas[i].clientName.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                    else if(this.groupDatas[i].clientUnit.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                    else if(this.groupDatas[i].jietuanOperator.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                    else if(this.groupDatas[i].salesQuoteNo.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                    else if(this.groupDatas[i].teamLev.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                    else if(this.groupDatas[i].teamName.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                    else if(this.groupDatas[i].teamType.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                    else if(this.groupDatas[i].tourCode.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }else if(this.groupDatas[i].visitDate.indexOf(this.input)!=-1){
                        newarr.push(this.groupDatas[i]);
                    }
                }
            }
            this.currentPage=1;
            this.groupDatas=newarr;
        },
        addgroup(){
            this.$router.push({path: '/home/Groupedit'});
        },
        //
        Grouplist(){
            var url="/api/Groups/GetGroupList"
            var that=this
            that.fullscreenLoading = true;
            this.$axios({
                method: 'post',
                url:url,
                headers:{
                    Authorization:'Bearer '+this.token
                },
                data:{
                    portType: 1,
                }
            }).then(function(res){
                if(res.data.code==200){
                    that.groupData=res.data.data
                    that.groupData.forEach(item=>{
                        if(item.clientName==null){
                            item.clientName = ''
                        } 
                        if(item.clientUnit==null){
                            item.clientUnit = ''
                        } 
                        if(item.id==null){
                            item.id = ''
                        } 
                        if(item.isSure==null){
                            item.isSure = ''
                        } 
                        if(item.jietuanOperator==null){
                            item.jietuanOperator = ''
                        } 
                        if(item.salesQuoteNo==null){
                            item.salesQuoteNo = ''
                        } 
                        if(item.teamName==null){
                            item.teamName = ''
                        } 
                        if(item.teamType==null){
                            item.teamType = ''
                        } 
                        if(item.tourCode==null){
                            item.tourCode = ''
                        } 
                        if(item.visitDate==null){
                            item.visitDate = ''
                        } 
                        if(item.visitDays==null){
                            item.visitDays = ''
                        } 
                        if(item.visitPNumber==null){
                            item.visitPNumber = ''
                        } 
                    })
                    that.groupDatas=that.groupData;
                    console.log(that.groupData)
                    that.fullscreenLoading = false;
                }
            })
        },
        filteryear(val){
            let gstime=val.visitDate.split(' ')[0]
            return gstime;
        },
        //筛选状态
        filterStatus(val){
            var newgroup=[];
            if(this.input==""){
                if(val=="0"||val==""){
                    newgroup=this.groupData;
                }else if(val=="1"){
                    for(var j=0;j<this.groupData.length;j++){
                        if(this.groupData[j].isSure==1){
                            newgroup.push(this.groupData[j])
                        }
                    }
                }else if(val=="2"){
                    for(var j=0;j<this.groupData.length;j++){
                        if(this.groupData[j].isSure==0){
                            newgroup.push(this.groupData[j])
                        }
                    }
                }
                this.currentPage=1;
                this.groupDatas=newgroup
                this.groupDatass=newgroup 
            }else{
                if(val=="0"||val==""){
                    newgroup=this.groupData
                }else if(val=="1"){
                    for(var j=0;j<this.groupData.length;j++){
                        if(this.groupData[j].isSure==1){
                            newgroup.push(this.groupData[j])
                        }
                    }
                }else if(val=="2"){
                    for(var j=0;j<this.groupData.length;j++){
                        if(this.groupData[j].isSure==0){
                            newgroup.push(this.groupData[j])
                        }
                    }
                }
                this.currentPage=1;
                this.groupDatas=newgroup
                this.groupDatass=newgroup 
                this.Inquireclick()
            }
        },
        //编辑
        handleEdit(index, row,rows) {
            this.$router.push({path: '/home/Groupedit?id=' + row.id +''});
        },
        confirmDeparture(index, row){
            console.log(row)
            this.$confirm('此操作将会确认出团, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                var url="/api/Groups/SetConfirmationGroup"
                var that=this
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        portType: 1,
                        groupId:row.id,
                        groupsOperator:that.userid
                    }
                }).then(function(res){
                    console.log(res)
                    if(res.data.code==200){
                        that.$message({
                            message:res.data.msg ,
                            type: 'success',
                            offset:50
                        });
                        row.tourCode=res.data.data.tourCode;
                        rows.splice(index, 1);
                    }else{
                        that.$message({
                            message:res.data.msg ,
                            type: 'warning',
                            offset:50
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消确认出团'
                });          
            });
        },
        //删除
        handleDelete(index, row,rows) {
            this.$confirm('此操作将删除该条信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                var url="/api/Groups/GroupDel"
                var that=this
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        portType: 1,
                        id:row.id,
                        userId:that.userid
                    }
                }).then(function(res){
                    if(res.data.code==200){
                        that.$message({
                            message:res.data.msg ,
                            type: 'success',
                            offset:50
                        });
                        rows.splice(index, 1);
                    }else{
                        that.$message({
                            message:res.data.msg ,
                            type: 'warning',
                            offset:50
                        });
                    }
                })
            }).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.Grouplist();
    }
}
</script>
<style>
    .group-list{
        background-color: #fff;
        padding: 10px;
        box-shadow: 0 0 5px #0005;
        border-radius: 10px;
    }
    .group-title{
        display: flex;
        font-size: 17px;
        font-weight:600 ;
        color: #555;
        margin-top: 8px;
        margin-bottom: 2px;
        justify-content: space-between;
        align-items: center;
    }
    .group-box{
        display: flex;
    }
    .group-box>button{
        margin-left: 10px;
        padding: 8px 20px;
    }
    .el-table td.el-table__cell, .el-table th.el-table__cell.is-leaf{
        text-align: center;
    }
    .group-box>.el-input{
        margin-left: 10px;
    }
</style>