<style lang="scss">
@import '@/styles/index.scss'; //导入样式

.project {
    width: 100%;
    margin: auto;

    &__item {
        width: 100%;
        display: flex;

        &-left {
            flex: 1;
        }

        &-right {
            flex: none;
            //width: 200px;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding-left: 10px;
            text-align: right;
        }
    }
}
</style>

<template>
    <div class="project">
        <app-search>
            <el-form :inline="true" :model="tableData.listData.search" :size="controlSize">
                <el-form-item label="项目名称">
                    <el-input v-model.trim="tableData.listData.search.name" auto-complete="off" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :disabled="tableData.listData.loading == true"
                        @click="getProjectPageList">查询</el-button>
                    <el-button type="success" @click="openAddDlg">新增</el-button>
                </el-form-item>
            </el-form>
        </app-search>
        <app-table pagination paginationAlign="right" table :tableData="tableData.listData"
            :handlePageSizeChange="handlePageSizeChange" :handlePageNumChange="handlePageNumChange">
            <template slot="table-column">
                <el-table-column label="操作" header-align="center" width="100" testalign="center">
                    <template slot-scope="scope">
                        <el-dropdown>
                            <span class="el-dropdown-link">
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </span>
                            <el-dropdown-menu>
                                <el-dropdown-item @click.native="openUpdateDlg(scope.row)">修改</el-dropdown-item>
                                <el-dropdown-item @click.native="deleteProject(scope.row)">删除</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </template>
        </app-table>

        <el-dialog width="50%" v-drag-dialog :title="dialogData.editData.title" center
            :visible.sync="dialogData.editData.show" :modal-append-to-body="false" custom-class="editDataDialog"
            :close-on-click-modal="false" :close-on-press-escape="false">
            <el-form :model="dialogData.editData.data" :rules="dialogData.editData.rules" ref="editDataForm"
                label-width="85px" :size="controlSize">
                <el-form-item label="项目标识" prop="domain_name">
                    <div class="project__item">
                        <div class="project__item-left">
                            <el-input v-model="dialogData.editData.data.domain_name" auto-complete="off"
                                placeholder="移动端项目标识符"></el-input>
                        </div>
                        <div class="project__item-right">移动端访问系统时使用</div>
                    </div>
                </el-form-item>
                <el-form-item label="项目编号" prop="project_id" v-if="dialogData.editData.operation == 'update'">
                    <div class="project__item">
                        <div class="project__item-left">
                            <el-input v-model="calProjectId" readonly auto-complete="off"></el-input>
                        </div>
                        <div class="project__item-right">新增基站时使用</div>
                    </div>
                </el-form-item>
                <el-form-item label="项目名称" prop="name">
                    <el-input v-model="dialogData.editData.data.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="建筑数量">
                    <div class="project__item">
                        <div class="project__item-left">
                            <el-input-number :precision="0" :step="1" :min="1"
                                v-model="dialogData.editData.data.build_num" auto-complete="off"
                                style="width:100%"></el-input-number>
                        </div>
                        <div class="project__item-right">单位：幢栋</div>
                    </div>
                </el-form-item>
                <el-form-item label="建筑面积">
                    <div class="project__item">
                        <div class="project__item-left">
                            <el-input-number :precision="0" :step="1" :min="1"
                                v-model="dialogData.editData.data.build_area" auto-complete="off"
                                style="width:100%"></el-input-number>
                        </div>
                        <div class="project__item-right">单位：平米</div>
                    </div>
                </el-form-item>
                <el-form-item label="项目描述">
                    <el-input type="textarea" rows="6" v-model="dialogData.editData.data.desc"
                        auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="同步基站">
                    <div class="project__item">
                        <div class="project__item-left">
                            <el-input-number :precision="0" :step="1" :min="1"
                                v-model="dialogData.editData.data.sync_bts_num" auto-complete="off"
                                style="width:100%"></el-input-number>
                        </div>
                        <div class="project__item-right">单位：台</div>
                    </div>
                </el-form-item>
                <el-form-item label="定位基站">
                    <div class="project__item">
                        <div class="project__item-left">
                            <el-input-number :precision="0" :step="1" :min="1"
                                v-model="dialogData.editData.data.lct_bts_num" auto-complete="off"
                                style="width:100%"></el-input-number>
                        </div>
                        <div class="project__item-right">单位：台</div>
                    </div>
                </el-form-item>
            </el-form>
            <div style="padding-top: 10px;text-align:right">
                <el-button type="success" @click="onSubmit">确定</el-button>
                <el-button type="warning" @click="hClose">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>


<script>
import { addProject, updateProject, deleteProject, getProjectPageList } from "@/api/project"

export default {
    name: 'project',
    data: function () {
        return {
            pageData: {
                id: 0,
                name: '',
                pid: ''
            },
            tableData: {
                listData: {
                    loading: false,
                    disabled: false,
                    totalCount: 0,
                    pageNum: 1,
                    pageSize: this.$store.getters.pageSize,
                    search: {
                        name: '',
                    },
                    columns: [
                        { key: 'project_id', name: '项目编号', width: 100 },
                        { key: 'name', name: '项目名称', width: 150 },
                        { key: 'domain_name', name: '移动端标识符', width: 150 },
                        { key: 'build_num', name: '建筑数量(幢)', width: 120 },
                        { key: 'build_area', name: '建筑面积(平方米)', width: 150 },
                        { key: 'sync_bts_num', name: '同步基站数', width: 120 },
                        { key: 'lct_bts_num', name: '定位基站数', width: 120 },
                        { key: 'desc', name: '项目描述' },
                    ],
                    formatter: {
                        'project_id': this.formatPorjectId
                    },
                    items: [],
                }
            },
            dialogData: {
                editData: {
                    operation: 'add',
                    title: '新增项目资料',
                    show: false,
                    data: {
                        id: 0,
                        domain_name: '',
                        project_id: 0,
                        name: '',
                        build_num: 1,
                        build_area: 1,
                        desc: '',
                        sync_bts_num: 1,
                        lct_bts_num: 1,
                    },
                    rules: {
                        domain_name: [
                            { required: true, message: '请输入项目标识符', trigger: 'blur' },
                            { min: 2, max: 16, message: '访问域名字符长度2-16', trigger: 'blur' }
                        ],
                        name: [
                            { required: true, message: '请输入项目名称', trigger: 'blur' },
                            { min: 2, max: 32, message: '项目名称字符长度2-32', trigger: 'blur' }
                        ],

                    }
                }
            }
        }
    },
    watch: {
        updateTime(val, old) { //监听到改变后，重新初始化组件
            this.init()
        }
    },
    computed: {
        controlSize() {
            return this.$store.getters.controlSize
        },
        calProjectId() {
            let hex = this.dialogData.editData.data.project_id.toString(16).toUpperCase()
            return hex.padStart(8, '0');//补齐8位
        }
    },
    beforeMount() {
    },
    mounted() {
        this.$nextTick(function () {
            this.init()
        })
    },
    methods: {
        init() {
            this.getProjectPageList();
        },
        handlePageSizeChange(val) { // 每页记录条数改变的事件
            //保存修改的PageSize
            this.$store.commit('app/setPageSize', val)

            //如果（每页记录条数改变后重新计算的总页数>=当前页），表示每页记录条数改变后，当前页不会改变，此处需要刷新table数据
            //如果（当前页*每页记录条数<=总记录条数），表示每页记录条数改变后，当前页不会改变，此处需要刷新table数据
            //如果（当前页*每页记录条数>总记录条数），表示每页记录条数改变后，当前页会重新计算并改变，handlePageNumChange会被调用，此处不需要刷新table数据
            var totalCount = this.tableData.listData.totalCount
            var pageNum = this.tableData.listData.pageNum
            var pageSize = this.tableData.listData.pageSize
            var pageCount = totalCount % pageSize === 0 ? totalCount / pageSize : Math.floor((totalCount / pageSize) + 1)
            if ((pageCount >= pageNum) || (pageNum * pageSize <= totalCount)) {
                this.getProjectPageList()
            }
        },
        handlePageNumChange(val) { // 当前页改变的事件，需要在此事件中刷新table数据
            this.tableData.listData.pageNum = val
            this.getProjectPageList()
        },
        formatPorjectId(row, col, value) {
            let hex = value.toString(16).toUpperCase()
            return hex.padStart(8, '0');//补齐8位
        },
        openAddDlg() {
            this.dialogData.editData.data = {}
            this.dialogData.editData.data.build_num = 1
            this.dialogData.editData.data.build_area = 1
            this.dialogData.editData.data.sync_bts_num = 1
            this.dialogData.editData.data.lct_bts_num = 1
            this.dialogData.editData.operation = 'add'
            this.dialogData.editData.title = '新增项目资料'
            this.dialogData.editData.show = true
        },
        openUpdateDlg(item) {
            this.dialogData.editData.data = item

            this.dialogData.editData.operation = 'update'
            this.dialogData.editData.title = '修改项目资料'
            this.dialogData.editData.show = true
        },
        getProjectPageList() {
            let pageNum = this.tableData.listData.pageNum
            let pageSize = this.tableData.listData.pageSize
            let query = []

            if (this.tableData.listData.search.name.length > 0) {
                const itemName = "%" + this.tableData.listData.search.name + "%"
                let item = { key: "name", data: itemName, opt: "like" }
                query.push(item)
            }

            let param = { page_num: pageNum, page_size: pageSize, query: query, order_by: "id desc" }

            //查询数据
            this.tableData.listData.loading = true
            getProjectPageList(param).then((res) => {
                this.tableData.listData.totalCount = res.data.total_count
                this.tableData.listData.items = res.data.items
                this.tableData.listData.loading = false
            }).catch((err) => {
                this.tableData.listData.loading = false
            })
        },
        onSubmit() { //对话框确定按钮响应函数
            this.$refs.editDataForm.validate((valid) => {
                if (valid) {
                    if ('add' == this.dialogData.editData.operation) {
                        this.addProject()
                    } else {
                        this.updateProject()
                    }
                }
            })
        },
        addProject() {
            let paramData = {}
            paramData.domain_name = this.dialogData.editData.data.domain_name
            paramData.name = this.dialogData.editData.data.name
            paramData.build_num = parseInt(this.dialogData.editData.data.build_num)
            paramData.build_area = parseInt(this.dialogData.editData.data.build_area)
            paramData.desc = this.dialogData.editData.data.desc
            paramData.name = this.dialogData.editData.data.name
            paramData.sync_bts_num = parseInt(this.dialogData.editData.data.sync_bts_num)
            paramData.lct_bts_num = parseInt(this.dialogData.editData.data.lct_bts_num)
            addProject(paramData).then((res) => {
                this.dialogData.editData.data.id = res.data
                this.$message({
                    message: '新增项目成功',
                    type: 'success',
                    duration: 2 * 1000
                })
                this.getProjectPageList()
            }).catch((err) => {
                this.$message({
                    message: err,
                    type: 'error',
                    duration: 2 * 1000
                })
            })
        },
        updateProject() {
            let paramData = {}
            paramData.id = parseInt(this.dialogData.editData.data.id)
            paramData.domain_name = this.dialogData.editData.data.domain_name
            paramData.name = this.dialogData.editData.data.name
            paramData.build_num = parseInt(this.dialogData.editData.data.build_num)
            paramData.build_area = parseInt(this.dialogData.editData.data.build_area)
            paramData.desc = this.dialogData.editData.data.desc
            paramData.sync_bts_num = parseInt(this.dialogData.editData.data.sync_bts_num)
            paramData.lct_bts_num = parseInt(this.dialogData.editData.data.lct_bts_num)

            updateProject(paramData).then((res) => {
                this.$message({
                    message: '修改项目成功',
                    type: 'success',
                    duration: 2 * 1000
                })
                this.getProjectPageList()
                this.dialogData.editData.show = false
            }).catch((err) => {
                this.$message({
                    message: err,
                    type: 'error',
                    duration: 2 * 1000
                })
            })
        },
        async deleteProject(item) {
            let res = await this.$confirm("删除项目的同时将删除该项目的所有基站，确定要删除吗", "提示", { type: "error" })
            if (res != 'confirm') {
                return
            }

            res = await this.$confirm("删除项目的同时将删除该项目的所有基站，确定要删除吗", "再次提示", { type: "error" })
            if (res != 'confirm') {
                return
            }

            let paramData = {}
            paramData["id"] = parseInt(item.id)
            deleteProject(paramData).then(() => {
                this.$message({
                    message: '删除项目成功',
                    type: 'success',
                    duration: 2 * 1000
                })
                this.getProjectPageList()
            }).catch((err) => {
                this.$message({
                    message: err,
                    type: 'error',
                    duration: 2 * 1000
                })
            })
        },
        hClose() {//关闭对话框
            this.dialogData.editData.show = false
        }
    }
}

</script>
@/api/project