<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution">
    <div style="display: inline-block">
      <span style="font-weight: bold;font-size: 14px">&#12288;场站名称：</span>
      <el-select style="width:250px" clearable v-model="stationCode" size="small">
        <el-option
          v-for="item in stationList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <span style="float: left">{{ item.label }}</span>
          <span style="float: right; color: #8492a6;font-size: 13px">{{ item.value }}</span>
        </el-option>
      </el-select>
      &nbsp;
      <div class="timeQuery" style="display: inline-block">
        <el-button size="small" :loading="loading" @click="dataQuery">查询</el-button>
      </div>
    </div>
    <basic-container>
      <avue-crud ref="crud"
                 :page.sync="page"
                 :data="tableData"
                 :table-loading="tableLoading"
                 :option="tableOption"
                 :before-open="beforeOpen"
                 @on-load="dataQuery"
                 @search-change="searchChange"
                 @refresh-change="refreshChange"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @row-update="handleUpdate"
                 @row-save="handleSave"
                 @row-del="rowDel">
        <template slot="menu" slot-scope="{ row, index }">
          <el-button type="primary" size="small" icon="el-icon-edit" class="myButton"
                     @click.stop="handleEdit(row,index)">
            编辑
          </el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" class="myButton"
                     @click.stop="handleDel(row,index)">
            删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {fetchList, getObj, addObj, putObj, delObj, getByStationCode, getStation} from '@/api/inverterinfo'
import {tableOption} from '@/const/crud/inverterinfo'
import {mapGetters} from 'vuex'

export default {
  name: 'inverterinfo',
  data() {
    return {
      stationList: [],
      stationCode: [],
      searchForm: {},
      tableData: [],
      loading: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      modId: '',//备用id
    }
  },
  mounted() {
    this.getStationCode()
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.idp_inverterinfo_add, false),
        delBtn: this.vaildData(this.permissions.idp_inverterinfo_del, false),
        editBtn: this.vaildData(this.permissions.idp_inverterinfo_edit, false)
      };
    }
  },
  methods: {
    checkequipmentNo(rule, value, callback) {
      if (value == '') {
        callback(new Error('请输入设备编号'))
      } else {
        var s6 = this.tableData
        for (let i = 0; i < s6.length; i++) {
          if (this.modId != s6[i].id) {
            if ((value == s6[i].equipmentNo)) {
              callback(new Error('设备编号不能重复'))
            }
          }
        }
      }
      callback()
    },
    /*打开表格前的回调函数*/
    beforeOpen(done) {
      const _self = this
      let equipmentNo = this.findObject(this.tableOption.column, 'equipmentNo')
      equipmentNo.rules = [{required: true, validator: _self.checkequipmentNo, trigger: 'blur'}]
      done()
    },
    handleEdit(row, index) {
      this.modId = row.id
      this.$refs.crud.rowEdit(row, index);
    },
    handleDel(row, index) {
      this.$refs.crud.rowDel(row, index);
    },
    getStationCode() {
      this.tableLoading = true
      getStation().then(response => {
        this.stationList = response.data.data
        this.tableLoading = false
      })
      this.dataQuery()
    },
    dataQuery() {
      this.tableLoading = true
      const param = new URLSearchParams()
      param.append('currentPage', this.page.currentPage)
      param.append('pageSize', this.page.pageSize)
      param.append('stationCode', this.stationCode)
      getByStationCode(param).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },

    rowDel: function (row, index) {
      this.$confirm('是否确认删除名称为 ' + row.name + ' 的逆变器', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.dataQuery()
      })
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.dataQuery()
      }).catch(() => {
        loading();
      });
    },
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.dataQuery()
      }).catch(() => {
        loading();
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange(current) {
      this.page.currentPage = current
    },
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.dataQuery()
      done()
    },
    refreshChange() {
      this.dataQuery()
    }
  }
}
</script>
<style scoped>
.myButton {
  color: #409EFF;
  border: none;
  background: transparent
}
</style>
