<template>
    <div>
        <Row style="margin-top: 20px;width: 100%;background-color: #F7F8FA;">
            <i-col span="24" style="text-align: left;padding-left: 10px;">
                <Row class="row-style">
                    <i-col span="1" style="text-align: right;">
                        <p class="region">选择日期</p>
                    </i-col>
                    <i-col span="3" style="padding-left: 10px;">
                        <DatePicker :value="selectDate" type="daterange" split-panels placeholder="请选择日期" @on-change="onChangeDateLister"
                                    style="width: 200px"></DatePicker>
                    </i-col>
                    <i-col span="2" style="text-align: right;">
                        <span class="region">零售公司</span>
                    </i-col>
                    <i-col span="4" style="padding-left: 10px;">
                        <Select v-model="selectedCompany" filterable style="width: 200px;">
                            <Option v-for="item in companyList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                        </Select>
                    </i-col>
                    <i-col span="4">
                        <Button type="primary" @click="generalizeOfRetail">查询</Button>
                    </i-col>
                    <div style="text-align: right;padding-right: 20px;float: right">
                        <Button type="primary" @click="exportData">导出数据</Button>
                    </div>
                </Row>
            </i-col>
        </Row>


        <Table ref="table" :columns="columns1" :data="data1" style="margin-top: 20px;"></Table>
        <Page :total="totalSize" :current="pageNum" :page-size="pageSize" show-elevator show-total
              placement="top" @on-change="handlePage" style="float: right;margin-top: 20px"></Page>
    </div>
</template>

<script>
    import IncreaseData from "../../services/generalize/IncreaseData";

    export default {
        name: "IncreaseDataCompany",
        data() {
            return {
                selectedCompany: null,
                companyList: [],
                columns1: [
                    {
                        type: 'index',
                        title: '序号',
                        key: 'id'
                    },
                    {
                        title: '零售公司名称',
                        key: 'retailCompany'
                    },
                    {
                        title: '总好友数',
                        key: 'allCustomer'
                    },
                    {
                        title: '总好友数（去重）',
                        key: 'effectiveCustomer'
                    },
                    {
                        title: '新增好友数',
                        key: 'newCustomer'
                    },
                    {
                        title: '新增好友数（去重）',
                        key: 'newEffectiveCustomer'
                    },
                    {
                        title: '日增好友平均（去重）',
                        key: 'avgNewCustomer'
                    },
                    {
                        title: '删除/拉黑成员客户数（累计）',
                        key: 'delCustomer'
                    },
                    {
                        title: '拉黑率（累计）',
                        key: 'delRate'
                    }
                ],
                data1: [],
                //分页内容
                totalSize: 0,
                pageNum: 1,
                pageSize: 10,
                selectDate: [],
            }
        },
        mounted: function () {
            let startDate = this.$moment(new Date()).add(-30,"day").format("YYYY-MM-DD");
            let endDate = this.$moment(new Date()).format("YYYY-MM-DD");
            this.selectDate.push(startDate);
            this.selectDate.push(endDate);
            this.allCompany();
            this.generalizeOfRetail();
        },
        methods: {
            onChangeDateLister(date) {
                this.selectDate = date;
            },
            allCompany() {
                let that = this;
                let request = {
                    userId: JSON.parse(sessionStorage.getItem("loginInfo")).userId
                };
                this.companyList = [];
                IncreaseData.listOfRetailCompany(request, function (data) {
                    data = data.data;
                    if (data.code === '0001') {
                        that.$Message.error("查询零售公司出错!");
                        return;
                    }
                    if (data.code === '0000') {
                        that.companyList.push({id:'-1',name:"全部"});
                        data.results.forEach(da => {
                            that.companyList.push(da);
                        })
                    }
                })
            },
            generalizeOfRetail() {
                if(this.selectDate[0] === ""){
                    this.$Message.info("请选择日期");
                    return
                }
                debugger
                let orgId = this.selectedCompany;
                if(this.selectedCompany === '-1'){
                    orgId = null;
                }
                let request = {
                    startTime: this.selectDate[0],
                    endTime: this.selectDate[1],
                    orgId: orgId,
                    pageNum: this.pageNum,
                    pageSize: this.pageSize
                };
                let that = this;
                that.data1 = [];
                IncreaseData.generalizeOfRetail(request, function (data) {
                    data = data.data;
                    if (data.code === '0001') {
                        that.$Message.info("查询推广数据失败！");
                        return;
                    }
                    if (data.code === '0000') {
                        that.data1 = data.results.dataVOS;
                        that.totalSize = data.results.sizeNum;
                    }
                })
            },
            //切页处理
            handlePage: function (value) {
                this.pageNum = value;
                this.generalizeOfRetail();
            },
            exportData(){
                this.$refs.table.exportCsv({
                    filename: 'The original data'
                });
            },
        }
    }
</script>

<style scoped>
    .region {
        font-size: 14px;
        line-height: 32px;
    }

    .row-style {
        padding-top: 20px;
        padding-bottom: 20px;
    }
</style>
