<template>
    <div class="flex-container">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <!-- 搜索区域 -->
            <a-form layout="inline">
                <a-row :gutter="24">
                    <a-col :md="5" :sm="8">
                        <a-form-item label="订单编号：">
                            <a-input placeholder="订单编号" v-model="queryParam.orderNum" style="width: 12rem;"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="4" :sm="6">
                        <a-form-item label="买家账号：">
                            <a-input placeholder="买家账号" v-model="queryParam.account" style="width:10rem;"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="4" :sm="6">
                        <a-form-item label="买家姓名：">
                            <a-input placeholder="买家姓名" v-model="queryParam.name" style="width: 8rem;"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="4" :sm="6">
                        <a-form-item label="付款状态：">
                            <a-select v-model="queryParam.state" style="width: 8rem;">
                                <a-select-option key="">--全部--</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="4">
                        <a-form-item label="县分">
                            <a-select v-model="queryParam.substName" @change="getSchoolNames" style="width: 6rem;">
                                <a-select-option key="">--全部--</a-select-option>
                                <a-select-option v-for="d in subNames" :key="d.value" :value="d.value">{{d.text}}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="12">
                        <a-form-item label="学校">
                            <a-select v-model="queryParam.schoolName" style="width: 18rem;">
                                <a-select-option key="">--全部--</a-select-option>
                                <a-select-option v-for="d in schoolNames" :key="d.value" :value="d.value">{{d.text}}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="6">
                        <a-form-item label="创建时间">
                            <a-range-picker @change="onChange" />
                            <!--<a-range-picker
                                    :placeholder="['开始时间', '结束时间']"
                                    format="YYYY-MM"
                                    :value="queryParam.month"
                                    :mode="tempMode"
                                    @panelChange="handlePanelChange"
                                    style="width: 12rem;"
                            />-->
                        </a-form-item>
                    </a-col>
                    <span class="table-page-search-submitButtons" style="float: left; overflow: hidden;">
                        <a-col :md="6" :sm="24">
                             <a-button @click="search()" type="primary">查询</a-button>
                             <!--<a-divider type="vertical"/>
                             <a-button @click="modifyfunction()" type="primary">添加</a-button>
                             <a-divider type="vertical"/>
                             <a-button @click="excelInto()" type="primary">院线通订单数据导入</a-button>-->
                             <a-divider type="vertical"/>
                             <a-button @click="excelOut()" type="primary">院线通订单数据导出</a-button>
                        </a-col>
                    </span>
                </a-row>
            </a-form>
        </div>
        <a-modal
                title="批量导入"
                :visible="kpiModifyvisible"
                @ok="createfunctionOk0"
                @cancel="createfunctionCancel0"
                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="YRYMmoBan">
                        模板下载：
                        <a-button type="primary" @click="downloadMoBan">院线通订单信息模板</a-button>
                    </p>
                </a-col>
            </a-row>
            <a-row :gutter="24" :v-show="KDmoBan">
                <a-col :md="24" :sm="12">
                    <p>说明：</p>
                    <p>1.批量导入请严格按照模版进行导入;</p>
                </a-col>
            </a-row>
        </a-modal>
        <!--列表-->
        <s-table :columns="columns" :data="rowdata" bordered ref="table" size="small" class="flex-filling" :locale="emptyText">
            <!--拦截器-->
            <template slot="updateDate" slot-scope="text">
                {{ text | dayjs}}
            </template>
            <template slot="state" slot-scope="text">
                <p v-if="text==1">未付款</p>
                <p v-if="text==2">付款成功</p>
                <p v-if="text==3">付款失败</p>
                <p v-if="text==4">已取消订单</p>
                <p v-else=""></p>
            </template>
            <template slot="operation" slot-scope="text,record">
                <span>
                  <a @click="modifyfunction(record)">查看订单详情</a>
                  <a-divider v-if="record.state==1" type="vertical"/>
                  <a-popconfirm title="确定要取消订单吗?" @confirm="remove(record)">
                    <a v-if="record.state==1">取消订单</a>
                  </a-popconfirm>
                </span>
            </template>
        </s-table>
        <!----弹框-->
        <a-modal title="订单详情" :visible="modifyvisible" @ok="createfunctionCancel" @cancel="createfunctionCancel" height="600px" width="1100px">
            <a-table :columns="columns2" :data-source="datas" bordered :row-style="{height:'25px'}">
                <!--拦截器-->
                <template slot="updateDate2" slot-scope="text">
                    {{ text | dayjs}}
                </template>
                <!--拦截器-->
                <template slot="validityPeriod2" slot-scope="text">
                    {{ text | dayjs}}
                </template>
                <template slot="shelfState2" slot-scope="text">
                    <p v-if="text==1">已上架</p>
                    <p v-if="text==2">已下架</p>
                    <p v-else=""></p>
                </template>
                <template slot="state2" slot-scope="text">
                    <p v-if="text==1">未售出</p>
                    <p v-if="text==2">已售出</p>
                    <p v-if="text==3">售出中</p>
                    <p v-else=""></p>
                </template>
                <template v-for="col in ['cardNumber', 'state', 'shelfState','validityPeriod','updateDate']" :slot="col" slot-scope="text, record, index">
                    <div :key="col">
                        <a-input v-if="record.editable" style="margin: -5px 0" :value="text" @change="e => handleChange(e.target.value, record.key, col)"/>
                        <template v-else>
                            {{ text }}
                        </template>
                    </div>
                </template>
            </a-table>
        </a-modal>
    </div>
</template>

<script>
    import STable from '@/components/table';
    import ImportBtn from "../../../components/sysmanage/ImportBtn";
    import {
        yxtOrderList, yxtOrderDelete, getSchoolNames, initSubstName, uploadExcel, downloadMoBan
    } from "../../../api/school-center/yxtOrderAPI"
    import {cloneObject, exportFile} from '@/utils/util';
    import Vue from 'vue';
    import {DatetimePicker} from 'vant';
    import moment from "moment";
    import ConstantActivity from "@/constant/ConstantActivity";
    import {getYxtCard} from "../../../api/school-center/yxtOrderAPI";

    Vue.use(DatetimePicker);

    export default {
        name: "louZhangKPI",
        components: {
            STable, ImportBtn
        },
        data() {
            return {
                emptyText: {emptyText: '暂无数据'},
                Formtable: this.$form.createForm(this),
                modifyvisible: false,
                kpiModifyvisible: false,
                title: ConstantActivity.title,
                add: true,
                queryParam: {
                    orderNum:'',
                    account:'',
                    name: '',
                    state:'',
                    substName: '',
                    schoolName: '',
                    month: '',
                },
                subNames: [],
                schoolNames: [],
                modelData: {
                    id: '',
                    userId: '',
                    sysuName: '',
                    account: '',
                    substName: '',
                    schoolName: '',
                    reportDaily: '',
                    collectiveSales: '',
                    weeklyMeeting: '',
                    organTraining: '',
                    designPlan: '',
                    kpi: '',
                    month: '',
                    isDel: '0'
                },
                columns: [
                    {dataIndex: 'orderNum', width: 40, title: '订单号'},
                    {dataIndex: 'totalPrice', width: 15, title: '金额'},
                    {dataIndex: 'state', width: 15, title: '订单状态', scopedSlots: {customRender: 'state'}},
                    {dataIndex: 'orderType', width: 15, title: '订单类型'},
                    {dataIndex: 'account', width: 20, title: '账号'},
                    {dataIndex: 'sysuName', width: 15, title: '姓名'},
                    {dataIndex: 'schoolName', width: 40, title: '学校'},
                    {dataIndex: 'substName', width: 10, title: '县分'},
                    {dataIndex: 'updateDate', width: 25, title: '创建时间', scopedSlots: {customRender: 'updateDate'}},
                    {
                        dataIndex: 'operation',
                        width: 30,
                        title: '操作',
                        scopedSlots: {customRender: 'operation'},
                        fixed: "right"
                    }
                ],
                columns2: [
                    {title: '兑换券账号',dataIndex: 'cardNumber',width: 25,scopedSlots: { customRender: 'cardNumber' },},
                    {title: '售出状态',dataIndex: 'state',width: 15,scopedSlots: { customRender: 'state2' },},
                    {title: '上架状态',dataIndex: 'shelfState',width: 15,scopedSlots: { customRender: 'shelfState2' },},
                    {title: '有效截止日期',dataIndex: 'validityPeriod',width: 25,scopedSlots: { customRender: 'validityPeriod2' },},
                    {title: '上传时间',dataIndex: 'updateDate',width: 25,scopedSlots: { customRender: 'updateDate2' },}
                ],
                rowdata: parameter => {
                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }
                    let obj = cloneObject(this.queryParam)
                    //表示
                    return yxtOrderList(Object.assign(params, obj)).then(res => {
                        let data = {}
                        if (res.state !== 'success') {
                            this.$message.error("查询失败！", 5);
                            this.emptyText.emptyText = '查询失败！'
                            data = {
                                data: [],
                                pageSize: parameter.pageSize,
                                pageNo: 1,
                                totalCount: 0
                            }
                        } else {
                            data = {
                                data: res.data.records,
                                pageSize: parameter.pageSize,
                                pageNo: parameter.pageNo,
                                totalCount: res.data.total
                            }
                        }
                        return data
                    })
                },
                importTile: "",
                importUrl: "",
                YRYMmoBan: true,
                tempMode:['month', 'month'],
                datas : [],
            }
        },
        methods: {
            moment,
            onChange(date, dateString) {
                this.queryParam.month = dateString;
                console.log(date, dateString);
            },
            excelOut() {
                let params = {
                    pageNo: 1,
                    pageSize: 10
                }
                let obj = cloneObject(this.queryParam)
                let nowdate = moment().format("YYYY-MM-DD");
                //导出
                exportFile(
                    uploadExcel(Object.assign(params, obj)),
                    "院线通订单信息" + nowdate + ".xlsx",
                );

            },
            excelInto() {
                this.importTile = "批量导入院线通订单信息";
                this.YRYMmoBan = true;
                this.importUrl = "manager/ciop/yxtOrder/importDate";
                this.kpiModifyvisible = true
            },
            downloadMoBan: function () {
                exportFile(downloadMoBan(), "院线通订单信息.xlsx");
            },
            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 : "上传文件出错！", 10);
                    return false;
                }
                this.$message.success("上传成功！" + response.data, 5);
            },
            createfunctionOk0() {
                this.kpiModifyvisible = false;
                this.clearmodel0();
            },
            createfunctionCancel0() {
                this.kpiModifyvisible = false;
                this.clearmodel0();
            },
            clearmodel0() {
                this.importTile = "";
                this.YRYMmoBan = false;
                this.importUrl = "";
            },
            search() {
                this.$refs.table.refresh({search: true})
            },
            remove(data) {
                yxtOrderDelete({'id': data.id}).then((res) => {
                    if (res.state == "success") {
                        this.$notification.success({message: res.data, description: '', duration: 4})
                    }
                    else{
                        this.$message.error(res.msg);
                        return;
                    }
                    this.$refs.table.refresh({search: true})
                })
            },
            modifyfunction(data) {
                if (data != undefined) {
                    getYxtCard({'orderId': data.id,'pageNo': 1,'pageSize': 10}).then((res) => {
                        if (res.state == "success") {
                            this.datas = res.data.records
                        } else{
                            this.$message.error(res.msg);
                            return;
                        }
                    })
                }
                this.modifyvisible = true;
            },
            createfunctionCancel() {
                this.modifyvisible = false;
            },
            getSchoolNames() {
                this.schoolNames = []
                this.queryParam.schoolName = ''
                getSchoolNames({"substName": this.queryParam.substName}).then((res) => {
                    if (res.state == "success") {
                        const result = res.data
                        result.forEach((r) => {
                            if (r.schoolName === 'null')
                                return;
                            this.schoolNames.push({
                                value: r.schoolName,
                                text: r.schoolName
                            })
                        })
                    }
                })
            },
            initSubstName() {
                initSubstName().then((res) => {
                    if (res.state == "success") {
                        const result = res.data
                        result.forEach((r) => {
                            if (r.subName === 'null')
                                return;
                            this.subNames.push({
                                value: r.subName,
                                text: r.subName
                            })
                        })
                    }
                })
            }
        },
        mounted() {
            this.initSubstName();
            this.getSchoolNames();
        }
    }
</script>

<style scoped>

</style>