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

            <el-input placeholder="关键字" size="small" v-model="filter1" clearable class="filter-item" style="width:120px"></el-input>
            <el-select placeholder="市" size="small" @change="cityChange" filterable clearable v-model="cityCode" class="filter-item" style="width:120px">
                <el-option v-for="item in cityCodeOptions" :key="item.code" :label="item.shortName" :value="item.code">
                </el-option>
            </el-select>

            <el-select v-model="districtCode" filterable clearable @change="districtChange" placeholder="区" size="small" style="width:140px">
                <el-option v-for="item in districts" :key="item.code" :label="item.fullName" :value="item.code">
                </el-option>
            </el-select>

            <el-select v-model="streetCode" filterable clearable placeholder="街道" size="small" style="width:140px">
                <el-option v-for="item in streets" :key="item.code" :label="item.fullName" :value="item.code">
                </el-option>
            </el-select>

            <el-button @click="searchData" type="primary" size="small" icon="el-icon-search" class="filter-item">搜索
            </el-button>
            <el-button @click="$router.push('/houseInfo')" 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('title')" prop="title" 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('price')" prop="price" label="价格" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('payMode')" prop="payMode" label="付款方式" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('featureCode')" prop="featureCode" label="房源特色" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('houseArea')" prop="houseArea" label="面积" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('room')" prop="room" label="室" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('hall')" prop="hall" label="厅" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('guard')" prop="guard" label="卫" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('totalFloor')" prop="totalFloor" label="总楼层" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('houseFloor')" prop="houseFloor" label="层" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('faced')" prop="faced" label="朝向" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('decoration')" prop="decoration" label="装修" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('checkIn')" prop="checkIn" label="入住" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('elevator')" prop="elevator" label="电梯" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('rentingType')" prop="rentingType" label="出租类型" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('partCode')" prop="partCode" label="房源配置" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('houseIntroduce')" prop="houseIntroduce" label="房源介绍" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('housekeeper')" prop="housekeeper" label="管家" min-width="100">
            </el-table-column>
            <el-table-column prop="phone" label="业务员电话" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('retrieval')" prop="retrieval" label="检索关键字" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('housingDistrict')" prop="housingDistrict" label="小区" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('streetCode')" prop="streetCode" label="街道" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('businessCircleCode')" prop="businessCircleCode" label="商圈" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('districtCode')" prop="districtCode" label="区" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('cityCode')" prop="cityCode" label="市" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('lineCode')" prop="lineCode" label="地铁" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('stationCode')" prop="stationCode" label="地铁站" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('lat')" prop="lat" label="纬度" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('lng')" prop="lng" label="经度" 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('rank')" prop="rank" label="排序" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('hot')" prop="hot" label="热度" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('recommendFlag')" prop="recommendFlag" label="推荐" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('selectedFlag')" prop="selectedFlag" label="精选" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('address')" prop="address" label="地址" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('addressDetail')" prop="addressDetail" label="详细地址" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('buildYear')" prop="buildYear" label="建造年" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('createTime')" prop="createTime" :formatter="DateTimeFormatter" label="创建时间" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('updateTime')" prop="updateTime" :formatter="DateTimeFormatter" label="更新时间" min-width="100">
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="150">
                <template slot-scope="scope">
                    <el-button @click="editRow(scope.row)" v-if="userInfo.roleId!='10'&&userInfo.roleId!='7'" type="primary" size="mini" plain>编辑</el-button>
                    <el-button @click="houseImages(scope.row)" type="primary" 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.$http.get({
            url: '/districtInfo/all',
            data: {
                useFlag: 'Y'
            }
        }).then(res => {
            if (res.success) {
                this.cityCodeOptions = res.data;
            }
        })


        this.getData();
    },
    data() {
        return {
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 20,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: '标题',
                    value: 'title',
                    show: true
                },
                {
                    label: '价格',
                    value: 'price',
                    show: true
                },
                {
                    label: '付款方式',
                    value: 'payMode',
                    show: true
                },
                {
                    label: '房源特色',
                    value: 'featureCode',
                    show: false
                },
                {
                    label: '面积',
                    value: 'houseArea',
                    show: true
                },
                {
                    label: '室',
                    value: 'room',
                    show: true
                },
                {
                    label: '厅',
                    value: 'hall',
                    show: true
                },
                {
                    label: '卫',
                    value: 'guard',
                    show: true
                },
                {
                    label: '总楼层',
                    value: 'totalFloor',
                    show: true
                },
                {
                    label: '层',
                    value: 'houseFloor',
                    show: true
                },
                {
                    label: '朝向',
                    value: 'faced',
                    show: true
                },
                {
                    label: '装修',
                    value: 'decoration',
                    show: true
                },
                {
                    label: '入住',
                    value: 'checkIn',
                    show: true
                },
                {
                    label: '电梯',
                    value: 'elevator',
                    show: true
                },
                {
                    label: '出租类型',
                    value: 'rentingType',
                    show: true
                },
                {
                    label: '房源配置',
                    value: 'partCode',
                    show: false
                },
                {
                    label: '房源介绍',
                    value: 'houseIntroduce',
                    show: false
                },
                {
                    label: '管家',
                    value: 'housekeeper',
                    show: true
                },
                {
                    label: '检索关键字',
                    value: 'retrieval',
                    show: true
                },
                {
                    label: '小区',
                    value: 'housingDistrict',
                    show: true
                },
                {
                    label: '街道',
                    value: 'streetCode',
                    show: true
                },
                {
                    label: '商圈',
                    value: 'businessCircleCode',
                    show: true
                },
                {
                    label: '区',
                    value: 'districtCode',
                    show: true
                },
                {
                    label: '市',
                    value: 'cityCode',
                    show: true
                },
                {
                    label: '纬度',
                    value: 'lat',
                    show: false
                },
                {
                    label: '经度',
                    value: 'lng',
                    show: false
                },
                {
                    label: '备注',
                    value: 'remark',
                    show: true
                },
                {
                    label: '排序',
                    value: 'rank',
                    show: false
                },
                {
                    label: '热度',
                    value: 'hot',
                    show: false
                },
                {
                    label: '推荐',
                    value: 'recommendFlag',
                    show: true
                },
                {
                    label: '精选',
                    value: 'selectedFlag',
                    show: true
                },
                {
                    label: '地址',
                    value: 'address',
                    show: true
                },
                {
                    label: '详细地址',
                    value: 'addressDetail',
                    show: true
                },
                {
                    label: '状态',
                    value: 'statusFlag',
                    show: true
                },
                {
                    label: '建造年',
                    value: 'buildYear',
                    show: true
                },
                {
                    label: '创建时间',
                    value: 'createTime',
                    show: true
                },
                {
                    label: '更新时间',
                    value: 'updateTime',
                    show: true
                },
                {
                    label: '地铁',
                    value: 'lineCode',
                    show: true
                },
                {
                    label: '地铁站',
                    value: 'stationCode',
                    show: true
                },
            ],
            multipleMode: false,
            cityCode: '',
            districtCode: '',
            streetCode: '',
            districts: [],
            streets: [],
            cityCodeOptions: [],
        }
    },
    computed: {
        ...mapState(['tableHeight','userInfo']),
        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: '/houseInfo/page',
                data: {
                    currentPage: this.currentPage,
                    pageNumber: this.pageSize,
                    searchKey: this.filter1,
                    cityCode: this.cityCode,
                    districtCode: this.districtCode,
                    streetCode: this.streetCode,
                }
            }).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: '/houseInfo',
                query: {
                    id: row.id
                }
            })
        },
        houseImages(row) {
            this.$router.push({
                path: '/houseImages',
                query: {
                    houseId: row.id
                }
            })
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        cityChange() {

            this.getDistricts(this.cityCode)
        },
        getDistricts(code) {
            this.districts = [];
            if (code) {

                this.$http.get({
                    url: '/districtInfo/all?cityCode=' + code
                }).then(res => {
                    if (res.success) {
                        this.districts = res.data;
                    }
                });
            }
        },
        districtChange() {
            this.getStreets(this.districtCode);
        },

        getStreets(code) {
            this.streets = [];
            if (code) {

                this.$http.get({
                    url: '/districtInfo/all?districtCode=' + code
                }).then(res => {
                    if (res.success) {
                        this.streets = res.data;
                    }
                });
            }
        },
        DateTimeFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD HH:mm', { locale: zh })
            }

        },
        statusFlagFormatter(row, column, cellValue) {
            var valueStr = '';
            switch (cellValue) {
                case 0:
                    valueStr = '编辑';
                    break;
                case 1:
                    valueStr = '上线';
                    break
                case 2:
                    valueStr = '下线';
                    break
            }

            return valueStr;

        },
        searchData() {
            this.currentPage = 1;
            this.getData();
        }
    }
}
</script>
<style lang="less" scoped>
</style>
