<template>
  <el-dialog :title="!id ? '新增品牌' : '编辑品牌'" :close-on-click-modal="false" :visible.sync="visible"
  >
    
      <el-form :inline="true">
        <el-form-item label="类型">
          <el-select
            v-model="specReq.typeId"
            :disabled="departmentReaderOnly"
            @change="changeType"
          >
            <el-option v-for="item in types" :key="item.id" :label="item.type" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        
      </el-form>
      <el-form :inline="true">
        <el-form-item label="品牌">
          <el-input v-model="brand" placeholder="品牌名称" clearable>{{brand}}</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 { apiEditDeviceBrands, apiAddDeviceBrands } from "@/api/api_equipment";
export default {
  data() {
    return {
      visible: false,
      types: [],
      id: null,
      typeId: 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,
        brand: null,
        typeId: null,
        rank: null, //排序
      },
    };
  },
  mounted() {},
  computed: {},
  methods: {
    init(
      obj,
      types
    ) {
      console.log("device-brand-detail init")
      this.specReq.typeId = obj.typeId
      this.typeId = obj.typeId
      this.id = obj.id
      this.type = obj.type
      this.rank = obj.rank
      this.brand = obj.brand
      this.types = JSON.parse(JSON.stringify(types))//拷贝一份
      this.types.splice(0, 1)//去掉全部
      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.specReq.typeId == null){
        this.$message.error('请选择类型')
        return
      }
      
      if(this.brand == null || this.brand.length == 0){
        this.$message.error('请输入品牌名称')
        return
      }
      
      if((!this.rank && this.rank != 0) || this.rank.length == 0){
        this.$message.error('请设置排序')
        return
      }
      
      this.specReq.rank = this.rank
      this.specReq.brand = this.brand
      
      this.specReq.id = this.id
      this.editSpecs()
    },
    editSpecs(){
      console.log('editSpecs')
      if(this.isAdd){
        apiAddDeviceBrands(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 {
        apiEditDeviceBrands(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>