<template>
  <el-dialog :title="!id ? '新增类型' : '编辑类型'" :close-on-click-modal="false" :visible.sync="visible"
  >
    
      
      <el-form :inline="true">
        <el-form-item label="类型">
          <el-input v-model="type" placeholder="类型名称" clearable>{{type}}</el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="rank" placeholder="请输入数字" clearable>{{rank}}</el-input>
        </el-form-item>
      </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="warning" @click="save(0)">取消</el-button>
      <el-button type="primary" @click="save(1)">保存</el-button>
      
    </span>
  </el-dialog>
</template>
<script>
import {  apiEditDeviceTypes, apiAddDeviceTypes } from "@/api/api_equipment";
export default {
  data() {
    return {
      visible: false,
      id: null,
      type:null,
      brand:null,
      rank: 0, //排序
      departmentReaderOnly: false,
      isAdd: false,
      specReq: {
        page: 1,
        rows: 10,
        sort: null,
        order: null,
        id: null,
        isDelete: 0,
        gmtCreate: null,
        gmtModified: null,
        spetypecs: null,
        describe: null,
        rank: null, //排序
      },
    };
  },
  mounted() {},
  computed: {},
  methods: {
    async init(obj) {
      console.log("device-type-detail init")
      this.specReq.id = obj.id
      this.id = obj.id
      this.type = obj.type
      this.rank = obj.rank
      
    
      this.visible = true
      if(!this.id){
        this.isAdd = true
      }else{
        this.isAdd = false
      }
      
    },
    changeType(val){
      console.log(val)
      this.getBrands()
      
    },
    changeBrand(val){
      console.log(val)
      
    },
    
    save(val){
      if(val == 0){
        this.visible = false
        return
      }
      if(!this.type || this.type.length == 0){
        this.$message.error('请输入类型名称')
        return
      }
      if((!this.rank && this.rank != 0) || this.rank.length == 0){
        this.$message.error('请设置排序')
        return
      }
      this.specReq.type = this.type
      this.specReq.rank = this.rank
      this.specReq.id = this.id
      this.editSpecs()
    },
    editSpecs(){
      console.log('editSpecs')
      if(this.isAdd){
        apiAddDeviceTypes(this.specReq).then((data) => {
          console.log(data);
          if (data && data.code === 0) {
            this.visible = false
            this.$message('添加成功')
            this.$emit('refreshDataList')
          } else {
            this.$message.error(data.msg)
          }
        });
      } else {
        apiEditDeviceTypes(this.specReq).then((data) => {
          console.log(data);
          if (data && data.code === 0) {
            this.visible = false
            this.$message('更新成功')
            this.$emit('refreshDataList')
          } else {
            this.$message.error(data.msg)
          }
        });
      }
      
    }
  },
};
</script>