<template>
    <div class="periodCollect" v-loading="loadingImport">
        <div class="periodCollectBtn">
            <p class="titleSty">集电线管理</p>
            <div class="collectSeach">
                <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" @change="changeProject">
                        <el-option
                        v-for="item in windProjectoptions"
                        :key="item.projectNo"
                        :label="item.projectName"
                        :value="item.projectNo">
                        </el-option>
                    </el-select>
                </div>
                <div class="exceed">
                    <span style="margin-top: 3px;">集电线名称：</span>
                    <el-select v-model="windCollect" placeholder="请选择" :disabled="!collectShow">
                        <el-option
                        v-for="item in windCollectoptions"
                        :key="item.collectorwireNo"
                        :label="item.wireName"
                        :value="item.collectorwireNo">
                        </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="el-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="toWtgMang">机组管理</el-button>
            </div>
            <div class="periodCollectTableData">
                <el-table :data="collecttableData" border style="width: 100%" @row-click="rowClick">
                    <el-table-column label="操作" width="60" align="center">
                        <template #default="scope">
                            <el-radio v-model="collectradio" :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}}</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.wireName}}</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.wireCapacity ? scope.row.wireCapacity : 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">
                    <el-form-item label="风场名称" prop="wpNo">
                        <el-select v-model="ruleForm.wpNo" placeholder="请选择风电场" @change="changeWindData">
                            <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="projectNo">
                        <el-select v-model="ruleForm.projectNo" placeholder="请选择风场期次" :disabled="multiple">
                            <el-option
                            v-for="item in projectNooptions"
                            :key="item.projectNo"
                            :label="item.projectName"
                            :value="item.projectNo">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="集电线名称" prop="wireName">
                        <el-input v-model="ruleForm.wireName" 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="wireCapacity">
                        <el-input type="number" 
                        v-model="ruleForm.wireCapacity" 
                        placeholder="请输入装机容量" 
                        @keyup.native="ruleForm.wireCapacity = $utils.oninput(ruleForm.wireCapacity, 3, '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>
            <span slot="footer" 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>
        </el-dialog>
        <import-dailog ref="importPage" @successImport="successImport" @importLoading="importLoading"></import-dailog>
    </div>
</template>

<script>
import importDailog from '../importPage/importDailog.vue'
import {apiGetCollectorwireprojectList, apiGetaddCollectorwireproject, apiGetupdateCollectorwireproject, 
apiGetdeleteCollectorwireproject,apiGetCollectorwireprojectByProjectNo, apiGetWindplantprojectListByWindplantNo, 
apiGetcheckCollectorwireprojectAnalogNoUnique, apiGetStation} from '../../../api/api'
export default {
  components: { importDailog },
    data() {
        return {
            changeDateSelect: [],
            loadingImport: false,
            title:'',
            windTurbines:'',
            collecttableData:[],
            windFramoptions:[], //风电场数据
            projectNooptions: [], //期次数据
            windProject: '',
            projectShow: true, //期次是否能搜索
            windProjectoptions: [], // 搜索中期次的数据
            windCollect: '',
            collectShow:false,
            windCollectoptions: [], // 搜索中集电线的数据
            collectradio: {},
            single: false,
            multiple: true,
            ashModify: true,
            dialogVisible:false,
            regionaloptions:[],
            page:{
                pagesize: 13,
                currentPage: 1,
                total: 0
            },
            ruleForm: {
                wpNo: '',
                projectNo: '',
                wireName:'',
                analogNo: '',
                wireCapacity: '',
                statSpeedTime:''
            },
            rules: {
                wpNo: [
                    { required: true, message: '请选择风场', trigger: 'change' }
                ],
                projectNo: [
                    { required: true, message: '请输入期次编号', trigger: 'blur' }
                ],
                wireName: [
                    { required: true, message: '请输入集电线名称', trigger: 'blur' }
                ],
                analogNo: [
                    { required: true, message: '请输入有功功率遥测号', trigger: 'blur' }
                ],
                wireCapacity: [
                    { required: true, message: '请输入装机容量', trigger: 'blur' }
                ],
                statSpeedTime: [
                    { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                ]
            }
        }
    },
    mounted() {
        this.getWindFramData()
    },
    methods:{
        toWtgMang() {
            this.$router.push({name: 'unitManagement'})
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getCollectorwireprojectList('seach')
        },
        successImport(val) {
            this.loadingImport = false
            this.getCollectorwireprojectList()
        },
        importLoading(val) {
            this.loadingImport = true
        },
        reset() {
            this.windTurbines = ''
            this.windProject = ''
            this.projectShow = false
            this.windCollect = ''
            this.collectShow = false
            this.page = {
                pagesize: 13,
                currentPage: 1,
                total: 0
            }
            this.getCollectorwireprojectList()
        },
        handleSizeChange(val){
            this.page.pagesize = val
            if (this.windTurbines != '') {
                this.getCollectorwireprojectList('seach')
            } else {
                this.getCollectorwireprojectList()
            }
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            if (this.windTurbines != '') {
                this.getCollectorwireprojectList('seach')
            } else {
                this.getCollectorwireprojectList()
            }
        },
        rowClick(row, column, event) {
            this.collectradio = row
        },
        closeDialog() {
            this.$refs['ruleForm'].clearValidate()
        },
        submitForm(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.collectradio.analogNo !== that.ruleForm.analogNo*1) {
                        that.checkanalogNo(that.ruleForm.analogNo)
                    } else {
                        that.addWindProjectMsg()
                    }
                }
            });
        },
        // 校验遥测号是否唯一---------待定
        checkanalogNo(num) {
            let that = this
            let params = {
                analogNo: num
            }
            apiGetcheckCollectorwireprojectAnalogNoUnique(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.projectNo,
                statSpeedTime: that.getTime(that.ruleForm.statSpeedTime),
                wireCapacity: that.ruleForm.wireCapacity,
                wireName: that.ruleForm.wireName,
                wpNo: that.ruleForm.wpNo
            }
            if (!that.single) {
                apiGetaddCollectorwireproject(params).then(datas =>{
                    that.changeshowData(datas)
                })
            } else {
                params.collectorwireNo = that.collectradio.collectorwireNo
                apiGetupdateCollectorwireproject(params).then(datas =>{
                    that.changeshowData(datas)
                })
            }
        },
        changeshowData(datas) {
            if (datas.msg === '操作成功') {
                this.dialogVisible = false
                this.single = false
                if (!this.single) {
                    this.$message({
                        message: '期次集电线配置信息新增成功',
                        type: 'success'
                    });
                } else {
                    this.$message({
                        message: '期次集电线配置信息修改成功',
                        type: 'success'
                    });
                }
                this.getCollectorwireprojectList('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: '',
                projectNo: '',
                wireName:'',
                analogNo: '',
                wireCapacity: '',
                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.getCollectorwireprojectList()
                    that.getprojectNoData(that.windTurbines, 'seach')
                }
            })
        },
        // 选择风电场后
        changeWindData(val) {
            if (!this.single) {
                this.multiple = false
                this.getprojectNoData(val)
            }
        },
        // 选择搜索中的风电场
        changeWind(val) {
            this.projectShow = true
            this.windProject = ''
            this.getprojectNoData(val, 'seach')
        },
        // 获取风电场期次数据
        getprojectNoData(val, type) {
            let that = this
            let params = {
                stationNo: val
            }
            apiGetWindplantprojectListByWindplantNo(params).then(datas =>{
                if (datas && datas.data && datas.data.list) {
                    if (type) {
                        that.windProjectoptions = datas.data.list
                    } else {
                        that.projectNooptions = datas.data.list
                    }
                }
            })
        },
        // 选择搜索中的期次
        changeProject(val) {
            this.collectShow = true
            this.getCollector(val)
        },
        // 根据期次查询集电线
        getCollector(num) {
            let that = this
            let params = {
                stationNo: that.windTurbines,
                projectNo: num.toString()
            }
            apiGetCollectorwireprojectByProjectNo(params).then(datas=>{
                if (datas && datas.data && datas.data.list.length>0) {
                    that.windCollectoptions = 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.getprojectNoData(this.collectradio.wpNo)
            this.ruleForm = {
                wpNo: this.collectradio.wpNo,
                projectNo: this.collectradio.projectNo,
                wireName: this.collectradio.wireName,
                analogNo: this.collectradio.analogNo,
                wireCapacity: this.collectradio.wireCapacity,
                statSpeedTime:new Date(this.collectradio.statSpeedTime)
            }
        },
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let params = {
                    wpNo: this.collectradio.wpNo,
                    projectNo: this.collectradio.projectNo,
                    collectorwireNo: this.collectradio.collectorwireNo
                }
                apiGetdeleteCollectorwireproject(params).then(datas =>{
                    if (datas.msg === '操作成功') {
                        that.getCollectorwireprojectList()
                    }
                })
                if (that.collecttableData.length>0) {
                    that.collectradio = that.collecttableData[0]
                }
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            })
        },
        getCollectorwireprojectList(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
                params.collectorwireNo = that.windCollect
            }
            apiGetCollectorwireprojectList(params).then(datas =>{
                if (datas && datas.rows) {
                    that.collecttableData = datas.rows
                    that.collectradio = datas.rows[0]
                    that.page.total = datas.total
                    that.ashModify = true
                }
            })
        }
    }
}
</script>

<style lang="less">
.periodCollect{
  .periodCollectBtn{
      background: #fff;
      padding: 10px 20px 0 20px;
      .collectSeach{
          display: flex;
          margin:20px;
            .exceed{
                display: flex;
                margin-right:20px;
                .el-select{
                    width: 200px;
                    height:30px;
                    margin-right:10px;
                    .el-input__wrapper{
                        padding: 0 7px;
                    }
                    .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;
          }
      }
  }
  .periodCollectTableData{
    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%;
          }
      }
  }
}
</style>