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

            <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="$router.push('/seeOrder')" type="primary" size="small" icon="el-icon-edit" class="filter-item">添加
            </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('userId')" prop="userId" label="用户ID" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('userInfo.nickname')" prop="userInfo.nickname" label="用户" min-width="120">
            </el-table-column>
            <el-table-column v-if="isColumnShow('houseId')" prop="houseId" label="房源" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('houseInfo.title')" prop="houseInfo.title" label="房源标题" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('housekeeper')" prop="housekeeper" label="管家ID" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('keeper.keepName')" prop="keeper.keepName" label="管家" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('orderStatus')" prop="orderStatus" :formatter="orderStatusFormatter" label="状态" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('contact')" prop="contact" label="联系人" min-width="120">
            </el-table-column>
            <el-table-column v-if="isColumnShow('telephone')" prop="telephone" label="手机号" min-width="120">
            </el-table-column>
            <el-table-column v-if="isColumnShow('leaveMessage')" prop="leaveMessage" label="留言" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('orderTime')" :formatter="DateTimeFormatter" prop="orderTime" label="预约时间" min-width="140">
            </el-table-column>
            <el-table-column v-if="isColumnShow('timeInterval')" prop="timeInterval" label="预约时段" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('seeTime')" :formatter="DateTimeFormatter" prop="seeTime" label="看房时间" min-width="140">
            </el-table-column>
            <el-table-column v-if="isColumnShow('showFlag')" prop="showFlag" label="用户查看" min-width="100">
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" min-width="150">
                <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>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <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>
    </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();
    },
    data() {
        return {
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 20,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: '用户ID',
                    value: 'userId',
                    show: true
                },
                {
                    label: '用户',
                    value: 'userInfo.nickname',
                    show: true
                },
                {
                    label: '房源',
                    value: 'houseId',
                    show: true
                },
                {
                    label: '房源标题',
                    value: 'houseInfo.title',
                    show: true
                },
                {
                    label: '管家ID',
                    value: 'housekeeper',
                    show: true
                },
                {
                    label: '管家',
                    value: 'keeper.keepName',
                    show: true
                },
                {
                    label: '联系人',
                    value: 'contact',
                    show: true
                },
                {
                    label: '手机号',
                    value: 'telephone',
                    show: true
                },
                {
                    label: '留言',
                    value: 'leaveMessage',
                    show: true
                },
                {
                    label: '预约时间',
                    value: 'orderTime',
                    show: true
                },
                {
                    label: '预约时段',
                    value: 'timeInterval',
                    show: true
                },
                {
                    label: '状态',
                    value: 'orderStatus',
                    show: true
                },
                {
                    label: '看房时间',
                    value: 'seeTime',
                    show: true
                },
                {
                    label: '用户查看',
                    value: 'showFlag',
                    show: true
                },
            ],
            multipleMode: false
        }
    },
    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() {
            this.$http.get({
                url: '/seeOrder/page',
                data: {
                    currentPage: this.currentPage,
                    pageNumber: this.pageSize,
                    searchKey: this.filter1,
                }
            }).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: '/seeOrder',
                query: {
                    id: row.id
                }
            })
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },

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

        },
        orderStatusFormatter(row, column, cellValue) {
            var valueStr = '';
            switch (cellValue) {
                case 0:
                    valueStr = '待客服确认';
                    break;
                case 1:
                    valueStr = '待看房';
                    break
                case 2:
                    valueStr = '已完成';
                    break
                case 3:
                    valueStr = '已取消';
                    break
            }

            return valueStr;

        },
        searchData() {
            this.currentPage = 1;
            this.getData();
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/seeOrder/del',
                    data: { id: row.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.getData();
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
