<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 class="f16">还款凭证</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="6">
                                <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="6">
                                <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.financialOrgName"
                                              clearable placeholder="请输入金融机构名称"
                                              @keydown.enter.native="handleSearch">
                                    </el-input>
                                </div>
                            </el-col>
                            <el-col :span="6">
                                <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.mediatorId"
                                            clearable placeholder="请选择所属调解员"
                                            @keydown.enter.native="handleSearch"
                                            class="width100">
                                        <el-option
                                                v-for="item in MediateUserList"
                                                :key="item.id"
                                                :label="item.realName"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </div>
                            </el-col>
                            <el-col :span="6">
                                <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.respondent"
                                              clearable placeholder="请输入被申请人姓名"
                                              @keydown.enter.native="handleSearch">
                                    </el-input>
                                </div>
                            </el-col>
                        </el-row>
                        <el-row :gutter="56">
                            
                            <el-col :span="6">
                                <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.respondentPhone"
                                              clearable placeholder="请输入被申请人电话"
                                              @keydown.enter.native="handleSearch"
                                              maxlength="11"
                                              >
                                    </el-input>
                                </div>
                            </el-col>
                            <el-col :span="6">
                                <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.respondentIdcard"
                                              clearable placeholder="请输入被申请人证件号"
                                              @keydown.enter.native="handleSearch"
                                              maxlength="18"
                                              >
                                    </el-input>
                                </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 class="flex-row">
                    </div>
                </div>
                <div class="case-table">
                    <el-table :data="tableData" :height="`${contentHeight}`" >
                        <el-table-column type="index" label="序号" width="55"></el-table-column>
                        <el-table-column prop="financialOrgName" label="金融机构名称"  show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="pkgName" label="案件包名称"  show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="caseNo" label="案件编号"  show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="respondent" label="被申请人姓名"  show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="moneyAmount" label="案件总金额(元)"  show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="overdueTimes" label="案件逾期天数"  show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="amount" label="回款金额(元)"  show-overflow-tooltip ></el-table-column>
                        <el-table-column label="回款时间" show-overflow-tooltip >
                            <template slot-scope="scope">
                                <span >{{ scope.row.actualPayDate | formaDate("yyyy-MM-dd") }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="createAt" label="上传还款凭证时间"  show-overflow-tooltip ></el-table-column>
                        <el-table-column prop="mediatorName" label="所属调解员"  show-overflow-tooltip ></el-table-column>
                        <el-table-column label="操作" width="200" >
                            <template slot-scope="scope">
                                <div class="flex-row align-items-center">
                                    <el-button size="mini" class="case-btn" @click="handleCaseShow(scope)">查看凭证</el-button>
                                    <!-- <el-button size="mini" v-if="scope.row.status == 1" class="case-btn" @click="EffectDialog = scope.row">效验</el-button> -->
                                    <el-button size="mini" class="case-btn" @click="UpdateClick(scope)">修改</el-button>
                                </div>
                            </template>

                        </el-table-column>
                    </el-table>
                </div>
                <div class="text-left pt-8">
                    <span class="color-274ea4">合计回款金额：{{Proof_statistics.payback}}元</span>
                </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>
        <!-- 文件预览 -->
        <showFile v-if="fileDialog" :fileDialog.sync="fileDialog" />
        <!-- 效验 -->
        <RepaymentEffectDialog v-if="EffectDialog" :EffectDialog.sync="EffectDialog" />
        <!-- 修改 -->
        <RepaymentUpdateDialog v-if="UpdateDialog" :EffectDialog.sync="UpdateDialog" />
        
    </div>
</template>
<script>
import api from "@/services/caseManagement";
import smapi from "@/services/systemManage";
    export default {
        components: {
            showFile: () => import('../../components/showFile.vue'),//
            RepaymentEffectDialog: () => import('../case-management/components/RepaymentEffectDialog.vue'),//效验
            RepaymentUpdateDialog: () => import('../case-management/components/RepaymentUpdateDialog.vue'),//编辑
        },
        data() {
            return {
                fileDialog:null,
                EffectDialog:null,
                UpdateDialog:null,
                showSearch: true,
                queryDate: '',
                activeUsage: '',
                queryParam: {
                    pkgId:'',
                    financialOrgName:'',//金融机构名称
                    caseNo: '',//案件编号
                    respondent:'',//债券人姓名
                    respondentPhone:'',//债务人手机号
                    respondentIdcard:'',
                    endTime: '',//结束时间
                    beginTime:'',//开始时间
                    startTime:'',//开始时间
                    mediatorId:'',//区域id
                    current:1,
                    size:10,
                    status:'OK',

                },
                usageOption: ['今日','昨日', '近一周', '近一月', '近半年', '近一年'],
                tableData: [],
                total:0,
                caseStatusData:[],
                MediateUserList:[],
                Proof_statistics:{}

            }
        },
        watch: {
            // 监听选中的数据，选中的数据发生变化，通过判断长度给多选按钮赋值
           
        },

        created() {
            if (this.$route.query.dataKey != undefined) {
                this.queryParam.pkgId = this.$route.query.pkgId;
            }
        
            this.getMediateUserList()
            this.getCaseInfoList(1)
        },
        computed:{
            // 获取抽屉drawer的内容高度
            contentHeight(){
                let oh = document.documentElement.clientHeight;
                if(this.showSearch){
                    return oh-56-48-366-85
                    
                }else{
                    return oh-56-48-175-65
                }
            }  
        },
        methods: {
            jumpUrl (url) {
                this.$router.push(url);
            },
            async handleCaseShow(scope){
                let previewUrl = await this.getProofFile(scope.row.proofUrl)
                this.fileDialog = {showfile:{fullUrl:previewUrl,url:scope.row.proofUrl},filelist:[]}
            },
            async getProofFile(url){
                let previewUrl = await this.$fetchApi.viewFullFile({path: url})
                return previewUrl
            },

            UpdateClick(scope){
                this.UpdateDialog = scope.row
                this.UpdateDialog.paybackTime = scope.row.actualPayDate
            }, 
            
            // 调解员数据
            getMediateUserList(){
                let deptId = ''
                if(JSON.parse(sessionStorage.getItem('userInfo'))){
                    deptId = JSON.parse(sessionStorage.getItem('userInfo')).deptId
                }
                smapi.getUserByDeptList({ids:[deptId]}).then(res => {
                    if (!res.code) {
                        this.MediateUserList = res
                    }
                })
            },
            getProofList(){
                this.getCaseInfoList(1)
            },
            // 列表数据
            getCaseInfoList(val){
                this.queryParam.current = val
                this.queryParam.startTime = this.queryParam.beginTime
                api.gettraceProof_page(this.queryParam).then(res => {
                    if (!res.code) {
                        this.tableData = res.records;
                        this.total = res.total;

                    }
                })
                api.gettraceProof_amount(this.queryParam).then(res => {
                    if (!res.code) {
                        this.Proof_statistics = res;
                    }
                })

                
            },
            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]
                }

            },

        }
    }
</script>
<style scoped lang="scss">
.el-badge{
    top: 3px;
}
.case-table{
    .table-row{
        color: #86909C;
        p{padding: 0;margin: 0;}
        .case-bg{
            background-color: #eceef1d1;
            padding: 10px 20px;
            margin: 5px 0 10px 0;
        }
        .table-span-one span{width: 180px;}
        .case-btn{
            color: #C66A5B;
            border-color: #C66A5B;
            margin: 0 5px;
        }
    }
}

</style>
<style lang="scss">
.case-table{
    .el-table tr {
        background-color: #f7f8fa63 !important;
    }
    .el-checkbox{line-height: 10px;margin-top: -3px;}
    .el-checkbox .el-checkbox__label{width: 0px;overflow: hidden;}
}
</style>