<template>
    <!-- 基础管理-项目管理 -->
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="项目名称" prop="proName">
                <el-input
                    v-model="queryParams.proName"
                    placeholder="请输入项目名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <!-- <el-form-item label="负责人" prop="chargePerson">
                <el-input
                    v-model="queryParams.chargePerson"
                    placeholder="请输入负责人"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['basic:project:add']"
                    >新增</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['basic:project:export']"
                    >导出</el-button
                >
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="proList" @selection-change="handleSelectionChange" border>
            <el-table-column label="序号" align="center" type="index" />
            <el-table-column label="项目名称" align="center" prop="proName" />
            <el-table-column label="层级配置" align="center" prop="level" />
            <el-table-column label="负责人" align="center" prop="chargePerson" />
            <el-table-column label="备注" align="center" prop="remark" width="180" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['basic:project:edit']"
                        >编辑</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['basic:project:remove']"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改岗位对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="项目名称" prop="proName">
                    <el-input v-model="form.proName" placeholder="请输入项目名称" />
                </el-form-item>
                <el-form-item label="层级配置" prop="level">
                    <el-select v-model="form.level" placeholder="请选择层级配置" style="width: 100%">
                        <el-option
                            :key="item.configId"
                            :value="item.configId"
                            :label="item.configName"
                            v-for="item in levelList"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 地图选点对话框 -->
        <el-dialog
            title="地图选点"
            id="mapDialog"
            :fullscreen="false"
            :visible.sync="mapVisible"
            :close-on-click-modal="false"
            style="margin-top: 8.5vh"
        >
            <div style="margin-bottom: 10px">
                <label>关键词：<input v-model="keyword" class="lineinput" style="width: 200px" size="mini" /></label>
                <label>地区：<input v-model="location2" class="lineinput" style="width: 200px" size="mini" /></label>
            </div>
            <baidu-map
                class="map"
                :center="center"
                :zoom="zoom"
                @ready="handler"
                :scroll-wheel-zoom="true"
                @click="clickEvent"
                ak="你的ak"
            >
                <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
                <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
                <bm-geolocation
                    anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
                    :showAddressBar="true"
                    :autoLocation="true"
                    @locationSuccess="getLoctionSuccess"
                ></bm-geolocation>
                <bm-view :style="{ width: '100%', height: '300px', flex: 1, marginTop: '0px' }"></bm-view>
                <bm-local-search
                    :keyword="keyword"
                    :auto-viewport="false"
                    :location="location2"
                    style="height: 150px; overflow-y: scroll; margin: 2px 0"
                ></bm-local-search>
            </baidu-map>
            <div class="demo-input-suffix" style="margin-top: 2vh">
                <el-link type="info">经度：</el-link
                ><el-input
                    class="lineinput"
                    style="width: 200px"
                    size="mini"
                    v-model.number="form.longitude"
                    :disabled="true"
                ></el-input>
                <el-link type="info"> 纬度：</el-link
                ><el-input
                    class="lineinput"
                    style="width: 200px"
                    size="mini"
                    v-model.number="form.latitude"
                    :disabled="true"
                ></el-input>
                <el-link type="info"> 位置：</el-link
                ><el-input
                    class="lineinput"
                    style="width: 200px"
                    size="mini"
                    v-model="form.location"
                    :disabled="true"
                ></el-input>
            </div>
            <div slot="footer" class="dialog-footer" style="margin-top: -30px">
                <el-button type="warning" size="small" icon="el-icon-close" @click.native="mapVisible = false"
                    >取消</el-button
                >
                <el-button type="primary" size="small" icon="el-icon-check" @click="findLocation">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject } from '@/api/basic/project'
import { getLevelListApi } from '@/api/basic/level-manage.js'
import { BaiduMap, BmNavigation, BmView, BmGeolocation, BmCityList, BmLocalSearch } from 'vue-baidu-map'

export default {
    name: 'Post',
    dicts: ['sys_normal_disable'],
    data() {
        return {
            location2: '',
            keyword: '',
            center: { lng: 111.752912, lat: 40.832246 },
            zoom: 12,
            mapVisible: false,
            iconUrl: 'http://api0.map.bdimg.com/images/marker_red_sprite.png',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 岗位表格数据
            proList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                proName: undefined,
                unit: undefined,
                chargePerson: undefined,
            },
            // 表单参数
            form: {
                longitude: '',
                latitude: '',
                location: '',
                remark: '',
                level: '',
            },
            // 表单校验
            rules: {
                proName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
                level: [{ required: true, message: '请选择层级配置', trigger: 'change' }],
            },

            levelList: [],
        }
    },
    created() {
        this.getList()
        this.getLevelList()
    },
    methods: {
        async getLevelList() {
            const res = await getLevelListApi({
                pageNum: 1,
                pageSize: 1000,
            })
            this.levelList = res.rows
        },
        /** 查询岗位列表 */
        getList() {
            this.loading = true
            listProject(this.queryParams).then((response) => {
                this.proList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                proId: undefined,
                proName: undefined,
                proType: undefined,
                unit: undefined,
                chargePerson: undefined,
                location: undefined,
                remark: undefined,
                level: undefined,
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '新增项目'
        },
        /** 地图选点操作 */
        mapAdd() {
            this.mapVisible = true
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const proId = row.proId || this.ids
            getProject(proId).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改项目'
                this.form.level = response.data.proId
            })
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.proId != undefined) {
                        updateProject(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addProject(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },

        /** 地图选点 */
        handler({ BMap, map }) {
            let _this = this // 设置一个临时变量指向vue实例，因为在百度地图回调里使用this，指向的不是vue实例；
            let geolocation = new BMap.Geolocation()
            geolocation.getCurrentPosition(
                function (r) {
                    console.log(r)
                    _this.center = { lng: r.longitude, lat: r.latitude } // 设置center属性值
                    _this.autoLocationPoint = { lng: r.longitude, lat: r.latitude } // 自定义覆盖物
                    _this.initLocation = true
                },
                { enableHighAccuracy: true },
            )
            window.map = map
        },
        //点击地图监听
        clickEvent(e) {
            map.clearOverlays()
            let Icon_0 = new BMap.Icon(
                'http://api0.map.bdimg.com/images/marker_red_sprite.png',
                new BMap.Size(64, 64),
                { anchor: new BMap.Size(18, 32), imageSize: new BMap.Size(36, 25) },
            )
            let myMarker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat), { icon: Icon_0 })
            map.addOverlay(myMarker)
            //用所定位的经纬度查找所在地省市街道等信息
            let point = new BMap.Point(e.point.lng, e.point.lat)
            let gc = new BMap.Geocoder()
            let _this = this
            gc.getLocation(point, function (rs) {
                let addComp = rs.addressComponents
                _this.form.location = rs.address
            })
            this.form.longitude = e.point.lng
            this.form.latitude = e.point.lat
        },
        //定位成功回调
        getLoctionSuccess(point, AddressComponent, marker) {
            map.clearOverlays()
            let Icon_0 = new BMap.Icon(
                'http://api0.map.bdimg.com/images/marker_red_sprite.png',
                new BMap.Size(64, 64),
                { anchor: new BMap.Size(18, 32), imageSize: new BMap.Size(36, 36) },
            )
            let myMarker = new BMap.Marker(new BMap.Point(point.point.lng, point.point.lat), { icon: Icon_0 })
            map.addOverlay(myMarker)
            this.form.longitude = point.point.lng
            this.form.latitude = point.point.lat
        },
        //返回选中点的位置
        findLocation() {
            this.mapVisible = false
            this.$emit('findlocdata', this.form)
            this.temp.location = this.keyword
            this.temp.lng = this.form.longitude
            this.temp.lat = this.form.latitude
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const proIds = row.proId || this.ids
            this.$modal
                .confirm('是否确认删除项目编号为"' + proIds + '"的数据项？')
                .then(function () {
                    return delProject(proIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'basic/project/export',
                {
                    ...this.queryParams,
                },
                `项目信息表.xlsx`,
            )
        },
    },
}
</script>
