<template>
    <div class="windframconfig">
        <div class="windframBtn">
            <p class="titleSty">场站管理</p>
            <div class="periodSeach">
                <div class="exceed">
                    <span style="margin-top: 3px;">区域名称：</span>
                    <el-select v-model="regionaWh" placeholder="请选择">
                        <el-option
                        v-for="item in regionaloptions"
                        :key="item.areaNo"
                        :label="item.name"
                        :value="item.areaNo">
                        </el-option>
                    </el-select>
                </div>
                <el-button type="primary" style="margin-left: 10px;" @click="getSeachData">搜索</el-button>
                <el-button style="margin-left: 10px;" @click="reset">重置</el-button>
            </div>
            <div style="display:flex;justify-content: space-between;padding: 10px;">
                <el-row :gutter="10" class="regionalBtn">
                    <el-col :span="1.5">
                        <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        size="mini"
                        @click="handleAdd"
                        >新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="success"
                        plain
                        icon="Edit"
                        size="mini"
                        @click="handleUpdate"
                        >修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        size="mini"
                        @click="handleDelete"
                        >删除</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="warning"
                        plain
                        icon="DocumentCopy"
                        size="mini"
                        @click="handleCopy"
                        >复制</el-button>
                    </el-col>
                </el-row>
                <el-button type="primary" style="margin-right: 10px;" @click="toPeriodMang">期次管理</el-button>
            </div>
            <div class="windframTableData">
                <el-table :data="windframtableData.slice((page.currentPage-1)*page.pagesize, page.currentPage*page.pagesize)" border style="width: 100%" @row-click="rowClick">
                    <el-table-column label="操作" width="60" align="center">
                        <template #default="scope">
                            <el-radio v-model="windframradio" :label="scope.row"></el-radio>
                        </template>
                    </el-table-column>
                    <el-table-column label="所属区域">
                        <template #default="scope">
                            <span>{{scope.row.areaBelongs}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="场站名称">
                        <template #default="scope">
                            <span>{{scope.row.stationName}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="场站代码">
                        <template #default="scope">
                            <span>{{scope.row.stationCode}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="场站类型">
                        <template #default="scope">
                            <span>{{scope.row.stationType}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="场站经度">
                        <template #default="scope">
                            <span>{{scope.row.stationLongitude}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="场站纬度">
                        <template #default="scope">
                            <span>{{scope.row.stationLatitude}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="机组数量">
                        <template #default="scope">
                            <span>{{scope.row.numberUnits}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="装机容量(MW)">
                        <template #default="scope">
                            <span>{{scope.row.installedCapacity ? scope.row.installedCapacity : 0}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="所属公司">
                        <template #default="scope">
                            <span>{{scope.row.affiliatedCompany}}</span>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page.currentPage"
                    :page-size="page.pagesize"
                    layout="total, prev, pager, next, jumper"
                    :total="page.total">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="title" v-model="dialogVisible" width="600px" :close-on-click-modal="false" @close="closeDialog">
            <div class="windframFrom">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="区域名称" prop="region">
                        <el-select v-model="ruleForm.region" placeholder="请选择区域">
                            <el-option
                            v-for="item in regionaloptions"
                            :key="item.areaNo"
                            :label="item.name"
                            :value="item.areaNo">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="场站名称" prop="stationName">
                        <el-input v-model="ruleForm.stationName" placeholder="请输入场站名称"></el-input>
                    </el-form-item>
                    <el-form-item label="场站类型" prop="stationType">
                        <el-select v-model="ruleForm.stationType" placeholder="请选择场站类型">
                            <el-option
                            v-for="item in energyrtuType"
                            :key="item.itemVal"
                            :label="item.itemName"
                            :value="item.itemVal">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="场站代码" prop="stationCode">
                        <el-input v-model="ruleForm.stationCode" placeholder="请输入场站代码"></el-input>
                    </el-form-item>
                    <el-form-item label="场站经度" prop="stationLongitude">
                        <el-input type="number" 
                        v-model="ruleForm.stationLongitude" 
                        placeholder="请输入场站经度" 
                        @keyup.enter.native="ruleForm.stationLongitude = $utils.oninput(ruleForm.stationLongitude, 3, 'float')"></el-input>
                    </el-form-item>
                    <el-form-item label="场站纬度" prop="stationLatitude">
                        <el-input type="number" 
                        v-model="ruleForm.stationLatitude" 
                        placeholder="请输入场站纬度" 
                        @keyup.native="ruleForm.stationLatitude = $utils.oninput(ruleForm.stationLatitude, 3, 'float')"></el-input>
                    </el-form-item>
                    <el-form-item label="所属公司" prop="affiliatedCompany">
                        <el-input v-model="ruleForm.affiliatedCompany" placeholder="请输入所属公司"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="resetForm" v-if="!single">重 置</el-button>
                    <el-button @click="dialogVisible = false" v-else>取 消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import {apiGetgetStationsList, apiGetaddStations, apiGetupdateStations, apiGetdeleteStations, apiGetgetEnumerateMenu, apiGetgetAreaAll} from '../../../api/api'
export default {
    data() {
        return {
            title:'',
            windframtableData:[],
            windframradio: {},
            single: false,
            multiple: false,
            isCopy: false,
            dialogVisible:false,
            regionaWh: '',
            regionaloptions:[],
            energyrtuType: [],
            page:{
                pagesize: 10,
                currentPage: 1,
                total: 0
            },
            ruleForm: {
                region: '',
                stationName: '',
                stationType: '',
                stationCode: '',
                stationLongitude: '',
                stationLatitude: '',
                affiliatedCompany: ''
            },
            rules: {
                region: [
                    { required: true, message: '请选择风区', trigger: 'change' }
                ],
                stationName: [
                    { required: true, message: '请输入场站名称', trigger: 'blur' }
                ],
                stationType: [
                    { required: true, message: '请输入场站类型', trigger: 'blur' }
                ],
                stationCode: [
                    { required: true, message: '请输入场站代码', trigger: 'blur' }
                ],
                stationLongitude: [
                    { required: true, message: '请输入场站经度', trigger: 'blur' }
                ],
                stationLatitude: [
                    { required: true, message: '请输入场站纬度', trigger: 'blur' }
                ],
                affiliatedCompany: [
                    { required: true, message: '请输入所属公司', trigger: 'blur' }
                ],
            }
        }
    },
    mounted() {
        this.getWindplantList()
        this.getRegionalData()
        this.getEnumerateMenu(115)
    },
    methods:{
        getSeachData() {
            this.page.currentPage = 1
            this.getWindplantList('seach')
        },
        reset() {
            this.regionaWh = ''
            this.page = {
                pagesize: 10,
                currentPage: 1,
                total: 0
            }
            this.getWindplantList()
        },
        toPeriodMang() {
            this.$emit('showPeriodFn', true)
        },
        handleSizeChange(val){
            this.page.pagesize = val
            // this.getWindplantList()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            // this.getWindplantList()
        },
        closeDialog() {
            this.$refs['ruleForm'].clearValidate()
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let that = this
                    if (!this.single) {
                        that.addOrUpdatewindframDate()
                    } else {
                        that.addOrUpdatewindframDate()
                    }
                }
            });
        },
        // 新增/修改风电场配置
        addOrUpdatewindframDate() {
            let that = this
            let params = {
                areaNo: that.ruleForm.region,
                stationCode: that.ruleForm.stationCode,
                stationName: that.ruleForm.stationName,
                stationType: that.ruleForm.stationType.toString(),
                stationLongitude: Number(that.ruleForm.stationLongitude),
                stationLatitude: Number(that.ruleForm.stationLatitude),
                affiliatedCompany: that.ruleForm.affiliatedCompany
            }
            if (!that.single) {
                apiGetaddStations(params).then(datas =>{
                    that.changeshowData(datas)
                })
            } else {
                params.no = that.ruleForm.windframnumber * 1
                apiGetupdateStations(params).then(datas =>{
                    that.changeshowData(datas)
                })
            }
            
        },
        changeshowData(datas) {
            if (datas.msg === '操作成功') {
                this.dialogVisible = false
                if (!this.single) {
                    this.$message({
                        message: '场站信息新增成功',
                        type: 'success'
                    });
                } else {
                    this.$message({
                        message: '场站信息修改成功',
                        type: 'success'
                    });
                }
                this.single = false
                this.getWindplantList('seach')
            }
        },
        resetForm() {
            this.ruleForm = {
                region: '',
                stationName: '',
                stationType: '',
                stationCode: '',
                stationLongitude: '',
                stationLatitude: '',
                affiliatedCompany: ''
            }
        },
        // 新增区域信息
        handleAdd() {
            this.dialogVisible = true
            this.single = false
            this.title = '新增场站信息'
            // this.ruleForm.region = this.regionaloptions[0].areaNo ? this.regionaloptions[0].areaNo : ''
            this.ruleForm = {
                region: '',
                stationName: '',
                stationType: '',
                stationCode: '',
                stationLongitude: '',
                stationLatitude: '',
                affiliatedCompany: ''
            }
            this.$nextTick(() =>{
                this.resetForm()
                this.$refs['ruleForm'].clearValidate()
            })
        },
        // 获取风区数据
        getRegionalData() {
            let that = this
            apiGetgetAreaAll().then(datas =>{
                if (datas && datas.data && datas.data.list) {
                    that.regionaloptions = datas.data.list
                }
            })
        },
        // 获取场站类型数据
        getEnumerateMenu(num) {
            let that = this
            let params = {
                menuNo: num
            }
            apiGetgetEnumerateMenu(params).then(datas =>{
                if (datas && datas.data && datas.data.list) {
                    that.energyrtuType = datas.data.list
                }
            })
        },
        // 复制区域信息
        handleCopy() {
            this.dialogVisible = true
            this.title = '复制场站信息'
            this.single = false
            this.isCopy = true
            this.ruleForm = {
                region: this.windframradio.areaNo,
                stationName: this.windframradio.stationName,
                stationType: Number(this.windframradio.itemVal),
                stationCode: this.windframradio.stationCode,
                stationLongitude: this.windframradio.stationLongitude,
                stationLatitude: this.windframradio.stationLatitude,
                affiliatedCompany: this.windframradio.affiliatedCompany
            }
        },
        // 修改区域信息
        handleUpdate() {
            this.dialogVisible = true
            this.single = true
            this.title = '修改场站信息'
            this.ruleForm = {
                region: this.windframradio.areaNo,
                stationName: this.windframradio.stationName,
                stationType: Number(this.windframradio.itemVal),
                stationCode: this.windframradio.stationCode,
                stationLongitude: this.windframradio.stationLongitude,
                stationLatitude: this.windframradio.stationLatitude,
                affiliatedCompany: this.windframradio.affiliatedCompany
            }
        },
        rowClick(row, column, event) {
            this.windframradio = row
        },
        // 删除区域信息
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let params = {
                    no: that.windframradio.no,
                    stationType: that.windframradio.stationType
                }
                apiGetdeleteStations(params).then(datas =>{
                    if (datas.msg === '操作成功') {
                        that.getWindplantList()
                    }
                })
                if (that.windframtableData.length>0) {
                    that.windframradio = that.windframtableData[0]
                }
                that.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            })
        },
        //获取风电场列表数据
        getWindplantList(type) {
            let that = this
            let params = {}
            if (type) {
                params = {
                    areaNo: that.regionaWh
                }
            }
            apiGetgetStationsList(params).then(datas =>{
                if (datas && datas.rows) {
                    that.windframtableData = datas.rows
                    that.windframradio = datas.rows[0]
                    that.page.total = datas.total
                }
            })
        }
    }
}
</script>

<style lang="less">
.windframconfig{
    .windframBtn{
        background: #fff;
        padding: 10px 20px 0 20px;
        .titleSty{
            border-left: 4px solid #2996da;
            padding-left: 5px;
            font-size: 14px;
            margin-left:20px;
        }
        .periodSeach{
            display: flex;
            margin:20px;
                .exceed{
                    display: flex;
                    margin-right:20px;
                    .el-select{
                        width: 200px;
                        margin-right:10px;
                        .el-input__inner{
                            height:30px;
                        }
                        .el-input__suffix{
                            .el-select__caret{
                                line-height:30px;
                            }
                        }
                    }
                }
        }
        span{
            font-size:14px;
        }
        .regionalBtn{
            padding-left: 10px;
        }

        .exceed{
            display: flex;
            margin-right:20px;
            .exTime{
                width: 240px;
                .el-select__tags{
                    left: 1px;
                    background:#fff;
                }
            }
        }
        .el-select{
            width: 160px;
            margin-right:10px;
            .el-input__inner{
                height:30px;
            }
            .el-input__suffix{
                .el-select__caret{
                    line-height:30px;
                }
            }
        }
        .el-button{
            height: 30px;
            // width:100px;
            padding: 0 30px ;
            // padding-top: 8px;
            span{
                margin:0;
            }
        }
    }
    .windframTableData{
        padding: 10px 20px;
        .el-table{
        .el-table__body-wrapper{
            .el-table__empty-block{
            height: 550px !important;
            }
        }
        .el-input__inner{
            height: 30px !important;
        }
        .el-radio__label{
            display: none;
        }
        }
        .el-pagination{
            margin-top: 20px;
            text-align: end;
            position: relative;
        }
    }
    .el-dialog__wrapper{
        .el-dialog{
            .el-dialog__body{
                padding: 30px 60px 30px 20px !important;
            }
        }
    }
    .el-overlay{
        .el-dialog{
            .windframFrom{
                .el-form{
                    .el-select {
                        width: 100%;
                    }
                    .el-input {
                        width: calc(100% - 30px);
                    }
                }
            }
        }
    }
}
</style>