<template>
    <div id="handsonStep2">
        <div class="btns-view">
            <el-button style="width:164px">AI自动识别</el-button>
            <el-button @click="discern(1)">批量识别设备类型关键字</el-button>
            <el-button @click="discern(2)">批量识别设备参数关键字</el-button>
            <el-button style="width:164px" @click="examine">检查&微调</el-button>
            <div style="float:right;">
                <el-button @click="reset">刷新</el-button>
                <el-button @click="saveData">保存</el-button>
            </div>
        </div>
        <div id="handsontableSteps1" v-loading="isLoading">
            <handsontable-component v-if="!!pages.total" @delete="delePoint" @mouseDown="clickTable" @change="changeHand" ref="handsontable"></handsontable-component>
            <div v-else class="center">
                <i class="iconwushuju iconfont"></i> 暂无数据
            </div>
        </div>
        <div class="right">
            <pagination :page="pages" @change="changePage"></pagination>
        </div>
        <own-dialog :width="'60%'" :title="title" :dialogVisible="isDialogShow" @cancel="close">
            <find-keyword ref="findKeyword" :type="type" @change="changeHand"></find-keyword>
        </own-dialog>
        <own-dialog :width="'1000px'" :index="true" title="关键内容设别——检查&微调" :dialogVisible="examineDialog" @cancel="close">
            <!-- <find-keyword ref="findKeyword" :type="type"></find-keyword> -->
            <examine-main></examine-main>
        </own-dialog>
        <!-- <own-dialog :width="'1000px'" :index="true" title="关键内容设别——检查&微调" :dialogVisible="examineDialog" @cancel="close"> -->
        <!-- <find-keyword ref="findKeyword" :type="type"></find-keyword> -->
        <!-- <examine-main></examine-main>
            </own-dialog> -->
        <own-dialog :width="'500px'" :index="true" title="位置标签" :dialogVisible="localtionDialog" @cancel="localFalgChange">
            <localtion-falg :renderData="renderData" @changeTag="changeLoc"></localtion-falg>
        </own-dialog>
    </div>
</template>
<script>
    import handsontableComponent from "@/components/common/handsontable"
    import examineMain from "@/components/config_point/examine_main"
    import headerArr from "@/utils/point_edit/steps2.js"
    import localtionFalg from "@/components/config_point/location_flag"
    import {
        changeHeader,
        showTypes
    } from "@/utils/handsontable/delType"
    import ownDialog from "@/components/el_pack/dialog"
    import {
        mapGetters,
        mapActions
    } from "vuex";
    import {
        queryPoint,
        updatePoint,
        deletePoint
    } from "@/fetch/point_http"
    import findKeyword from "@/components/config_point/find_keyword"
    import pagination from "@/components/common/myPagination"
    export default {
        data() {
            return {
                checked: false,
                settings: {},
                isDialogShow: false,
                msg: "",
                hot: null,
                title: "",
                type: "",
                examineDialog: false,
                changeFlag: true,
                pages: {
                    size: 10,
                    sizes: [10, 30, 50, 100],
                    total: 0,
                    currentPage: 0
                },
                oldPage: {
                    currentPage: 0,
                    size: 10
                },
                localtionDialog: false,
                renderData: {},
                isLoading: false
            }
        },
        created() {
            if (!this.protocolType) {
                this.$router.push({
                    path: "/configPoint"
                })
            }
        },
        mounted() {
            this.getData()
        },
        computed: {
            ...mapGetters("project", [
                "projectId",
                "datasourceId",
                "protocolType"
            ])
        },
        methods: {
            clickTable(info, row) {
                let activeCell = this.hot.getActiveEditor()
                this.renderData = info
                console.log(activeCell, 'activeCell')
                if (activeCell.prop == "LocationFlag") {
                    this.localtionDialog = true
                }
            },
            changeLoc(val) {
                this.renderData.LocationFlag = val
                this.changeFlag = false
            },
            //修改提示
            changeHand(changeData, source) {
                if (!!changeData) {
                    this.changeFlag = false
                }
                return false
            },
            //刷新
            reset() {
                console.log(this.changeFlag, "changeFlag")
                if (!this.changeFlag) {
                    this.$confirm("表格数据尚未保存，确定刷新吗？").then(_ => {
                        this.saveData()
                    }).catch(_ => {
                        this.pages.currentPage = this.oldPage.currentPage
                        this.pages.size = this.oldPage.size
                        this.getData()
                    })
                } else {
                    this.getData()
                }
            },
            //删除点位
            delePoint(delData) {
                let param = {
                    data: delData.map(item => {
                        return item.Id
                    }),
                    type: this.protocolType
                }
                console.log(param, 'param')
                this.$confirm('你确定要删除点位吗？').then(_ => {
                    console.log(param, 'param')
                    deletePoint(param, res => {
                        this.$message.success("删除成功")
                        this.getData()
                    })
                }).catch(_ => {
                    this.$message("取消删除")
                    this.getData()
                })
            },
            //页面发生更改
            changePage() {
                if (!this.changeFlag) {
                    //发生更改，提示是否保存
                    this.$confirm('存在数据未保存, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.getData()
                    }).catch(() => {
                        this.pages.currentPage = this.oldPage.currentPage
                        this.pages.size = this.oldPage.size
                        return false
                    });
                } else {
                    this.getData()
                }
            },
            /**
             *  @param num 代表着1类型2参数
             * 
             */
            discern(num) {
                if (num == 1) {
                    this.title = "关键内容识别——批量识别设备类型关键字"
                    this.type = "type"
                } else if (num == 2) {
                    this.title = "关键内容识别——批量识别设备参数关键字"
                    this.type = "arguments"
                }
                this.isChangeData()
                this.isDialogShow = true
                this.$nextTick(() => {
                    this.$refs.findKeyword.changeType(this.type)
                })
            },
            close() {
                this.isDialogShow = false
                this.examineDialog = false
                this.isChangeData()
            },
            localFalgChange() {
                this.localtionDialog = false
            },
            //判断是否修改，修改调用修改接口，否则调用获取最新数据
            isChangeData() {
                if (!this.changeFlag) {
                    this.saveData(false)
                } else {
                    this.getData()
                }
            },
            //点击检查按钮
            examine() {
                this.isChangeData()
                this.examineDialog = true
            },
            //保存
            saveData(falg) {
                if (!!this.hot) {
                    console.log(this.hot.getSourceData())
                    let data = this.hot.getSourceData(),
                        changeData;
                    changeData = data.map(item => {
                        return {
                            Id: item.Id,
                            DatasourceId: item.DatasourceId,
                            ProjectId: item.ProjectId,
                            Description: item.Description || null,
                            KeyEquipmentParameter: item.KeyEquipmentParameter || null,
                            Remarks: item.Remarks || null,
                            LocationFlag: item.LocationFlag || null,
                            KeyEquipmentType: item.KeyEquipmentType || null
                        }
                    })
                    updatePoint({
                        data: {
                            Content: changeData
                        },
                        type: this.protocolType
                    }, res => {
                        console.log(res)
                        if (!falg) {
                            this.$message.success("保存成功")
                        }
                        this.getData()
                    })
                } else {
                    this.$message.error("请确保存在数据")
                }
            },
            //发生更改
            changeHand(changeData, source) {
                console.log(changeData, source)
                if (!!changeData) {
                    this.changeFlag = false
                }
                return false
            },
            //获取初始数据
            getData() {
                this.isLoading = true
                let width, param, settings
                width = (document.getElementById("app").clientWidth - 50) / headerArr.length
                param = {
                    type: this.protocolType,
                    data: {
                        Filters: {
                            DatasourceId: this.datasourceId,
                        },
                        "PageNumber": this.pages.currentPage || 1,
                        "PageSize": this.pages.size,
                    }
                }
                this.oldPage = {
                    size: this.pages.size,
                    currentPage: this.pages.currentPage
                }
                queryPoint(param, res => {
                    console.log(res)
                    settings = {
                        data: res.Content,
                        colHeaders: changeHeader(headerArr),
                        columns: showTypes(headerArr),
                        colWidths: width,
                        rowHeights: 30,
                        maxRows: res.Content.length,
                        contextMenu: {
                            items: {
                                remove_row: {
                                    name: "删除点位"
                                }
                            }
                        }
                    }
                    this.pages.total = res.Total
                    this.isLoading = false
                    if (!this.pages.total) {
                        return false
                    }
                    this.$nextTick(_ => {
                        this.hot = this.$refs.handsontable.init(settings)
                    })
                    console.log(this.hot)
                })
            },
            noSaveData() {
                return this.changeFlag
            }
        },
        components: {
            handsontableComponent,
            ownDialog,
            findKeyword,
            examineMain,
            pagination,
            localtionFalg
        }
    }
</script>
<style lang="scss" scoped>
    #handsonStep2 {
        flex: 1;
        display: flex;
        flex-flow: column;
        .btns-view {
            height: 40px;
            line-height: 40px;
            margin-bottom: 10px;
            padding: 0 10px;
        }
        #handsontableSteps1 {
            flex: 1;
            overflow: hidden;
            position: relative;
            margin: 0 10px;
        }
    }
</style>