<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="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="addEdit" 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-80" row-key="id" ref="table">
            <el-table-column type="expand">
                <template scope="props">
                    <el-table :data="props.row.aimPlaceContentChildList" border stripe v-if="props.row.aimPlaceContentChildList" size="mini">

                        <el-table-column prop="id" label="ID" min-width="100">
                        </el-table-column>

                        <el-table-column prop="aimPlaceContentId" label="目标点内容ID" min-width="100">
                        </el-table-column>

                        <el-table-column prop="type" label="类别" min-width="100">
                        </el-table-column>

                        <el-table-column prop="itemName" label="名称" min-width="100">
                        </el-table-column>

                        <el-table-column prop="itemMess" label="描述" min-width="100">
                        </el-table-column>

                        <el-table-column prop="itemDes" label="详细" min-width="100">
                        </el-table-column>

                        <el-table-column prop="remark1" label="备注1" min-width="100">
                        </el-table-column>

                        <el-table-column prop="remark2" label="备注2" min-width="100">
                        </el-table-column>

                        <el-table-column prop="remark3" label="备注3" min-width="100">
                        </el-table-column>
                        <el-table-column label="操作" align="center" width="150">
                            <template slot-scope="scope">
                                <el-button @click="editChildRow(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>
                </template>
            </el-table-column>

            <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('name')" prop="name" label="名称" min-width="100">
            </el-table-column>

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

            <el-table-column v-if="isColumnShow('ownedLandMarkId')" prop="ownedLandMarkId" label="所属地标ID" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('arContentGroupId')" prop="arContentGroupId" label="AR内容组ID" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('arKitPosX')" prop="arKitPosX" label="ArKit坐标X" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('arKitPosY')" prop="arKitPosY" label="ArKit坐标Y" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('arKitPosZ')" prop="arKitPosZ" label="ArKit坐标Z" min-width="100">
            </el-table-column>

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

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

            <el-table-column v-if="isColumnShow('arKitShowLenth')" prop="arKitShowLenth" label="ArKit一定距离显示" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('gyroPosX')" prop="gyroPosX" label="Gyro坐标X" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('gyroPosY')" prop="gyroPosY" label="Gyro坐标Y" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('gyroPosZ')" prop="gyroPosZ" label="Gyro坐标Z" min-width="100">
            </el-table-column>

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

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

            <el-table-column v-if="isColumnShow('gyroPosShowLenth')" prop="gyroPosShowLenth" label="Gyro一定距离显示" min-width="100">
            </el-table-column>

            <el-table-column v-if="isColumnShow('vuforiaImageInfoAreaMarking')" prop="vuforiaImageInfoAreaMarking" label="Gyro绑定识别图区域标记编号" min-width="100">
            </el-table-column>

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

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

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

            <el-table-column v-if="isColumnShow('remark4')" prop="remark4" label="显示镜头前" min-width="100">
            </el-table-column>

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

            <el-table-column v-if="isColumnShow('remark6')" prop="remark6" label="备注6" min-width="100">
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" min-width="200">
                <template slot-scope="scope">
                    <el-button @click="addChild(scope.row)" type="primary" size="mini" plain>添加子集
                    </el-button>
                    <el-button @click="editRow(scope.row)" type="primary" size="mini" plain>编辑</el-button>
                </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>

        <el-dialog title="目标点内容编辑" :visible.sync="AimPlaceContentShow" width="450px">
            <div>
                <AimPlaceContent :MapWidth='MapWidth' :formData='editAimPlaceContent' @hide='AimPlaceContentShow=false' @update='getData'></AimPlaceContent>
            </div>
        </el-dialog>

        <el-dialog title="子集编辑" :visible.sync="AimPlaceContentChildShow" width="450px">
            <div>

                <AimPlaceContentChild :formData='editAimPlaceContentChild' @hide='AimPlaceContentChildShow=false' @update='getData'></AimPlaceContentChild>
            </div>
        </el-dialog>

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

export default {
    name: 'AimPlaceContents',
    props: {
        MapWidth: {
            type: String,
            default: '1'
        }
    },
    created() {
        this.getData(1);
    },
    data() {
        return {
            AimPlaceContentChildShow: false,
            editAimPlaceContentChild: {},
            AimPlaceContentShow: false,
            editAimPlaceContent: {},
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 20,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: 'ID',
                    value: 'id',
                    show: true
                },
                {
                    label: '名称',
                    value: 'name',
                    show: true
                },
                {
                    label: '类别',
                    value: 'type',
                    show: true
                },
                {
                    label: '所属地标ID',
                    value: 'ownedLandMarkId',
                    show: true
                },
                {
                    label: 'AR内容组ID',
                    value: 'arContentGroupId',
                    show: true
                },
                {
                    label: 'ArKit坐标X',
                    value: 'arKitPosX',
                    show: true
                },
                {
                    label: 'ArKit坐标Y',
                    value: 'arKitPosY',
                    show: true
                },
                {
                    label: 'ArKit坐标Z',
                    value: 'arKitPosZ',
                    show: true
                },
                {
                    label: 'ArKit缩放',
                    value: 'arKitScale',
                    show: true
                },
                {
                    label: 'ArKit角度',
                    value: 'arKitAngle',
                    show: true
                },
                {
                    label: 'ArKit一定距离显示',
                    value: 'arKitShowLenth',
                    show: true
                },
                {
                    label: 'Gyro坐标X',
                    value: 'gyroPosX',
                    show: true
                },
                {
                    label: 'Gyro坐标Y',
                    value: 'gyroPosY',
                    show: true
                },
                {
                    label: 'Gyro坐标Z',
                    value: 'gyroPosZ',
                    show: true
                },
                {
                    label: 'Gyro缩放',
                    value: 'gyroPosScale',
                    show: true
                },
                {
                    label: 'Gyro角度',
                    value: 'gyroPosAngle',
                    show: true
                },
                {
                    label: 'Gyro一定距离显示',
                    value: 'gyroPosShowLenth',
                    show: true
                },
                {
                    label: 'Gyro绑定识别图区域标记编号',
                    value: 'vuforiaImageInfoAreaMarking',
                    show: true
                },
                {
                    label: '模型X',
                    value: 'remark1',
                    show: true
                },
                {
                    label: '模型Y',
                    value: 'remark2',
                    show: true
                },
                {
                    label: '模型Z',
                    value: 'remark3',
                    show: true
                },
                {
                    label: '显示镜头前',
                    value: 'remark4',
                    show: true
                },
                {
                    label: '备注5',
                    value: 'remark5',
                    show: true
                },
                {
                    label: '备注6',
                    value: 'remark6',
                    show: true
                },
            ],
            multipleMode: false,
            showAdvancedQueryDialog: false,
            advancedQueryFields: [],
            showTableSortDialog: false,
            tableSortFields: [],
            searchMethods: ['=', '!=', '>', '>=', '<', '<=', 'like'],
            advancedQueryColumns: [
                {
                    label: 'ID',
                    value: 'id'
                },
                {
                    label: '名称',
                    value: 'name'
                },
                {
                    label: '类别',
                    value: 'type'
                },
                {
                    label: '所属地标ID',
                    value: 'owned_land_mark_id'
                },
                {
                    label: 'AR内容组ID',
                    value: 'ar_content_group_id'
                },
                {
                    label: 'ArKit坐标X',
                    value: 'ar_kit_pos_x'
                },
                {
                    label: 'ArKit坐标Y',
                    value: 'ar_kit_pos_y'
                },
                {
                    label: 'ArKit坐标Z',
                    value: 'ar_kit_pos_z'
                },
                {
                    label: 'ArKit缩放',
                    value: 'ar_kit_scale'
                },
                {
                    label: 'ArKit角度',
                    value: 'ar_kit_angle'
                },
                {
                    label: 'ArKit一定距离显示',
                    value: 'ar_kit_show_lenth'
                },
                {
                    label: 'Gyro坐标X',
                    value: 'gyro_pos_x'
                },
                {
                    label: 'Gyro坐标Y',
                    value: 'gyro_pos_y'
                },
                {
                    label: 'Gyro坐标Z',
                    value: 'gyro_pos_z'
                },
                {
                    label: 'Gyro缩放',
                    value: 'gyro_pos_scale'
                },
                {
                    label: 'Gyro角度',
                    value: 'gyro_pos_angle'
                },
                {
                    label: 'Gyro一定距离显示',
                    value: 'gyro_pos_show_lenth'
                },
                {
                    label: 'Gyro绑定识别图区域标记编号',
                    value: 'vuforia_image_info_area_marking'
                },
                {
                    label: '模型X',
                    value: 'remark1',
                },
                {
                    label: '模型Y',
                    value: 'remark2',
                },
                {
                    label: '模型Z',
                    value: 'remark3',
                },
                {
                    label: '显示镜头前',
                    value: 'remark4',
                },
                {
                    label: '备注5',
                    value: 'remark5'
                },
                {
                    label: '备注6',
                    value: 'remark6'
                },
            ],
            advancedQuerySearchKey: '',
            orderByStr: '',
            imgSrc: '',
            imageDialogVisible: false,
        }
    },
    computed: {
        ...mapState(['tableHeight']),
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        editChildRow(row) {
            this.editAimPlaceContentChild = { ...row }
            this.AimPlaceContentChildShow = true
        },
        pageSizeChange(size) {
            this.currentPage = 1;
            this.pageSize = size;
            this.getData();
        },
        currentPageChange(page) {
            this.currentPage = page;
            this.getData();
        },
        getData(type) {

            var data = {
                currentPage: this.currentPage,
                pageNumber: this.pageSize,
                searchKey: this.filter1,
                advancedQuery: this.advancedQuerySearchKey,
                orderByStr: this.orderByStr,
                ownedLandMarkId: this.$route.query.id
            }

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

            this.$http.get({
                url: '/aimPlaceContent/page',
                data: data
            }).then(res => {
                if (res.success) {
                    this.totalNumber = res.data.page.totalNumber;
                    this.tableData = res.data.pp;
                }
            })

            if (type != 1) {
                this.$emit('updatePoint')
            }
        },
        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();
            }
        },
        addEdit(jsonp) {

            this.editAimPlaceContent = {
                ownedLandMarkId: this.$route.query.id
            }
            if (jsonp) {
                this.editAimPlaceContent.arKitPosX = jsonp.X;
                this.editAimPlaceContent.arKitPosY = jsonp.Y;
                this.editAimPlaceContent.mapIndex = jsonp.index;
            }
            this.AimPlaceContentShow = true
        },
        addChild(row) {
            this.editAimPlaceContentChild = {
                aimPlaceContentId: row.id
            }
            this.AimPlaceContentChildShow = true
        },
        editRow(row) {
            this.editAimPlaceContent = { ...row }
            this.AimPlaceContentShow = true
        },
        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 + "/aimPlaceContent/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: '/aimPlaceContent/del',
                    data: { id: 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;
        },

    },
    components: {
        AimPlaceContent, AimPlaceContentChild
    }
}
</script>
<style lang="less" scoped>
</style>
