<template>
    <div>
        <div class="filters-container">

            <el-select v-model="storeId" clearable filterable placeholder="店铺" @change="storeIdChange">
                <el-option v-for="item in storeInfos" :key="item.id" :label="item.storeName" :value="item.id">
                </el-option>
            </el-select>
            <el-select v-model="userId" clearable filterable placeholder="用户" @change="userIdChange">
                <el-option v-for="item in userInfos" :key="item.id" :label="item.nickname" :value="item.id">
                </el-option>
            </el-select>

            <el-input placeholder="关键字" size="small" v-model="filter1" clearable class="filter-item"></el-input>
            <el-button @click="searchData" type="primary" size="small" icon="el-icon-search" class="filter-item">搜索
            </el-button>
            <el-button @click="showAdvancedQueryDialog = !showAdvancedQueryDialog" type="primary" size="small" icon="el-icon-search" class="filter-item">高级查询
            </el-button>
            <el-button @click="showTableSortDialog = !showTableSortDialog" type="primary" size="small" icon="el-icon-sort" class="filter-item">排序
            </el-button>
            <el-button @click="$router.push({path:'/userOrder',query:{column:$route.query.column}})" type="primary" size="small" icon="el-icon-edit" class="filter-item">添加
            </el-button>
            <el-button @click="exportExcel" type="primary" size="small" icon="el-icon-share" class="filter-item">导出EXCEL
            </el-button>
            <el-dropdown trigger="click" size="medium" class="table-column-filter">
                <span>
                    筛选数据
                    <i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown" class="table-column-filter-wrapper">
                    <el-checkbox v-for="item in tableColumns" :key="item.value" v-model="item.show">{{item.label}}
                    </el-checkbox>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
        <el-table :data="tableData" :height="tableHeight" row-key="id" ref="table">
            <el-table-column v-if="multipleMode" align="center" type="selection" width="50">
            </el-table-column>
            <el-table-column type="index" min-width="50" align="center">
            </el-table-column>

            <el-table-column v-if="isColumnShow('id')" prop="id" label="id" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('createTime')" prop="createTime" label="创建时间" :formatter="DateTimeFormatter" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('userId')" prop="userId" label="用户" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('orderCode')" prop="orderCode" label="订单编码" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('storeId')" prop="storeId" label="店铺" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('productType')" prop="productType" :formatter="ProductTypeFormatter" label="商品类型" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('productId')" prop="productId" label="商品ID" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('productName')" prop="productName" label="商品名称" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('specification')" prop="specification" label="规格" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('unitPrice')" prop="unitPrice" label="单价" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('quantity')" prop="quantity" label="数量" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('userCouponId')" prop="userCouponId" label="优惠券" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('offPrice')" prop="offPrice" label="优惠金额" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('totlePrice')" prop="totlePrice" label="总价" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('dealPrice')" prop="dealPrice" label="成交价" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('payMode')" prop="payMode" :formatter="PayModeFormatter" label="支付方式" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('payTime')" prop="payTime" label="支付时间" :formatter="DateTimeFormatter" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('remark')" prop="remark" label="备注" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('showFlag')" prop="showFlag" label="显示标识" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('statusFlag')" prop="statusFlag" :formatter="StatusFlagFormatter" label="状态" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('fee')" prop="fee" label="手续费" min-width="100">
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" min-width="230">
                <template slot-scope="scope">
                    <el-button @click="editRow(scope.row)" type="primary" size="mini" plain>编辑</el-button>
                    <el-button @click="deleteRow(scope.row)" type="danger" size="mini" plain>删除</el-button>
                    <el-dropdown>
                        <el-button type="primary" size="mini" plain>
                            更多
                            <i class="el-icon-arrow-down el-icon--right"></i>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-button @click="$router.push({path:'/OrderImages',query:{column:scope.row.id+',orderId'}})" type="primary" size="small" plain>图片
                            </el-button>
                            <el-button @click="$router.push({path:'/OrderAddresss',query:{column:scope.row.id+',orderId'}})" type="primary" size="small" plain>地址
                            </el-button>
                            <el-button @click="$router.push({path:'/OrderLogisticss',query:{column:scope.row.id+',orderId'}})" type="primary" size="small" plain>物流
                            </el-button>
                            <el-button @click="$router.push({path:'/OrderRepairs',query:{column:scope.row.id+',orderId'}})" type="primary" size="small" plain>返修
                            </el-button>
                            <el-button @click="$router.push({path:'/OrderInvoices',query:{column:scope.row.id+',orderId'}})" type="primary" size="small" plain>发票
                            </el-button>
                             <el-button @click="cleanOssRow(scope.row)" type="danger" size="mini" plain>清理OSS</el-button>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <div class="multiple-mode-wrapper" v-if="0">
                <el-button size="small" v-if="!multipleMode" @click="toggleMultipleMode(true)">批量编辑</el-button>
                <el-button-group v-else>
                    <el-button size="small" @click="operation1">批量操作1</el-button>
                    <el-button size="small" @click="operation2">批量操作2</el-button>
                    <el-button size="small" @click="toggleMultipleMode(false)">取消</el-button>
                </el-button-group>
            </div>
            <el-pagination background @size-change="pageSizeChange" @current-change="currentPageChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalNumber">
            </el-pagination>
        </div>
        <el-dialog title="高级查询" :visible.sync="showAdvancedQueryDialog">
            <el-button @click="addField" type="text" icon="el-icon-plus">添加</el-button>
            <el-table :data="advancedQueryFields">

                <el-table-column prop="link" label="链接符" align="center">
                    <template slot-scope="{row}">
                        <el-select placeholder="链接" size="small" v-model="row.link" class="filter-item">
                            <el-option label="AND" value="AND">
                            </el-option>
                            <el-option label="OR" value="OR">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="字段" align="center">
                    <template slot-scope="{row}">
                        <el-select v-model="row.name">

                            <el-option v-for="item in advancedQueryColumns" :label="item.label" :value="item.value" :key="item.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column prop="searchMethod" label="搜索方式" width="150" align="center">
                    <template slot-scope="{row}">
                        <el-select v-model="row.searchMethod">
                            <el-option v-for="item in searchMethods" :label="item" :value="item" :key="item"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column prop="value" label="参数" align="center">
                    <template slot-scope="{row}">
                        <el-input v-model="row.value"></el-input>
                    </template>
                </el-table-column>
                <el-table-column width="60" align="center">
                    <template slot-scope="{ row, column, $index }">
                        <el-button @click="removeField($index)" size="small" type="text">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <span slot="footer" class="dialog-footer">

                <el-button @click="advancedQuery" :loading="$store.state.fetchingData">确定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="排序" :visible.sync="showTableSortDialog">
            <el-button @click="addSortField" type="text" icon="el-icon-plus">添加</el-button>
            <el-table :data="tableSortFields">

                <el-table-column prop="name" label="字段" align="center">
                    <template slot-scope="{row}">
                        <el-select v-model="row.name">

                            <el-option v-for="item in advancedQueryColumns" :label="item.label" :value="item.value" :key="item.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column prop="order" label="排序" align="center">
                    <template slot-scope="{row}">
                        <el-select v-model="row.order">
                            <el-option label="降序" value="desc">
                            </el-option>
                            <el-option label="升序" value="asc">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column width="60" align="center">
                    <template slot-scope="{ row, column, $index }">
                        <el-button @click="removeSortField($index)" size="small" type="text">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <span slot="footer" class="dialog-footer">

                <el-button @click="tableSortQuery" :loading="$store.state.fetchingData">确定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="查看图片" :visible.sync="imageDialogVisible" size="small">
            <img width="100%" :src="imgSrc" alt="">
        </el-dialog>

    </div>
</template>
<script>
import { mapState } from 'vuex'
import { format } from 'date-fns'
import zh from 'date-fns/locale/zh_cn'

export default {
    created() {

        this.getData();

        this.$http.get({
            url: '/storeInfo/all'
        }).then(res => {
            if (res.success) {
                this.storeInfos = res.data;

            }
        });

        this.$http.get({
            url: '/userInfo/all'
        }).then(res => {
            if (res.success) {
                this.userInfos = res.data;

            }
        });
    },
    data() {
        return {
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 20,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: 'id',
                    value: 'id',
                    show: true
                },
                {
                    label: '创建时间',
                    value: 'createTime',
                    show: true
                },
                {
                    label: '用户',
                    value: 'userId',
                    show: true
                },
                {
                    label: '订单编码',
                    value: 'orderCode',
                    show: true
                },
                {
                    label: '店铺',
                    value: 'storeId',
                    show: true
                },
                {
                    label: '商品类型',
                    value: 'productType',
                    show: true
                },
                {
                    label: '商品ID',
                    value: 'productId',
                    show: true
                },
                {
                    label: '商品名称',
                    value: 'productName',
                    show: true
                },
                {
                    label: '规格',
                    value: 'specification',
                    show: true
                },
                {
                    label: '单价',
                    value: 'unitPrice',
                    show: true
                },
                {
                    label: '数量',
                    value: 'quantity',
                    show: true
                },
                {
                    label: '优惠券',
                    value: 'userCouponId',
                    show: true
                },
                {
                    label: '优惠金额',
                    value: 'offPrice',
                    show: true
                },
                {
                    label: '总价',
                    value: 'totlePrice',
                    show: true
                },
                {
                    label: '成交价',
                    value: 'dealPrice',
                    show: true
                },
                {
                    label: '支付方式',
                    value: 'payMode',
                    show: true
                },
                {
                    label: '支付时间',
                    value: 'payTime',
                    show: true
                },
                {
                    label: '备注',
                    value: 'remark',
                    show: true
                },
                {
                    label: '显示标识',
                    value: 'showFlag',
                    show: true
                },
                {
                    label: '状态',
                    value: 'statusFlag',
                    show: true
                },
                {
                    label: '手续费',
                    value: 'fee',
                    show: true
                },
            ],
            multipleMode: false,
            showAdvancedQueryDialog: false,
            advancedQueryFields: [],
            showTableSortDialog: false,
            tableSortFields: [],
            searchMethods: ['=', '!=', '>', '>=', '<', '<=', 'like'],
            advancedQueryColumns: [
                {
                    label: 'id',
                    value: 'id'
                },
                {
                    label: '创建时间',
                    value: 'create_time'
                },
                {
                    label: '用户',
                    value: 'user_id'
                },
                {
                    label: '订单编码',
                    value: 'order_code'
                },
                {
                    label: '店铺',
                    value: 'store_id'
                },
                {
                    label: '商品类型',
                    value: 'product_type'
                },
                {
                    label: '商品ID',
                    value: 'product_id'
                },
                {
                    label: '商品名称',
                    value: 'product_name'
                },
                {
                    label: '规格',
                    value: 'specification'
                },
                {
                    label: '单价',
                    value: 'unit_price'
                },
                {
                    label: '数量',
                    value: 'quantity'
                },
                {
                    label: '优惠券',
                    value: 'user_coupon_id'
                },
                {
                    label: '优惠金额',
                    value: 'off_price'
                },
                {
                    label: '总价',
                    value: 'totle_price'
                },
                {
                    label: '成交价',
                    value: 'deal_price'
                },
                {
                    label: '支付方式',
                    value: 'pay_mode'
                },
                {
                    label: '支付时间',
                    value: 'pay_time'
                },
                {
                    label: '备注',
                    value: 'remark'
                },
                {
                    label: '显示标识',
                    value: 'show_flag'
                },
                {
                    label: '状态',
                    value: 'status_flag'
                },
                {
                    label: '手续费',
                    value: 'fee'
                },
            ],
            advancedQuerySearchKey: '',
            orderByStr: '',
            imgSrc: '',
            imageDialogVisible: false,
            storeInfos: [],
            storeId: '',
            userInfos: [],
            userId: '',
        }
    },
    computed: {
        ...mapState(['tableHeight']),
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        pageSizeChange(size) {
            this.currentPage = 1;
            this.pageSize = size;
            this.getData();
        },
        currentPageChange(page) {
            this.currentPage = page;
            this.getData();
        },
        getData() {

            var data = {
                currentPage: this.currentPage,
                pageNumber: this.pageSize,
                searchKey: this.filter1,
                advancedQuery: this.advancedQuerySearchKey,
                orderByStr: this.orderByStr,
                storeId: this.storeId,
                userId: this.userId,
            }

            if (this.$route.query.column) {
                var tempColumn = this.$route.query.column;
                data[tempColumn.split(',')[1]] = tempColumn.split(',')[0];
            }

            this.$http.get({
                url: '/userOrder/page',
                data: data
            }).then(res => {
                if (res.success) {
                    this.totalNumber = res.data.page.totalNumber;
                    this.tableData = res.data.pp;
                }
            })
        },
        isColumnShow(column) {
            var row = this.tableColumns.find(i => i.value === column);
            return row ? row.show : false;
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        editRow(row) {
            this.$router.push({
                path: '/userOrder',
                query: {
                    id: row.id,
                    column: this.$route.query.column,
                }
            })
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        addField() {
            this.advancedQueryFields.push({
                link: 'AND',
                name: '',
                searchMethod: '=',
                value: '',
            });
        },
        removeField(i) {
            if (this.advancedQueryFields.length > 0) {
                this.advancedQueryFields.splice(i, 1);
            }
        },
        advancedQuery() {

            this.advancedQuerySearchKey = '';

            if (this.advancedQueryFields.length > 0) {

                var templist = [];

                this.advancedQueryFields.forEach(item => {
                    if (item.link && item.name && item.searchMethod && item.value) {
                        var tempItem = item.link + '_,' + item.name + '_,' + item.searchMethod + '_,' + item.value;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.advancedQuerySearchKey = templist.join('_;');
                }
            }

            this.getData();
            this.showAdvancedQueryDialog = false;
        },
        addSortField() {
            this.tableSortFields.push({
                name: '',
                order: 'asc',
            });
        },
        removeSortField(i) {
            if (this.tableSortFields.length > 0) {
                this.tableSortFields.splice(i, 1);
            }
        },
        tableSortQuery() {

            this.orderByStr = '';

            if (this.tableSortFields.length > 0) {

                var templist = [];

                this.tableSortFields.forEach(item => {
                    if (item.name && item.order) {
                        var tempItem = item.name + '_,' + item.order;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.orderByStr = templist.join('_;');
                }
            }

            this.getData();
            this.showTableSortDialog = false;
        },
        exportExcel() {
            window.location.href = this.$baseUrl + "/userOrder/exportExcel?searchKey="
                + this.filter1 + "&advancedQuery=" + this.advancedQuerySearchKey + "&orderByStr=" + this.orderByStr;
        },
        searchData() {
            this.currentPage = 1;
            this.getData();
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/userOrder/del',
                    data: { id: row.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.getData();
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
        cleanOssRow(row) {
            this.$alert('清理OSS将无法恢复，确认要清理么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/orderImage/handCleanOrderImage',
                    data: { orderId: row.id }
                })
            }).then(() => {
                this.$message.success('成功');
                this.getData();
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('取消');
                } else {
                    this.$message.error('失败');
                }
            })
        },
        DateTimeFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD HH:mm', { locale: zh })
            }

        },
        DateFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD', { locale: zh })
            }

        },
        showImg(img) {
            this.imgSrc = img;
            this.imageDialogVisible = true;
        },
        ProductTypeFormatter(row, column, cellValue) {

            var valueStr = '';
            switch (cellValue) {
                case '0':
                    valueStr = '修图';
                    break;
                case '1':
                    valueStr = '实体商品';
                    break
            }

            return valueStr;

        },
        PayModeFormatter(row, column, cellValue) {

            var valueStr = '';
            switch (cellValue) {
                case 0:
                    valueStr = '支付宝';
                    break;
                case 1:
                    valueStr = '微信';
                    break;
                case 2:
                    valueStr = '余额';
                    break
            }

            return valueStr;

        },
        StatusFlagFormatter(row, column, cellValue) {

            var valueStr = '';
            switch (cellValue) {
                case 0:
                    valueStr = '待付款';
                    break;
                case 1:
                    valueStr = '付款完成';
                    break
                case 2:
                    valueStr = '修图';
                    break
                case 3:
                    valueStr = '验收';
                    break
                case 4:
                    valueStr = '返修';
                    break
                case 5:
                    valueStr = '完成';
                    break
                case 6:
                    valueStr = '结束';
                    break
                case 7:
                    valueStr = '退款中';
                    break
                case 8:
                    valueStr = '取消';
                    break
                case 9:
                    valueStr = '退款失败';
                    break
                case 10:
                    valueStr = '退款完成';
                    break
            }

            return valueStr;

        },
        storeIdChange() {
            this.currentPage = 1;
            this.getData();
        },
        userIdChange() {
            this.currentPage = 1;
            this.getData();
        },

    }
}
</script>
<style lang="less" scoped>
</style>
