<template>
    <div class="layout-content-wrap">
        <div class="navigation-bar flex-row mb-16">
            <img src="../../assets/image/Link.png"  height="22" alt="">
            <a>/</a>
            <a>案件包列表</a>
        </div>
        
        <div class="layout-tabs-content-box background-color-fff border-radius-4">
            <div class="pt-8 border-b-solid-lighter-1">
                <div class="height-30 flex-row align-items-center justify-content-between mb-24">
                    <div class="f22 color-text-primary">筛选</div>
                    <div class="flex-row align-items-center color-text-regular cursor-pointer"
                         @click="handleToggleSearch">
                        <span class="f14 pr-8">{{showSearch?'展开':'收起'}}</span>
                        <i :class="['f14 el-icon-arrow-down transform-time-default', {
                        'rotate--180': showSearch
                    }]"></i>
                    </div>
                </div>
                <el-collapse-transition>
                    <div v-show="showSearch">
                        <el-row :gutter="56">
                            <el-col :span="8">
                                <div class="flex-row-center align-items-center height-40 mb-24">
                                    <span class="tabs__search-criteria-title flex-shrink-0 pr-16">金融机构</span>
                                    <el-select v-model="queryParam.entrustingAgencyName"
                                           clearable placeholder="请选择金融机构"
                                           @keydown.enter.native="handleSearch"
                                           class="width100">
                                        <el-option
                                                v-for="item in orglist"
                                                :key="item.entrustingAgencyName"
                                                :label="item.entrustingAgencyName"
                                                :value="item.entrustingAgencyName">
                                        </el-option>
                                    </el-select>
                                </div>
                            </el-col>
                            <el-col :span="8">
                                <div class="flex-row-center align-items-center height-40 mb-24">
                                    <span class="tabs__search-criteria-title flex-shrink-0 pr-16">案件包名称</span>
                                    <el-input v-model.trim="queryParam.pkgName"
                                              clearable placeholder="请输入案件包名称"
                                              @keydown.enter.native="handleSearch">
                                    </el-input>
                                </div>
                            </el-col>
                            <el-col :span="8">
                                <div class="flex-row-center align-items-center height-40 mb-24">
                                    <span class="tabs__search-criteria-title flex-shrink-0 pr-16">案件即将到期</span>
                                    <el-select v-model="queryParam.willEnd"
                                            clearable placeholder="请选择案件是否即将到期"
                                            @keydown.enter.native="handleSearch"
                                            class="width100">
                                        <el-option label="是" value="true"></el-option>
                                        <el-option label="否" value="false"></el-option>
                                    </el-select>
                                </div>
                            </el-col>
                        </el-row>
                        
                        <el-row :gutter="56">
                            <el-col :span="18">
                                <div class="flex-row align-items-center height-40 mb-24">
                                    <span class="tabs__search-criteria-title flex-shrink-0 pr-16">委案时间</span>
                                    <span :class="['f14 pr-24 cursor-pointer', i === 0 ? 'pl-8' : '',
                                    activeUsage === item ?'color-primary' : 'color-text-regular']"
                                          v-for="(item, i) in usageOption" :key="i"
                                          @click="handleClickUsage(item)">{{item}}</span>

                                    <el-date-picker
                                            v-model="queryDate"
                                            type="daterange"
                                            clearable
                                            range-separator="至"
                                            start-placeholder="开始日期"
                                            end-placeholder="结束日期"
                                            @change="handleChangeDate">
                                    </el-date-picker>
                                </div>
                            </el-col>
                            <el-col :span="4" :offset="2">
                                <div class="flex-row align-items-center justify-content-end">
                                    <el-button @click="hanldeReset">重置</el-button>
                                    <el-button type="primary" @click="handleSearch">搜索</el-button>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                </el-collapse-transition>
            </div>
            <!-- table -->
            <div class="pt-8">
                <div class="height-56 flex-row align-items-center justify-content-between">
                    <div class="f22 color-text-primary">列表</div>
                </div>
                
                <div >
                    <el-table :data="tableData" :height="`${contentHeight}`" >
                        <el-table-column type="index" label="序号" width="55"></el-table-column>
                        <!-- <el-table-column type="selection" width="55" ></el-table-column> -->
                        <el-table-column prop="pkgName" label="案件包名称" show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="entrustingAgencyName" label="金融机构" show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="channel" label="委案渠道" show-overflow-tooltip ></el-table-column>
                        <el-table-column label="委案开始日期" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span >{{ scope.row.planStartTime | formaDate("yyyy-MM-dd") }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="委案结束日期" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span >{{ scope.row.planEndTime | formaDate("yyyy-MM-dd") }}</span>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column prop="months" label="剩余调解天数" show-overflow-tooltip ></el-table-column> -->
                        <el-table-column prop="caseTotal" label="案件数量" show-overflow-tooltip ></el-table-column>
                        <el-table-column label="在办数量" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.caseStatus" :key="index">
                                    {{ item.status == 2 ? (scope.row.caseTotal - item.count):'' }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="办结数量" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.caseStatus" :key="index">
                                    {{ item.status == 2 ? item.count:'' }}
                                </span>
                            </template>
                        </el-table-column>
                       
                        <el-table-column label="未触达" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.mediateStatus" :key="index">
                                    {{ item.status == 0 ? item.count:'' }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="调解中" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.mediateStatus" :key="index">
                                    {{ item.status == 1 ? item.count:'' }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="已达成方案" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.mediateStatus" :key="index">
                                    {{ item.status == 2 ? item.count:'' }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="已签署协议" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.mediateStatus" :key="index">
                                    {{ item.status == 3 ? item.count:'' }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="调解成功数量" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.mediateStatus" :key="index">
                                    {{ item.status == 5 ? item.count:'' }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="调解失败" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span v-for="(item,index) in scope.row.mediateStatus" :key="index">
                                    {{ item.status == 4 ? item.count:'' }}
                                </span>
                            </template>
                        </el-table-column>
                        <!-- 产品类别、委案时间、委案期限、剩余调解天数、在案数量、结案数量、调解成功数量（签署调解协议即为成功）、调解失败等 -->
                        <el-table-column  label="操作" width="180">
                            <template slot-scope="scope">
                                <div class="flex-row align-items-center">
                                    <el-button size="mini" @click="jumpUrl(`/mediation-management?pkgId=${scope.row.id}&pkgName=${scope.row.pkgName}`)">查看案件</el-button>

                                    <!-- <span v-for="(item,index) in scope.row.mediateStatus" :key="index">
                                        {{ item.status == 1 ? item.count:'' }}
                                    </span> -->
                                    <el-button v-if="scope.row.mediateStatus.find(item=>{return item.status == 1 }).count > 0" size="mini" @click="handleConclude(scope.row)">办结</el-button>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                
                <div class="text-center pt-16">
                    <el-pagination
                        @size-change="getCaseInfoList"
                        @current-change="getCaseInfoList"
                        :current-page="queryParam.current"
                        :page-size="queryParam.size"
                        layout="total, prev, pager, next, jumper"
                        :total="total">
                    </el-pagination>
                </div>

            </div>
        </div>
        

    </div>
</template>
<script>
import api from "@/services/casePackageManagement";
import cmapi from "@/services/caseManagement";
    export default {
        components: {
            
        },
        data() {
            return {
                activeName:'all',
                showSearch: true,
                queryDate: '',
                activeUsage: '',
                
                mediateStatusData:[],//所有案件状态枚举
                financialProductsTypeOptions:[],//金融产品类型
                departmentOptions:[],//区域
                queryParam: {
                    pkgName:'',//案件包名称
                    entrustingAgencyName: '',//金融机构
                    endTime: '',//结束时间
                    beginTime:'',//开始时间
                    willEnd:'',
                    current:1,
                    size:10,
                },
                usageOption: ['今日','昨日', '近一周', '近一月', '近半年', '近一年'],
                tableData: [],
                total:0,
                orglist:[],//金融机构

            }
        },
        created() {
            this.getOrgList()
            this.getCaseInfoList(1)
        },
        computed:{
            // 获取抽屉drawer的内容高度
            contentHeight(){
                let oh = document.documentElement.clientHeight;
                if(this.showSearch){
                    return oh-56-48-366-32
                    
                }else{
                    return oh-56-48-175-32
                }
            }  
        },
        methods: {
            jumpUrl (url) {
                this.$router.push(url);
            },
            getOrgList(){
                api.postorgs({current:1,size:500}).then(res => {
                    if (!res.code) {
                        this.orglist = res.records;
                    }
                })
            },
            // 列表数据
            getCaseInfoList(val){
                this.queryParam.planBegin = this.queryParam.beginTime
                this.queryParam.planEnd = this.queryParam.endTime
                this.queryParam.current = val
                api.postquery_by_case(this.queryParam).then(res => {
                    if (!res.code) {
                        this.tableData = res.records;
                        this.total = res.total;
                    }
                })
            },
            handleToggleSearch() {
                this.showSearch = !this.showSearch
            },
            // 重置
            hanldeReset() {
                for (let key in this.queryParam) {
                this.queryParam[key] = ''
                }
                this.queryParam.current = 1
                this.queryParam.size = 10
                this.activeUsage = ''
                this.queryDate = ''
            },
            handleSearch() {
                this.queryParam.size = 10;
                this.queryParam.current = 1;
                this.getCaseInfoList(1)
            },
            handleClickUsage(item) {
                this.activeUsage = item
                this.setDateFast()
            },
            handleChangeDate() {
                this.activeUsage = ''
                if(this.queryDate === null) {
                    this.setDateFast()
                }else {
                    this.queryParam.beginTime = this.$util.getDayStart(this.queryDate[0])
                    this.queryParam.endTime = this.$util.getDayEnd(this.queryDate[1])
                }

            },
            setDateFast() {
                switch (this.activeUsage) {
                    case "今日":
                        let todayObj = this.$util.getToday()
                        this.queryParam.beginTime = todayObj.todayStart
                        this.queryParam.endTime = todayObj.todayEnd
                        break
                    case "昨日":
                        let yesterdayObj = this.$util.getYesterday()
                        this.queryParam.beginTime = yesterdayObj.beginTime
                        this.queryParam.endTime = yesterdayObj.endTime
                        break
                    case "近一周":
                        let rangeWeek = this.$util.getRecentWeeks()
                        this.queryParam.beginTime = rangeWeek.recentWeekStart
                        this.queryParam.endTime = rangeWeek.todayEnd
                        break
                    case "近一月":
                        let rangeMonth = this.$util.getRecentMonths()
                        this.queryParam.beginTime = rangeMonth.recentMonthStart
                        this.queryParam.endTime = rangeMonth.todayEnd
                        break
                    case "近半年":
                        let rangeMonths = this.$util.getRecentMonths(6)
                        console.log('近半年：', rangeMonths)
                        this.queryParam.beginTime = rangeMonths.recentMonthStart
                        this.queryParam.endTime = rangeMonths.todayEnd
                        break
                    case "近一年":
                        let rangeYear = this.$util.getRecentYears()
                        this.queryParam.beginTime = rangeYear.recentYearStart
                        this.queryParam.endTime = rangeYear.todayEnd
                        break
                    default:
                        this.queryParam.beginTime = ''
                        this.queryParam.endTime = ''
                        this.queryDate = ''
                        break
                }
                if(this.queryParam.beginTime && this.queryParam.endTime) {
                    this.queryDate = [this.queryParam.beginTime, this.queryParam.endTime]
                }

            },
            handleConclude(item) {
                this.$confirm("请确定是否对该案件包进行办结?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(() => {
                    if(!this.$clickThrottle()) { return }//防止重复点击
                    cmapi.updateCaseStatus({caseStatus: 2, pkgId: item.id}).then((res) => {
                        this.$message.success("办结成功");
                        this.getCaseInfoList(1)
                    });
                }).catch(() => {});
            },
            
        }
    }
</script>
<style scoped>
.el-badge{
    top: 3px;
}
</style>