<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="addForm" :model="addForm" status-icon :rules="ruleso">
        <el-form-item prop="model_number" style="display: inline-block;">
          <el-input v-model="addForm.model_number" placeholder="快速添加" autocomplete="off" style="width: 200px;" class="filter-item" name="modelNumberAdd" />
        </el-form-item>
        <el-form-item prop="model_number" style="display: inline-block;">
          <el-select v-model="addForm.device_type" placeholder="请选择设备类型" autocomplete="off" class="filter-item">
            <el-option
              v-for="item in deviceTypes"
              :key="item.tid"
              :label="item.tname"
              :value="item.tid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <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="modelNumbers" :span-method="objectSpanMethod" border>
      <el-table-column prop="date" label="ID" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="设备型号">
        <template slot-scope="scope">
          <span>{{ scope.row.model_number }}</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 prop="name" label="创建时间" width="220">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="更新时间" width="220">
        <template slot-scope="scope">
          <span>{{ scope.row.updated_at }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button type="info" size="small" @click="handleEdit(scope.row.id, scope.row.model_number)">修改</el-button>
          <el-button type="danger" size="small" @click="deleteModelNumber(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import waves from '@/directive/waves'; // 水波纹效果
import { deviceTypeList } from '@/api/device';
import { modelNumbers, addModelNumber, updateModelNumber, deleteModelNumber } from '@/api/modelNumber';
export default {
  directives: { waves },
  data() {
    var validateNumber = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入设备型号'));
      } else {
        callback();
      }
    };
    var validateType = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择设备类型'));
      } else {
        callback();
      }
    };
    return {
      modelNumbers: [], // 设备型号列表
      modelNumber: '',
      deviceTypes: [],
      addForm: {
        model_number: '',
        device_type: '',
      },
      ruleso: {
        model_number: [
          { validator: validateNumber, trigger: 'blur' },
        ],
        device_type: [
          { validator: validateType, trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    this.getList();// 设备型号
    this.deviceTypeList();// 设备分类
  },
  methods: {
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 2) {
        if (rowIndex % 2 === 0) {
          return {
            rowspan: 2,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
    deviceTypeList() {
      deviceTypeList()
        .then(response => {
          this.deviceTypes = response.data.devicetype;
        })
        .catch(err => {
          console.log(err);
        });
    },
    getList() {
      modelNumbers()
        .then(response => {
          this.modelNumbers = response.data;
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 添加
    handleClick() {
      if (this.addForm.model_number === '' || this.addForm.device_type === '') {
        this.$message({
          type: 'error',
          message: '请输入内容',
        });
        return false;
      }
      this.$prompt('确认设备型号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '型号格式不正确',
        inputValue: this.addForm.model_number,
      }).then(({ value }) => {
        addModelNumber({ model_number: value, device_type_id: this.addForm.device_type })
          .then(response => {
            if (response.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功！',
              });
              this.getList();
            }
          })
          .catch(err => {
            console.log(err);
          });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入',
        });
      });
      this.modelNumber = '';
    },
    // 修改
    handleEdit(id, model_number) {
      this.$prompt('请输入设备型号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '型号格式不正确',
        inputValue: model_number,
      }).then(({ value }) => {
        updateModelNumber(id, { 'model_number': 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: '取消输入',
        });
      });
    },
    deleteModelNumber(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteModelNumber(id)
          .then(response => {
            if (response.code === 200) {
              this.getList();
            }
          })
          .catch(err => {
            console.log(err);
          });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
        });
      });
    },
  },
};
</script>

<style>
</style>
