<template>
  <div class="equipment-container">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="挂载设备" name="1">
        <el-form :inline="true" :model="searchParam" class="demo-form-inline">
          <el-form-item label="设备编号">
            <el-input clearable v-model="searchParam.equipmentSn" size="small" placeholder="请输入设备编号"
                      style="width: 200px"/>
          </el-form-item>
          <el-form-item label="设备名称">
            <el-input clearable v-model="searchParam.name" size="small" placeholder="请输入设备名称"
                      style="width: 200px"/>
          </el-form-item>
          <el-form-item label="设备类型">
            <el-select clearable v-model="searchParam.type" size="small" placeholder="-请选择设备类型-"
                       style="width: 150px">
              <el-option v-for="(value,key,index) in typeList" :key="key" :label="value" :value="key"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :loading="tableLoading" @click="search" size="small" icon="el-icon-search" type="primary">搜索
            </el-button>
            <el-button @click="initSearchParam();search()" icon="el-icon-refresh-right"
                       type="primary" size="small"
                       style="background-color: #fff; border: 1px solid #ccc ; color: #666">清空
            </el-button>
          </el-form-item>
        </el-form>
        <el-table stripe ref="table" :height="tableHeight" v-loading="tableLoading" size="medium"
                  :data="mountEquipmentList"
                  border row-key="id">
          <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center"/>
          <el-table-column show-overflow-tooltip prop="equipmentSn" label="设备编号" align="center"/>
          <el-table-column show-overflow-tooltip prop="name" label="设备名称" align="center"/>
          <el-table-column show-overflow-tooltip prop="brand" label="品牌" align="center"/>
          <el-table-column show-overflow-tooltip prop="model" label="型号" align="center"/>
          <el-table-column show-overflow-tooltip prop="type" label="设备类型" align="center">
            <template slot-scope="{row}">
              <span v-if="row.type">{{typeList[row.type]}}</span>
            </template>
          </el-table-column>
          <el-table-column width="120" show-overflow-tooltip prop="status" label="状态" align="center">
            <template slot-scope="{row}">
              <span v-show="row.status ==1" style="color: #666">在线</span>
              <span v-show="row.status ==2" style="color: #666">离线</span>
              <el-switch v-if="isAdmin||btnRule.some(btn=>btn=='status')" v-model="row.status" :active-value="1"
                         :inactive-value="2" size="small"
                         @change="changeStatus(row)"/>
            </template>
          </el-table-column>
          <el-table-column v-if="isAdmin||btnRule.some(btn=>btn=='edit')" width="100" label="操作" align="center">
            <template slot-scope="scoped">
              <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='edit')" class="item" effect="dark" content="编辑"
                          placement="bottom">
                <el-button @click="handleEdit(scoped.row)" type="primary" size="mini" icon="el-icon-edit"/>
              </el-tooltip>
              <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='drop')" class="item" effect="dark" content="删除"
                          placement="bottom">
                <el-button @click="handleDel(scoped.row)" type="danger" size="mini" icon="el-icon-delete"/>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div  class="page-current">
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="total, prev, pager, next, jumper"
            style="display: inline-block;"
            @current-change="handleCurrentChange"
            :total="count"/>
        </div>
      </el-tab-pane>
      <el-tab-pane v-if="isAdmin||btnRule.some(btn=>btn=='add')" label="添加设备" name="2">
        <editForm :ruleForm="ruleForm" :typeList="typeList"
                    style="width: 50%" v-if="activeName==2" @submit="submit"></editForm>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" center :visible.sync="addDialogVisible" title="编辑设备">
      <editForm v-if="addDialogVisible" :typeList="typeList" ref="dialogForm" :ruleForm="ruleForm"
                  style="width: 100%" @submit="submit" @onBack="onBack"></editForm>
    </el-dialog>
  </div>
</template>

<script>
  import mountEquipmentApi from '@/api/basicData/mountEquipment'
  import editForm from './editForm'
  import {mapGetters} from "vuex";

  export default {
    name: "mountEquipment",
    data() {
      return {
        activeName: '1',
        addDialogVisible: false,
        searchParam: {
          equipmentSn: '',
          name: '',
          type: '',
        },
        ruleForm: {
          equipmentSn:'',
          name:'',
          brand:'',
          model:'',
          type:'',
          status:1
        },
        typeList:{},
        currentPage: 1,
        pageSize: parseInt((document.body.clientHeight - 290 - 43) / 44.5),
        count: 0,
        mountEquipmentList: [],
        tableLoading: false,
        tableHeight: document.body.clientHeight - 290,
      }
    },
    components: {editForm},
    computed: {
      ...mapGetters(["btnRule", "isAdmin"])
    },
    created() {
    },
    mounted() {
      this.tableLoading = true
      this.getAllList()
      this.getParamsList()
    },
    watch: {
      activeName() {
        this.initRuleForm()
      }
    },
    methods: {
      handleDel(row){
        this.$confirm('此操作将删除设备, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableLoading=true
          mountEquipmentApi.dropMountEquipment({id:row.id}).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getAllList()
          }).catch(e => {
            this.tableLoading =false
          })
        }).catch(() => {
        });
      },
      indexMethod(index) {
        return (index + 1) + ((this.currentPage - 1) * this.pageSize);
      },
      getParamsList() {
        mountEquipmentApi.getParamsList().then(res => {
          this.typeList=res.data.type_list?res.data.type_list:{}
        }).catch(e => {
          this.typeList={}
        })
      },
      changeStatus(data) {
        var content = ''
        if (data.status === 1) {
          content = '确定要启用吗？'
        } else {
          content = '确定要禁用吗？'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            status: data.status == 1 ? '1' : '2',
            id: data.id
          }
          this.tableLoading = true
          mountEquipmentApi.setStatus(params).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功！'
            })
            this.getAllList()
          }).catch(e => {
            let mountEquipmentList = JSON.parse(JSON.stringify(this.mountEquipmentList))
            for (let i = 0; i < mountEquipmentList.length; i++) {
              if (mountEquipmentList[i].id == data.id) {
                mountEquipmentList[i].status = data.status == 1 ? 2 : 1
              }
            }
            this.mountEquipmentList = Object.assign([], mountEquipmentList)
            this.tableLoading = false
          })
        }).catch(() => {
          let mountEquipmentList = JSON.parse(JSON.stringify(this.mountEquipmentList))
          for (let i = 0; i < mountEquipmentList.length; i++) {
            if (mountEquipmentList[i].id == data.id) {
              mountEquipmentList[i].status = data.status == 1 ? 2 : 1
            }
          }
          this.mountEquipmentList = Object.assign([], mountEquipmentList)
          this.$message({
            type: 'info',
            message: '已取消该操作！'
          })
        })
      },
      handleEdit(data, type) {
        this.initRuleForm()
        this.addDialogVisible = true
        this.$nextTick(function () {
          try {
            let child = JSON.parse(JSON.stringify(data))
            child.equipmentSn=child.equipment_sn
            this.ruleForm = Object.assign({}, child)
            this.$refs.dialogForm.$refs['ruleForm'].resetFields()
          } catch (e) {
          }
        })
      },
      initSearchParam() {
        this.searchParam = {
          equipmentSn: '',
          name: '',
          type: '',
        }
      },
      initRuleForm() {
        this.ruleForm = {
          equipmentSn:'',
          name:'',
          brand:'',
          model:'',
          type:'',
          status:1
        }
      },
      getAllList() {
        let searchParam = JSON.parse(JSON.stringify(this.searchParam))
        searchParam.page = this.currentPage
        searchParam.pageSize = this.pageSize
        mountEquipmentApi.mountEquipmentList(searchParam).then(res => {
          this.mountEquipmentList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(err => {
          this.mountEquipmentList = []
          this.tableLoading = false
        })
      },
      search() {
        this.currentPage = 1
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      submit(type) {
        if (type == 1) {
          this.activeName='1'
          this.search()
        } else {
          this.tableLoading = true
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.getAllList()
          this.addDialogVisible = false
        }
      },
      onBack() {
        this.initRuleForm()
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .equipment-container {
    width: 100%;

    .el-input.is-disabled .el-input__inner {
      background-color: #fff !important;
      color: #333 !important;
    }
  }
</style>
