<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务区" prop="areaCode">
        <el-select v-model="queryParams.areaCode">
          <el-option v-for="item in areaOptions" :label="item.areaName" :value="item.areaCode" :key="item.areaCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="箱柜代码" prop="boxCode">
        <el-input v-model="queryParams.boxCode" placeholder="请输入箱柜代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="箱体类型" prop="boxType">
        <el-select v-model="queryParams.boxType">
          <el-option v-for="item in boxTypeOptions" :label="item.name" :value="item.code" :key="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['ems:box:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ems:box:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ems:box:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['ems:box:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="boxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="箱柜代码" align="center" prop="boxCode" />
      <el-table-column label="箱体类型" align="center" prop="boxType">
        <template slot-scope="scope">
          <span>{{ getPowerBoxTypeName(scope.row.boxType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务区" align="center" prop="areaShortName" />
      <el-table-column label="安装位置" align="center" prop="location" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['basecfg:box:edit']">
            修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" class="deleteBtn" @click="handleDelete(scope.row)" v-hasPermi="['basecfg:box:remove']">
            删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改动力箱柜对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="箱柜代码" prop="boxCode">
              <el-input v-model="form.boxCode" placeholder="请输入箱柜代码"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="箱柜类型" prop="boxType" >
              <el-select v-model="form.boxType" placeholder="请选择箱柜类型" style="width: 100%;">
                <el-option v-for="item in boxTypeOptions" :label="item.name" :value="item.code" :key="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所在服务区" prop="areaCode">
              <el-select v-model="form.areaCode" placeholder="请选择所在服务区" style="width: 100%;">
                <el-option v-for="item in areaOptions" :label="item.areaName" :value="item.areaCode" :key="item.areaCode" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装位置" prop="location">
              <el-input v-model="form.location" placeholder="请输入安装位置" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="网关端口" prop="gatewayPort">
              <el-input v-model="form.gatewayPort" placeholder="请输入网关端口" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="网关地址" prop="gatewayAddr">
              <el-input v-model="form.gatewayAddr" placeholder="请输入网关地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="箱体尺寸" prop="boxSize">
              <el-input v-model="form.boxSize" placeholder="请输入箱体尺寸" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="电气元件" prop="elecEqptList" >
          <el-table v-loading="loading" :data="form.elecEqptList" max-height="400px">
            <el-table-column label="元件编码" align="center"  prop="eqptCode">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.eqptCode" placeholder="元件编码" />
              </template>
            </el-table-column>
            <el-table-column label="外系统编码" align="center"  prop="extEqptCode">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.extEqptCode" placeholder="外系统编码" />
              </template>
            </el-table-column>
            <el-table-column label="元件类型" align="center"  prop="eqptName">
              <template slot-scope="scope">
                <el-select v-model="scope.row.eqptType" size="mini" clearable style="width:100%">
                  <el-option v-for="item in eqptTypeOptions" :label="item.typeName" :key="item.typeCode" :value="item.typeCode"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="元件型号" align="center"  prop="eqptSpec">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.eqptSpec" placeholder="元件型号" />
              </template>
            </el-table-column>
            <el-table-column label="额定电压" align="center" prop="ratedVoltage">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.ratedVoltage" placeholder="额定电压" />
              </template>
            </el-table-column>
            <el-table-column label="额定电流" align="center"  prop="ratedCurrent">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.ratedCurrent" placeholder="额定电流" />
              </template>
            </el-table-column>
            <el-table-column label="祖籍列表" align="center"  prop="ancestors">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.ancestors" placeholder="祖籍列表" />
              </template>
            </el-table-column>
            <el-table-column label="上游元件" align="center"  prop="parentEqpt">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.parentEqpt" placeholder="上游元件" />
              </template>
            </el-table-column>
            <el-table-column label="下级支路" align="center"  prop="subCircuit">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.subCircuit" placeholder="下级支路" />
              </template>
            </el-table-column>
            <el-table-column align="center" fixed="right">
              <template slot="header">
                <div class="operateBtns" @click="addSub">
                  <span>操作</span><i class="el-icon-circle-plus-outline"></i>
                </div>
              </template>
              <template slot-scope="scope">
                <i class="el-icon-delete" @click="deleteSub(scope.$index)"></i>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPowerBox, getPowerBox, delPowerBox, addPowerBox, updatePowerBox } from '@/api/device/powerbox'
import { listTotalArea } from '@/api/basecfg/area'
import {listElecEqptType} from '@/api/basecfg/elecEqptType.js'

export default {
  name: 'Box',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动力箱柜表格数据
      boxList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      areaOptions: [],
      boxTypeOptions: [
        { code: 1, name: '配电柜' },
        { code: 2, name: '控制柜' },
        { code: 3, name: '逆变器' }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boxCode: null,
        boxType: null,
        areaCode: null,
        location: null,
        boxSize: null,
        gatewayAddr: null,
        gatewayPort: null
      },
      // 表单参数
      form: {
        id: null,
        boxCode: null,
        boxType: null,
        areaCode: null,
        location: null,
        boxSize: null,
        gatewayAddr: null,
        gatewayPort: null,
        elecEqptList: []
      },
      // 表单校验
      rules: {
        boxCode: [{ required: true, message: '箱柜代码不能为空', trigger: 'blur' }],
        boxType: [{ required: true, message: '箱体类型不能为空', trigger: 'change' }]
      },
      eqptTypeOptions:[]
    }
  },
  created() {
    this.getAreaOptions()
    this.getList()
    this.getEqptType()
  },
  methods: {
    getEqptType(){
      listElecEqptType().then(({rows})=>{
        this.eqptTypeOptions =rows
      })
    },
    addSub() {
      this.form.elecEqptList.push({
        eqptCode: '',
        extEqptCode: '',
        eqptType:'',
        eqptSpec:'',
        ratedVoltage:'',
        ratedCurrent:'',
        parentEqpt:'',
        meterDevice:'',
        ancestors: '',
        subCircuit: ''
      })
    },
    deleteSub(index) {
      this.form.elecEqptList.splice(index, 1)
    },
    /** 查询动力箱柜列表 */
    getList() {
      this.loading = true
      listPowerBox(this.queryParams).then(response => {
        this.boxList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = this.$options.data().form
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加动力箱柜'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPowerBox(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改动力箱柜'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.elecEqptList.forEach(item=>{
            item.boxCode = this.form.boxCode
          })
          if (this.form.id != null) {
            updatePowerBox(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPowerBox(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除动力箱柜编号为"' + ids + '"的数据项？')
        .then(function() {
          return delPowerBox(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'ems/powerBox/export',
        {
          ...this.queryParams
        },
        `box_${new Date().getTime()}.xlsx`
      )
    },
    getAreaOptions() {
      listTotalArea().then(response => {
        this.areaOptions = response.data
      })
    },
    getPowerBoxTypeName(boxType) {
      const boxTypeMap = {
        1: '配电柜',
        2: '控制柜',
        3: '逆变器'
      }
      return boxTypeMap[boxType] || ''
    }
  }
}
</script>
