<template>
  <el-card class="container">
    <el-row :gutter="20" class="content">
      <el-col :xs="8" :sm="8" :md="6" :lg="4">
        <div class="custom-menu">
          <el-menu
            ref="customMenu"
            :default-active="defaultActive"
            @select="onSelect"
          >
            <el-menu-item
              v-for="(type, index) in typeList"
              :key="'type' + index"
              :index="type.id.toString()"
            >{{ type.name }}</el-menu-item>
          </el-menu>
        </div>
      </el-col>
      <el-col :xs="16" :sm="16" :md="18" :lg="20">
        <div class="top-container" style="display: flex;justify-content: space-between">
          <el-form
            ref="searchForm"
            :model="searchForm"
            inline
          >
            <el-form-item
              label="设备名称"
              prop="instanceName"
            >
              <el-input v-model="searchForm.instanceName" maxlength="50" />
            </el-form-item>
            <el-form-item
              label="业务ID"
              prop="businessId"
            >
              <el-input v-model="searchForm.businessId" maxlength="50" />
            </el-form-item>
            <el-form-item
              label="型号"
              prop="deviceModel"
            >
              <el-input v-model="searchForm.deviceModel" maxlength="200" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="getInstanceList(1)">查询</el-button>
              <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
          <div class="buttons-group">
            <el-button type="primary" @click="showAddItem">新增实例</el-button>
          </div>
        </div>
        <el-table
          :data="dataTable"
          border
        >
          <el-table-column
            type="index"
            label="序号"
            width="50px"
            align="center"
          />
          <el-table-column
            prop="name"
            label="设备名称"
            min-width="155"
          />
          <el-table-column
            prop="description"
            label="设备描述"
            min-width="155"
          />
          <el-table-column
            prop="businessId"
            label="设备业务ID"
            min-width="155"
          />
          <el-table-column
            prop="deviceModel"
            label="设备型号"
            min-width="155"
          />
          <el-table-column
            prop="createTime"
            label="创建时间"
            :formatter="$moment"
            min-width="155"
          />
          <el-table-column
            prop="updateTime"
            label="修改时间"
            :formatter="$moment"
            min-width="155"
          />
          <el-table-column
            label="操作"
            min-width="120"
          >
            <template slot-scope="scope">
              <el-button type="text" @click="editItem(scope.row)">编辑</el-button>
              <el-popconfirm
                title="确定要删除此条记录吗？"
                style="margin-left: 10px"
                @onConfirm="deleteItem(scope.row.id)"
              >
                <el-button slot="reference" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="margin-top-20 text-align-right"
          :current-page="pageForm.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageForm.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
    <el-dialog
      :title="add ? '新增实例' : '编辑实例'"
      :visible.sync="dialogVisible"
      width="700px"
    >
      <el-form
        v-if="dialogVisible"
        ref="instanceForm"
        :model="instanceForm"
        label-width="90px"
        :rules="rules"
      >
        <el-form-item
          label="设备名称:"
          prop="name"
        >
          <el-input v-model="instanceForm.name" />
        </el-form-item>
        <el-form-item
          label="设备描述:"
          prop="description"
        >
          <el-input
            v-model="instanceForm.description"
            type="textarea"
            :row="2"
          />
        </el-form-item>
        <el-form-item
          label="设备业务ID:"
        >
          <el-input v-model="instanceForm.businessId" />
        </el-form-item>
        <el-form-item
          label="设备型号:"
        >
          <el-input v-model="instanceForm.deviceModel" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addItem">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getAllDeviceModel,
  getDeviceInstance,
  addDeviceInstance,
  updateDeviceInstance,
  deleteDeviceInstance
} from '@/api/equipment-management/equipment-instance'

export default {
  name: 'EquipmentInstance',
  props: {},
  data() {
    return {
      dataTable: [],
      dataHeader: [],
      defaultActive: null,
      typeList: [],
      searchForm: {
        instanceName: null,
        businessId: null,
        deviceModel: null
      },
      pageForm: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      dialogVisible: false,
      instanceForm: {
        deviceTypeId: null,
        name: null,
        description: null,
        businessId: null,
        deviceModel: null
      },
      add: true,
      rules: {
        name: [
          { required: true, message: '请输入设备名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入设备描述', trigger: 'blur' }
        ]
      },
      currentId: null,
      currentDeviceTypeId: null
    }
  },
  created() {
    getAllDeviceModel()
      .then(res => {
        this.typeList = res.data
        if (this.typeList.length > 0) {
          const type = this.typeList[0]
          this.currentDeviceTypeId = type.id
          const indexPath = type.id.toString()
          this.defaultActive = indexPath
          this.getInstanceList(1)
        }
      })
      .catch(err => {
        this.$message.error(err.message)
      })
  },
  mounted() {},
  methods: {
    showAddItem() {
      this.instanceForm = {
        deviceTypeId: this.currentDeviceTypeId,
        name: null,
        description: null,
        businessId: null,
        deviceModel: null
      }
      this.dialogVisible = true
      this.add = true
    },
    onSelect(index, indexPath) {
      const id = parseInt(index)
      const activeType = this.typeList.find(v => v.id === id)
      this.currentDeviceTypeId = activeType.id
      this.getInstanceList(1)
    },
    getInstanceList(pageNum) {
      const params = {
        deviceTypeId: this.currentDeviceTypeId,
        name: this.searchForm.instanceName,
        businessId: this.searchForm.businessId,
        deviceModel: this.searchForm.deviceModel,
        pageNum: pageNum,
        pageSize: this.pageForm.pageSize
      }
      getDeviceInstance(params)
        .then(res => {
          const { records, total } = res.data
          this.dataTable = records
          this.pageForm.total = total
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.pageForm.pageSize = val
      this.getInstanceList(1)
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val
      this.getInstanceList(val)
    },
    editItem(row) {
      const { id, deviceTypeId, name, description, businessId, deviceModel } = row
      this.instanceForm = {
        deviceTypeId,
        name,
        description,
        businessId,
        deviceModel
      }
      this.currentId = id
      this.dialogVisible = true
      this.add = false
    },
    deleteItem(id) {
      deleteDeviceInstance({ id })
        .then(_ => this.getInstanceList(1))
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    addItem() {
      this.$refs.instanceForm.validate()
        .then(_ => {
          return { ...this.instanceForm }
        })
        .then(params => {
          if (!this.add) { params.id = this.currentId }
          return this.add ? addDeviceInstance(params) : updateDeviceInstance(params)
        })
        .then(_ => this.$message.success('操作成功'))
        .then(_ => this.getInstanceList(1))
        .then(_ => {
          this.dialogVisible = false
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    resetForm() {
      this.$refs.searchForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.container{
  min-height: calc(100vh - 95px);
  .content{
    display: flex;
    min-height: calc(100vh - 135px);
    .padding-left{
      padding-left: 10px;
    }
    .custom-menu{
      width: 100%;
      background-color: #efeff1;
      height: 100%;
      .el-menu{
        background-color: initial;
        border-right: none;
        border-top: solid 1px #e6e6e6;
        border-left: solid 1px #e6e6e6;
        .el-menu-item.el-menu-item{
          min-width: initial;
          background-color: #efeff1;
        }
        .el-menu-item.is-active{
          background-color: #fff;
          color: #303133;
        }
      }
    }
    .top{
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
