<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="query" :model="deviceSearch" style="display: inline-block;">
        <el-input v-model="categoryName" placeholder="快速添加" style="width: 200px;" class="filter-item" name="categoryAdd" />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="handleClick()">
          {{ $t('table.add') }}
        </el-button>
      </el-form>
    </div>
    <el-table :data="deviceListType" border style="width: 33%">
      <el-table-column prop="date" label="id" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.tid }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="分类名称">
        <template slot-scope="scope">
          <span>{{ scope.row.tname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="info" size="small" @click="handleEdit(scope.row.tid, scope.row.tname)">修改</el-button>
          <el-button type="danger" size="small" @click="delteClassify(scope.row.tid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { deviceTypeList, addClassify, updateClassify, delteClassify } from '@/api/device';
export default {
  data() {
    return {
      deviceListType: [], // 设备分类
      categoryName: '',
    };
  },
  created() {
    this.getList();// 设备分类
  },
  methods: {
    getList() {
      deviceTypeList()
        .then(response => {
          var devicetype = response.data;
          this.deviceListType = devicetype['devicetype'];
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 添加
    handleClick() {
      this.$prompt('请输入分类名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '分类名称格式不正确',
        inputValue: this.categoryName,
      }).then(({ value }) => {
        addClassify(value)
          .then(response => {
            if (response.code === 200) {
              this.getList();
            }
          })
          .catch(err => {
            console.log(err);
          });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入',
        });
      });
      this.categoryName = '';
    },
    // 修改分类
    handleEdit(tid, tname) {
      this.$prompt('请输入分类名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '分类名称格式不正确',
        inputValue: tname,
      }).then(({ value }) => {
        updateClassify({ 'tid': tid, 'tname': value })
          .then(response => {
            if (response.code === 200) {
              this.$message({
                type: 'success',
                message: '修改成功！',
              });
              this.getList();
            }
          })
          .catch(err => {
            console.log(err);
          });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入',
        });
      });
    },
    delteClassify(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delteClassify(id)
          .then(response => {
            if (response.code === 200) {
              this.getList();
            }
          })
          .catch(err => {
            console.log(err);
          });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
        });
      });
    },
  },
};
</script>

<style>
</style>
