<template>
    <div class="windFarmPeriod" v-loading="loadingImport">
        <div class="windFarmPeriodBtn">
            <p class="titleSty">期次管理</p>
            <div class="periodSeach">
                <div class="exceed">
                    <span style="margin-top: 3px;">场站名称：</span>
                    <el-select v-model="windTurbines" placeholder="请选择" @change="changeWind">
                        <el-option
                        v-for="item in windFramoptions"
                        :key="item.no"
                        :label="item.stationName"
                        :value="item.no">
                        </el-option>
                    </el-select>
                </div>
                <div class="exceed">
                    <span style="margin-top: 3px;">期次名称：</span>
                    <el-select v-model="windProject" placeholder="请选择" :disabled="!projectShow">
                        <el-option
                        v-for="item in windProjectoptions"
                        :key="item.projectNo"
                        :label="item.projectName"
                        :value="item.projectNo">
                        </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="PeriodBtn">
                    <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="toCollectionMang">集电线管理</el-button>
            </div>
            <div class="windFarmPeriodTableData">
                <el-table :data="periodtableData" border style="width: 100%" @row-click="rowClick">
                    <el-table-column label="操作" width="60" align="center">
                        <template #default="scope">
                            <el-radio v-model="periodradio" :label="scope.row"></el-radio>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column type="selection" label="操作" align="center"></el-table-column> -->
                    <el-table-column label="风场名称">
                        <template #default="scope">
                            <span>{{scope.row.windplantName || scope.row.pvStationName}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="期次编号">
                        <template #default="scope">
                            <span>{{scope.row.projectNo}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="期次名称">
                        <template #default="scope">
                            <span>{{scope.row.projectName}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="有功功率遥测号">
                        <template #default="scope">
                            <span>{{scope.row.analogNo}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="装机容量(KW)">
                        <template #default="scope">
                            <span>{{scope.row.projectCapacity ? scope.row.projectCapacity : 0}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="统计时间">
                        <template #default="scope">
                            <span>{{scope.row.statSpeedTime}}</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="periodFrom">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" :validate-on-rule-change="false">
                    <el-form-item label="风场名称" prop="wpNo">
                        <el-select v-model="ruleForm.wpNo" placeholder="请选择风场">
                            <el-option
                            v-for="item in windFramoptions"
                            :key="item.no"
                            :label="item.stationName"
                            :value="item.no">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="期次编号" prop="periodNum">
                        <el-input type="number" 
                        v-model="ruleForm.periodNum" 
                        :disabled="single"
                        placeholder="请输入期次编号" 
                        @keyup.native="ruleForm.periodNum = oninput(ruleForm.periodNum, 10, 'number', 'zero-')"></el-input>
                    </el-form-item>
                    <el-form-item label="期次名称" prop="periodname">
                        <el-input v-model="ruleForm.periodname" placeholder="请输入期次名称"></el-input>
                    </el-form-item>
                    <el-form-item label="功率遥测号" prop="analogNo">
                        <el-input type="number" 
                        v-model="ruleForm.analogNo" 
                        placeholder="请输入功率遥测号" 
                        @keyup.native="ruleForm.analogNo = $utils.oninput(ruleForm.analogNo, 6, 'number')"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="装机容量" prop="projectCapacity">
                        <el-input type="number" 
                        v-model="ruleForm.projectCapacity" 
                        placeholder="请输入装机容量" 
                        @keyup.native="ruleForm.projectCapacity = $utils.oninput(ruleForm.projectCapacity, 1, 'float')"></el-input>
                    </el-form-item> -->
                    <el-form-item label="统计时间" prop="statSpeedTime">
                        <el-date-picker
                            v-model="ruleForm.statSpeedTime"
                            type="datetime"
                            placeholder="选择日期时间">
                        </el-date-picker>
                    </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>
        <import-dailog ref="importPage" @successImport="successImport" @importLoading="importLoading"></import-dailog>
    </div>
</template>

<script>
import importDailog from '../importPage/importDailog.vue'
import {apiGetWindplantprojectList, apiGetaddWindplantproject, apiGetupdateWindplantproject, 
apiGetdeleteWindplantproject, apiGetWindplantprojectListByWindplantNo, apiGetStation, 
apiGetcheckWindplantprojectAnalogNoUnique, apiGetcheckProjectNoUnique} from '../../../api/api'
export default {
    components: { importDailog },
    data() {
        return {
            changeDateSelect: [],
            loadingImport: false,
            title:'',
            periodtableData:[],
            windTurbines: '',
            windFramoptions:[], //风电场数据
            windFramoptionsAll: [],
            windProject: '',
            windProjectoptions: [], //搜索中期次数据
            projectShow: true,
            periodradio: {},
            single: false,
            multiple: false,
            dialogVisible:false,
            regionaloptions:[],
            page:{
                pagesize: 13,
                currentPage: 1,
                total: 0
            },
            ruleForm: {
                wpNo: '',
                periodname:'',
                periodNum: '',
                analogNo: '',
                projectCapacity: '',
                statSpeedTime:''
            },
            rules: {
                wpNo: [
                    { required: true, message: '请选择风场', trigger: 'change' }
                ],
                periodname: [
                    { required: true, message: '请输入期次名称', trigger: 'blur' }
                ],
                periodNum: [
                    { required: true, message: '请输入期次编号', trigger: 'blur' }
                ],
                analogNo: [
                    { required: true, message: '请输入有功功率遥测号', trigger: 'blur' }
                ],
                projectCapacity: [
                    { required: true, message: '请输入装机容量', trigger: 'blur' }
                ],
                statSpeedTime: [
                    { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                ]
            }
        }
    },
    mounted() {
        this.getWindplantprojectList()
        this.getWindFramData()
    },
    methods:{
        toCollectionMang() {
            this.$emit('showCollectionFn', true)
        },
        oninput(str, limit) {
            let strZ = ''
            if (str.substr(0, 1) === '.' || str.substr(0, 1) === 'e') {
                strZ = ''
            } else if (str.substr(0, 1) === '0') {
                return 0
            } else if(str.substr(0, 1) === '-') {
                strZ = '-'
            } else if(str.substr(0,limit).indexOf('-') != -1) {
                strZ = ''
            }
            str = str.replace(/[^\d^]+/g, '')
            str = str.substring(0, limit)
            return str === 0 ? 0 : strZ + str
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getWindplantprojectList('seach')
        },
        successImport(val) {
            this.loadingImport = false
            this.getWindplantprojectList()
        },
        importLoading(val) {
            this.loadingImport = true
        },
        reset() {
            this.windTurbines = ''
            this.windProject = ''
            this.projectShow = false
            this.page = {
                pagesize: 13,
                currentPage: 1,
                total: 0
            }
            this.getWindplantprojectList()
        },
        handleSizeChange(val){
            this.page.pagesize = val
            if (this.windTurbines != '') {
                this.getWindplantprojectList('seach')
            } else {
                this.getWindplantprojectList()
            }
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            if (this.windTurbines != '') {
                this.getWindplantprojectList('seach')
            } else {
                this.getWindplantprojectList()
            }
        },
        rowClick(row, column, event) {
            this.periodradio = row
        },
        closeDialog() {
            this.$refs['ruleForm'].clearValidate()
        },
        submitForm(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    if (!that.single) {
                        this.checkProjectNo(this.ruleForm.periodNum)
                    } else {
                        if (that.periodradio.analogNo !== that.ruleForm.analogNo*1) {
                            that.checkanalogNo(that.ruleForm.analogNo)
                        } else {
                            that.addWindProjectMsg()
                        }
                    }
                }
            });
        },
        // 校验期次编号是否唯一
        checkProjectNo(num) {
            let that = this
            let params = {
                projectNo: num
            }
            apiGetcheckProjectNoUnique(params).then(datas =>{
                if (datas.msg === 'true') {
                    that.checkanalogNo(that.ruleForm.analogNo)
                } else {
                    this.$message({
                        message: '期次编号已存在，请重新填写',
                        type: 'warning'
                    });
                }
            })
        },
        // 校验遥测号是否唯一---------待定
        checkanalogNo(num) {
            let that = this
            let params = {
                analogNo: num
            }
            apiGetcheckWindplantprojectAnalogNoUnique(params).then(datas =>{
                if (datas.msg === 'true') {
                    that.addWindProjectMsg()
                } else {
                    this.$message({
                        message: '有功功率遥测号已存在，请重新填写',
                        type: 'warning'
                    });
                }
            })
        },
        // 添加风电场期次信息
        addWindProjectMsg() {
            let that = this
            let params = {
                analogNo: that.ruleForm.analogNo,
                projectNo: that.ruleForm.periodNum,
                // projectCapacity: that.ruleForm.projectCapacity,
                projectName: that.ruleForm.periodname,
                statSpeedTime: that.getTime(that.ruleForm.statSpeedTime),
                wpNo: that.ruleForm.wpNo
            }
            if (!that.single) {
                apiGetaddWindplantproject(params).then(datas =>{
                    that.changeshowData(datas)
                })
            } else {
                apiGetupdateWindplantproject(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.getWindplantprojectList('seach')
            }
        },
        //转换时间
        getTime(date){
            var y = date.getFullYear();  
            var m = date.getMonth() + 1;  
            m = m < 10 ? ('0' + m) : m;  
            var d = date.getDate();  
            d = d < 10 ? ('0' + d) : d;  
            var h = date.getHours();  
            h=h < 10 ? ('0' + h) : h;  
            var minute = date.getMinutes();  
            minute = minute < 10 ? ('0' + minute) : minute;  
            var second=date.getSeconds();  
            second=second < 10 ? ('0' + second) : second;  
            return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
        },
        resetForm() {
            this.ruleForm = {
                wpNo: '',
                periodname:'',
                analogNo: '',
                projectCapacity: '',
                statSpeedTime:''
            }
        },
        // 获取风电场数据
        getWindFramData() {
            let that = this
            apiGetStation().then(datas =>{
                if (datas && datas.data && datas.data.list) {
                    that.windFramoptions = datas.data.list
                    that.windTurbines = datas.data.list[0].no
                    that.getWindplantprojectList('seach')
                    that.getWindplantprojectListByWindplantNo(that.windTurbines)
                }
            })
        },
        // 选择搜索中的风电场
        changeWind(val) {
            this.projectShow = true
            this.windProject = ''
            this.getWindplantprojectListByWindplantNo(val)
        },
        // 根据风场获取期次数据
        getWindplantprojectListByWindplantNo(val) {
            let that = this
            let params = {
                stationNo: val
            }
            apiGetWindplantprojectListByWindplantNo(params).then(datas =>{
                if (datas && datas.data.list.length > 0) {
                    that.windProjectoptions = datas.data.list
                }
            })
        },
        handleAdd() {
            this.dialogVisible = true
            this.single = false
            this.title = '新增风电场期次信息'
            this.$nextTick(() =>{
                this.resetForm()
                this.$refs['ruleForm'].clearValidate()
            })
        },
        handleUpdate() {
            this.dialogVisible = true
            this.single = true
            this.title = '修改风电场期次信息'
            this.ruleForm = {
                wpNo: this.periodradio.wpNo,
                periodname:this.periodradio.projectName,
                periodNum: this.periodradio.projectNo,
                analogNo: this.periodradio.analogNo,
                projectCapacity: this.periodradio.projectCapacity,
                statSpeedTime:new Date(this.periodradio.statSpeedTime)
            }
        },
        // 复制区域信息
        handleCopy() {
            this.dialogVisible = true
            this.title = '复制风电场期次信息'
            this.single = false
            this.ruleForm = {
                wpNo: this.periodradio.wpNo,
                periodname:this.periodradio.projectName,
                periodNum: this.periodradio.projectNo,
                analogNo: this.periodradio.analogNo,
                projectCapacity: this.periodradio.projectCapacity,
                statSpeedTime: new Date(this.periodradio.statSpeedTime)
            }
        },
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let params = {
                    wpNo: this.periodradio.wpNo,
                    projectNo: this.periodradio.projectNo
                }
                apiGetdeleteWindplantproject(params).then(datas =>{
                    if (datas.msg === '操作成功') {
                        that.getWindplantprojectList()
                    }
                })
                if (that.periodtableData.length>0) {
                    that.periodradio = that.periodtableData[0]
                }
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            })
        },
        getWindplantprojectList(type) {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize
            }
            if (type === 'seach') {
                params.wpNo = that.windTurbines
                params.projectNo = that.windProject
            }
            apiGetWindplantprojectList(params).then(datas =>{
                if (datas && datas.rows) {
                    that.periodtableData = datas.rows
                    that.periodradio = datas.rows[0]
                    that.page.total = datas.total
                }
            })
        }
    }
}
</script>

<style lang="less">
.windFarmPeriod{
    .windFarmPeriodBtn{
        background: #fff;
        padding: 10px 20px 0 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;
                            }
                        }
                    }
                }
        }
        .titleSty{
            border-left: 4px solid #2996da;
            padding-left: 5px;
            font-size: 14px;
            margin-left:20px;
        }
        span{
            font-size:14px;
        }
        .PeriodBtn{
            padding-left: 10px;
        }
        .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;
            }
        }
    }
    .windFarmPeriodTableData{
        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;
            }
        }
        .periodFrom{
            .el-select, .el-date-editor{
                width: 100%;
            }
        }
    }
    .el-overlay{
        .el-dialog{
            .periodFrom{
                .el-form{
                    .el-select {
                        width: 100%;
                    }
                    .el-input {
                        width: calc(100% - 30px);
                    }
                }
            }
        }
    }
}
</style>