<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container" style="margin: 0;">
        <div class="container-box">
            <div class="search">
                <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                <el-button class="btn-check" size="medium" type="primary" @click="outList">导出
                </el-button>
            </div>
            <div class="tabbox">
                <el-table height="100%" class="com-table" ref="multipleTable" :data="tableData" :show-summary="true" :summary-method="getSummaries"
                    tooltip-effect="dark" size="small" border style="width: 100%" :header-cell-style="headerStyleFun" :cell-style="cellStyle">
                    <el-table-column prop="cityName" align="center" fixed="left" label="地市"></el-table-column>
                    <el-table-column align="center" width="800px" :label="tooltit">
                        <el-table-column align="center" label="区县">
                            <el-table-column prop="countyCount" align="center" label="区县个数" ></el-table-column>
                            <el-table-column prop="countyDeductCount"  align="center" label="扣罚次数"></el-table-column>
                            <el-table-column prop="countyDeductPoints" align="center" label="扣分" ></el-table-column>
                            <el-table-column prop="countyInterview"  align="center" label="约谈"></el-table-column>
                        </el-table-column>
                        <el-table-column align="center" label="人员">
                            <el-table-column prop="personnelPeopleNumber" align="center" label="扣罚人数" ></el-table-column>
                            <el-table-column prop="personnelDeductCount" align="center" label="扣罚人次" ></el-table-column>
                            <el-table-column prop="personnelDeductPoints" align="center" label="扣分" ></el-table-column>
                            <el-table-column prop="personnelFineMoney" align="center" label="罚款" ></el-table-column>
                            <el-table-column prop="personnelNotification"  align="center" label="通报"></el-table-column>
                        </el-table-column>
                        <el-table-column align="center" label="渠道">
                            <el-table-column prop="channelCount" align="center" label="渠道个数" ></el-table-column>
                            <el-table-column prop="channelDeductCount" align="center" label="扣罚次数" ></el-table-column>
                            <el-table-column prop="channelFineMoney" align="center" label="扣罚金额" ></el-table-column>
                            <el-table-column prop="channelShutDownMonth" align="center" width="100" label="关停工号（1个月、3个月）" ></el-table-column>
                            <el-table-column prop="channelIsSixNotAllow" align="center" label="6个月不允许开户" ></el-table-column>
                            <el-table-column prop="channelIsCancelCooperation" align="center" label="取消合作" ></el-table-column>
                            <el-table-column prop="channelWarnEducation" align="center" width="100" label="警示教育（约谈、通报）" ></el-table-column>
                            <el-table-column prop="channelTerminalSixNotAllow" align="center" width="100" label="末梢渠道6个月不允许开户" ></el-table-column>
                            <el-table-column prop="channelTerminalCancelCooperation" align="center" label="末梢渠道取消合作" ></el-table-column>
                            <el-table-column prop="channelTerminalWarnEducation" align="center" label="末梢渠道警示教育" ></el-table-column>
                        </el-table-column>
                    </el-table-column>
                    <el-table-column align="center" fixed="right" label="操作">
                        <template slot-scope="scope">
                            <el-button @click="handleClick(scope.row)" type="text" size="small">详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
    </fullscreen>
</template>
<script>
    import mySearch from "../../../components/search.vue";

    export default {
        components: {
            mySearch
        },
        data() {

            return {
                tooltit: '处罚台账',
                searchList: [{
                        type: 'month',
                        tit: '开始月份',
                        value: new Date(),
                        width: '49%',
                    },
                    {
                        type: 'month',
                        tit: '结束月份',
                        value: new Date(),
                        width: '49%',
                    },
                ],
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [],
                params: new FormData(),
            }
        },
        methods: {
            cellStyle({row, column, rowIndex, columnIndex}){
                if (columnIndex === 0){
                    return "font-weight:bold;"
                }else {
                    return "";
                }
            },
            headerStyleFun({row, column, rowIndex, columnIndex}){
                if (rowIndex === 0){
                    return "background-color:#d8e4bc;font-size:14px;font-weight:bold;color:black;"
                }else {
                    return "background-color:#d8e4bc;font-size:12px;font-weight:bold;color:black;"
                }
            },
            handleClick(row){
                window.sessionStorage.setItem("antiSpoofingSearchParams", JSON.stringify({
                    queryTime:[this.$formatDate(this.searchList[0].value, "YYYY-MM"), this.$formatDate(this.searchList[1].value, "YYYY-MM")],
                    city:row.cityName
                }))
                this.$router.push({
                    path: "/districtCounty"
                });
            },
            //搜索数据
            searchInfo(v) {
                this.params = new FormData();
                this.params.append("startDate", v[0] ?  this.$formatDate(v[0], "YYYY-MM") : '');
                this.params.append("endDate", v[1] ? this.$formatDate(v[1], "YYYY-MM") : '');
                this.getList(this.params, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                this.$http({
                    url: "/market/antiSpoofing/getAllStandingBook",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"20"}'
                    },
                    data: v,
                }).then((res) => {
                    if (res.data.retCode === '1'){
                        this.$message.error(res.data.retMsg);
                    }else {
                        res.data.data.forEach((data)=> {
                            let keys = Object.keys(data);
                            keys.forEach((key)=>{
                                if (!data[key]){
                                    data[key] = '未填写'
                                }
                            })
                        })
                        this.tableData = res.data.data;
                        this.total = res.data.totalRecord;
                    }
                });
            },
            outList() {
                let param = new FormData();
                param.append("startDate", this.searchList[0].value ? this.$formatDate(this.searchList[0].value, "YYYY-MM") : '');
                param.append("endDate", this.searchList[1].value ? this.$formatDate(this.searchList[1].value, "YYYY-MM") : '');
                this.$http({
                    url: '/market/antiSpoofing/exportStandingBook',
                    method: "post",
                    responseType: 'blob',
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: param,
                }).then((response) => {
                    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                        let blob = new Blob([response.data], {
                            type: response.data.type
                        });
                        window.navigator.msSaveOrOpenBlob(blob, '人员考核模板.xlsx');
                    } else {
                        /* 火狐谷歌的文件下载方式 */
                        var blob = new Blob([response.data])
                        var downloadElement = document.createElement('a')
                        var href = window.URL.createObjectURL(blob);
                        downloadElement.href = href;
                        downloadElement.download = '人员考核模板.xlsx';
                        document.body.appendChild(downloadElement);
                        downloadElement.click();
                        document.body.removeChild(downloadElement);
                        window.URL.revokeObjectURL(href);
                    }
                })
            },
            getSummaries(param) {
                const { columns, data } = param;
                const sums = [];
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = '总计';
                        return;
                    }
                    if (index === 20){
                        sums[index] = 'N/A';
                        return;
                    }
                    const values = data.map(item => Number(item[column.property]));
                    if (!values.every(value => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                    } else {
                        sums[index] = '0';
                    }
                });
                return sums;
            }
        },
        mounted() {
            this.searchInfo([this.searchList[0].value, this.searchList[1].value], 1);
        },
        updated() {
            this.$nextTick(() => {
                this.$refs.multipleTable.doLayout()
            })
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .onetab {
        margin-bottom: 20px;
        padding: 0 20px;
    }

    .titbox {
        div {
            float: right;

            i {
                font-size: 22px;
                margin-left: 20px;
                cursor: pointer;
            }
        }
    }

    .tabbox {
        margin-top: 15px;
    }

    .pageBox {
        text-align: right;
        margin-top: 10px;
    }

    .info-line {
        width: 100%;
        display: block;
        padding-left: 20px;

        div {
            width: 50%;
            display: inline-block;
        }

        span {
            width: 80px;
            display: inline-block;
            text-align: left;

            i {
                color: red;
                display: inline-block;
                padding-right: 5px;
            }
        }

        .el-select,
        .el-input {
            width: calc(100% - 100px);
        }
    }

    .online {
        width: 100%;

        .el-select {
            width: calc(100% - 100px);
        }

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }
    }
</style>
