<template>
    <div class="flex-container">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <!-- 搜索区域 -->
            <a-form layout="inline">
                <a-row :gutter="24">
                    <a-col :md="4" :sm="8">
                        <a-form-item  label="电信激活号码：">
                            <a-input placeholder="" v-model="queryParam.title"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :md="3" :sm="6">
                        <a-form-item  label="放号人：">
                            <a-input  placeholder="" v-model="queryParam.name"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :md="4" :sm="8">
                        <a-form-item  label="放号账号：">
                            <a-input  placeholder="" v-model="queryParam.account"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :md="4" :sm="8">
                        <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}" label="审核状态">
                        <a-select placeholder="请选择" v-model="queryParam.status">
                                <a-select-option key="">请选择</a-select-option>
                                <a-select-option key="0">重复放号</a-select-option>
                                <a-select-option key="1">未审核</a-select-option>
                                <a-select-option key="2">系统审核不通过</a-select-option>
                                <a-select-option key="3">充值审核通过</a-select-option>
                                <a-select-option key="4">充值审核不通过</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col :md="4" :sm="8">
                        <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}" label="订单类型">
                            <a-select placeholder="请选择" v-model="queryParam.orderType">
                                <a-select-option key="">请选择</a-select-option>
                                <a-select-option key="1">放号登记</a-select-option>
                                <a-select-option key="0">新用户办理</a-select-option>
                                <a-select-option key="2">存量升级</a-select-option>
                                <a-select-option key="3">小白卡</a-select-option>
                                <a-select-option key="4">羊城通</a-select-option>
                                <a-select-option key="5">线上预制卡</a-select-option>
                                <a-select-option key="6">线上小白卡</a-select-option>
                                <a-select-option key="7">自助终端</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <span class="table-page-search-submitButtons" style="overflow: hidden;">
                        <a-col :md="6" :sm="24">
                            <a-button @click="search()" type="primary">查询</a-button>
                             <a-divider type="vertical"/>

                            <a-button  v-show="!callback" @click="exportList()" style="margin-left: 10px">导出</a-button>
                            <a-dropdown>
                              <a-menu slot="overlay" @click="handleMenuClick">
                                <a-menu-item key="1">
                                  <a-icon type="arrow-up" />放号上传
                                </a-menu-item>
                                <a-menu-item key="2">
                                  <a-icon type="arrow-up" />充值审核
                                </a-menu-item>
                                <a-menu-item key="3">
                                  <a-icon type="arrow-up" />累计充值导入
                                </a-menu-item>
                              </a-menu>
                              <a-button style="margin-left: 8px">
                                导入
                                <a-icon type="down" />
                              </a-button>
                            </a-dropdown>

                        </a-col>
                    </span>
                </a-row>
            </a-form>
           </div>

        <!--列表-->
        <s-table  :locale="emptyText" :columns="columns" :data="rowdata" bordered ref="table" size="small" class="flex-filling">
            <template slot="imageUrl" slot-scope="text">
                <img style="width: 20%" :src="apis + text" @click="imgClick(text)">
            </template>
            <template slot="operation" slot-scope="text,record">
<!--            <a  @click="modifyfunction(record)">编辑</a>-->
                <!--<a-divider type="vertical"/>-->
                <a-popconfirm title="是否要删除此行？" @confirm="deleteOrder(record.id)">
                    <a>删除</a>
                </a-popconfirm>
                <!--<a-divider type="vertical"/>-->
            </template>
            <template slot="orderTypeScope" slot-scope="text">
                <p v-if="text=='1'">放号登记</p>
                <p v-if="text=='0'">新用户办卡</p>
                <p v-if="text=='2'">存量升级</p>
                <p v-if="text=='3'">小白卡</p>
                <p v-if="text=='4'">羊城通</p>
                <p v-if="text=='5'">线上预制卡</p>
                <p v-if="text=='6'">线上小白卡</p>
                <p v-if="text=='7'">自助终端</p>
            </template>
            <template slot="statusScope" slot-scope="text">
                <p v-if="text=='0'">重复放号</p>
                <p v-if="text=='1'">未审核</p>
                <p v-if="text=='2'">系统审核不通过</p>
                <p v-if="text=='3'">充值审核通过</p>
                <p v-if="text=='4'">系统审核不通过</p>
            </template>
        </s-table>

        <a-modal
                title="批量导入"
                :visible="modifyvisible"
                @ok="createfunctionOk"
                @cancel="createfunctionCancel"
                html-type="submit"
                width="400px"
        >
            <a-row :gutter="24">
                <a-col :md="24" :sm="12">
                    <p style="text-align:center">{{ importTile }}</p>
                </a-col>
            </a-row>
            <import-btn :url="importUrl" :responseFun="doPush">
                <a-tooltip placement="topLeft" title="导入文件" arrowPointAtCenter>
                    <a-row :gutter="24">
                        <a-col :md="24" :sm="12">
                            <p>
                                选择文件：
                                <a-button type="primary">选择文件</a-button>
                            </p>
                        </a-col>
                    </a-row>
                </a-tooltip>
            </import-btn>
            <a-row :gutter="24">
                <a-col :md="24" :sm="12">
                    <p v-show="FHTemplate">
                        模板下载：
                        <a-button type="primary" @click="downloadTemplateFH">放号上传模板</a-button>
                    </p>
                    <p v-show="CZTemplate">
                        模板下载：
                        <a-button type="primary" @click="downloadTemplateCZ">充值审核模板</a-button>
                    </p>
<!--                    <p v-show="LJTemplate">-->
<!--                        模板下载：-->
<!--                        <a-button type="primary" @click="downloadTemplateLJ">累计审核模板</a-button>-->
<!--                    </p>-->
                </a-col>
            </a-row>
        </a-modal>
    </div>
</template>

<script>
    import STable from '@/components/table';
    import moment from 'dayjs'
    import {
        getList,
        deleteOrder,getSubstList,getSchoolList,
        downloadTemplateFH,downloadTemplateCZ,
        downloadResult,exportList
    } from "@/api/school-center/salesListAPI"
    import {cloneObject,exportFile} from '@/utils/util';
    import ImportBtn from "../../../components/sysmanage/ImportBtn";

    export default {
        name: "salesList",
        components: {
            STable,ImportBtn
        },
        data: function () {
            return {
                emptyText: {emptyText: '暂无数据'},
                previewVisible: false,
                previewImage: '',
                fileList: [
                ],
                apis:'',
                fileid:{},
                brackgroundurl:"manager/ciop/school/schoolManagement/qrcodeUpload",
                // form
                Formtable: this.$form.createForm(this, {name: 'Formtable'}),
                FHTemplate: false,
                CZTemplate: false,
                importUrl: "",
                importTile: "",
                modifyvisible: false,
                modifyForm: {
                    id: '',
                    title: '',
                    desc: '',
                    integral: '',
                    activityType: '',
                    subclass: '',
                    imageUrl: '',
                    range: '',
                    sort: '',
                },
                subNameList: [],
                packageName:[],
                chosePages:[],
                products:[],
                //输入框列表
                queryParam: {
                    orderPhone: '',
                    name: '',
                    account: '',
                    status: '',
                    orderType: '',
                    createTimeStart: '',
                    createTimeEnd: '',
                    activateTimeStart: '',
                    activateTimeEnd: '',
                },
                columns: [
                    {dataIndex: 'salesSchool', width: 200, title: '销售学校', align: "center"},
                    {dataIndex: 'salesSubst', width: 200, title: '销售县分', align: "center"},
                    {dataIndex: 'orderPhone', width: 200, title: '电信激活号码', align: "center"},
                    {dataIndex: 'name', width: 200, title: '放号人', align: "center"},
                    {dataIndex: 'account', width: 200, title: '放号人账号', align: "center"},
                    {dataIndex: 'createTime', width: 200, title: '上传时间', align: "center",
                      customRender: (text, row, index) => {
                        return moment(text).format("YYYY-MM-DD HH:mm:ss");
                      }
                    },
                    {
                        dataIndex: 'orderType', width: 200, title: '订单类型', align: "center",
                        scopedSlots: {customRender: "orderTypeScope"}
                    },
                    {dataIndex: "preType", width:200, title:'销售时段', align : "center"},
                    {dataIndex: 'networkName', width: 200, title: '所属网点', align: "center"},
                    {dataIndex: 'networkCode', width: 200, title: '网点编码', align: "center"},
                    {
                        dataIndex: 'status', width: 200, title: '审核状态', align: "center",
                        scopedSlots: {customRender: "statusScope"}
                    },
                    {dataIndex: 'remark', width: 200, title: '审核备注', align: "center"},
                    {dataIndex: 'auditTime', width: 200, title: '审核时间', align: "center",
                      customRender: (text, row, index) => {
                        if (text == null) {
                          return "";
                        }
                        return moment(text).format("YYYY-MM-DD HH:mm:ss");
                      }
                    },
                    {dataIndex: 'activateTime', width: 200, title: '激活时间', align: "center",
                      customRender: (text, row, index) => {
                        if (text == null) {
                          return "";
                        }
                        return moment(text).format("YYYY-MM-DD HH:mm:ss");
                      }
                    },
                    {dataIndex: 'recentlyAmount', width: 200, title: '最近充值', align: "center"},
                    {dataIndex: 'recentlyBonus', width: 200, title: '最近充值激励', align: "center"},
                    {dataIndex: 'capitalBonus', width: 200, title: '本金佣金', align: "center"},
                    {dataIndex: 'monthAmount', width: 200, title: '累计充值金额', align: "center"},
                    {dataIndex: 'monthBonus', width: 200, title: '累计充值激励', align: "center"},
                    {dataIndex: 'allBonus', width: 200, title: '应发总激励', align: "center"},
                    {dataIndex: 'nonSendBonus', width: 200, title: '未发激励', align: "center"},
                    {dataIndex: 'sendBonus', width: 200, title: '已发激励', align: "center"},
                    {dataIndex: 'oldCombo', width: 200, title: '原套餐', align: "center"},
                    {dataIndex: 'upgradeCombo', width: 200, title: '升级套餐', align: "center"},
                    {
                        dataIndex: 'operation',
                        width: 120,
                        title: '操作',
                        align: "center",
                        scopedSlots: {customRender: 'operation'},
                        fixed: "right"
                    }]
                ,
                rowdata: parameter => {
                    const env = process.env.NODE_ENV; //获取当前环境
                    if(env === 'development'){     //测试环境默认添加前缀
                        this.apis = '/api/';
                    }

                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }
                    //表示
                    let obj = cloneObject(this.queryParam)
                    return getList(Object.assign(params, obj)).then(res => {
                        let data = {}
                        if (res.state !== 'success') {
                            this.emptyText.emptyText = '查询失败！'
                            this.$message.error("查询失败！", 5);

                            data = {
                                data: [],
                                pageSize: parameter.pageSize,
                                pageNo: 1,
                                totalCount: 0
                            }
                        } else {

                            if (res.data.records.length==0){
                                return data
                            }

                            data = {
                                data: res.data.records,
                                pageSize: parameter.pageSize,
                                pageNo: parameter.pageNo,
                                totalCount: res.data.total
                            }
                        }
                        return data
                    })
                }
            }
        },
        methods:{
            search() {
                this.$refs.table.refresh({search: true})
            },
            handleMenuClick(e) {
                if (e.key == "1") {
                    this.handleSubmit1();
                }
                if (e.key == "2") {
                    this.handleSubmit2();
                }
            },
            handleSubmit1() {
                this.importTile = "放号上传";
                this.FHTemplate = true;
                this.importUrl = "manager/ciop/salesList/importOrder";
                this.modifyvisible = true;
            },
            handleSubmit2() {
                this.importTile = "充值审核";
                this.CZTemplate = true;
                this.importUrl = "manager/ciop/salesList/importRechargeData";
                this.modifyvisible = true;
            },
            downloadTemplateFH: function() {
                exportFile(downloadTemplateFH(), "放号上传清单.xlsx");
            },
            downloadTemplateCZ: function() {
                exportFile(downloadTemplateCZ(), "充值审核清单.xlsx");
            },
            // downloadTemplateLJ: function() {
            //     exportFile(downloadMoBanKD(), "累计充值审核清单.xlsx");
            // },
            createfunctionOk() {
                this.modifyvisible = false;
                this.clearmodel();
            },
            createfunctionCancel() {
                this.modifyvisible = false;
                this.clearmodel();
            },
            clearmodel() {
                this.importTile = "";
                this.FHTemplate = false;
                this.CZTemplate = false;
                this.importUrl = "";
            },
            // 执行上传
            doPush(res) {
                console.log(res);
                if (!res || !res.response) {
                    this.$message.error("上传文件出错！", 10);
                    return false;
                }
                const response = res.response;
                if (response.state !== "success") {
                    this.$message.error(response.msg ? response.msg : "上传文件出错！", 5);
                    return false;
                }
                this.$message.success("上传成功！" + response.data.msg, 5);
                if(response.data.msg.indexOf("返回导入结果清单") > -1){
                    exportFile(downloadResult({"key": response.data.key}), "导入结果清单.xlsx");
                }
                this.createfunctionCancel();
                this.search();

            },
            deleteOrder(id){
                let params = {};
                params.id = id;

                deleteOrder(params).then(res => {
                    if(res.state == "success"){
                        this.$message.success(res.data ? res.data : '删除成功', 5)
                        this.search();
                        return null;
                    }

                    this.$message.success(res.data ? res.data : '删除失败', 5);
                })
            },
            exportList(){

                exportFile(exportList(this.queryParam),'销售清单.xlsx');
            }



        }
    }
</script>

<style scoped>


</style>

