<template>
  <div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

      <el-form-item label="类型"  >
        <el-select v-model="specReq.typeId" :disabled="departmentReaderOnly" @change="changeDepartment(1)">
          <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-item label="品牌">
        <el-select v-model="specReq.brandId" :disabled="departmentReaderOnly" placeholder="请选择" @change="changeDepartment(2)">
          <el-option
            v-for="item in brands"
            :key="item.id"
            :label="item.brand"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规格">
        <el-input v-model="specReq.specs" placeholder="规格信息" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"  @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="addNewSpec()">新增规格</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="dataListLoading" :data="data" style="width: 100%" @row-click="rowClick">
      <el-table-column prop="id" header-align="center" align="center" label="ID"></el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="类型"></el-table-column>
      <el-table-column prop="brand" header-align="center" align="center" label="品牌"></el-table-column>
      <el-table-column prop="specs" header-align="center" align="center" label="规格"></el-table-column>
      <el-table-column prop="rank" header-align="center" align="center" label="排序"></el-table-column>
      <el-table-column prop="skus" header-align="center" align="center" label="库存"></el-table-column>
      <el-table-column prop="used" header-align="center" align="center" label="已使用"></el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="180" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="handleAddOrUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="specReq.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="specReq.rows"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <device-spec-detail v-if="isShowDetail" ref="deviceSpecDetail" @refreshDataList="getData"></device-spec-detail>
  </div>
</template>
<script>
import { apiGetDeviceSpecs, apiGetDeviceBrands, apiDelDeviceSpecs } from "@/api/api_equipment";
import * as dd from "dingtalk-jsapi";
import DeviceSpecDetail from './device-spec-detail.vue'
export default {
  components: {
      DeviceSpecDetail
    },
  data() {
    
    return {
      isShowSearch: true,
      data: [],
      types: [],
      brands: [],
      name: "",
      categorys: "",
      input: "",
      dataListLoading: false,
      typeIndex: 0, //0现在是1级 1二级2三级
      showHeader: false,
      totalPage: 1,
      departmentReaderOnly: false,
      specReq: {
        page: 1,
        rows: 10,
        sort: null,
        order: null,
        id: null,
        isDelete: 0,
        gmtCreate: null,
        gmtModified: null,
        specs: null,
        brandId: null,
        typeId: null,
        rank: null, //排序
        skus: null, //库存
        remark: null,
        used: 0, //是否在
        
      },
      isShowDetail: false
    };
  },
  created() {
    // this.handleGetImgCaptcha()
  },
  
  mounted() {
    this.getData();
  },
  methods: {
    
    addNewSpec(){
      //id, typeId, type, brandId, brand, specs, skus, rank, used, remark, types, brands
      setTimeout(() => {
        var val = {id: null, typeId: null, type: null, brandId: null, brand: null, specs: null, skus: null, rank: 0, used: 0, remark: null}
        this.$refs.deviceSpecDetail.init(val, this.types, this.brands)
      }, 500)
      this.isShowDetail = true
      
    },
    changeDepartment(type) {
      if(type == 1){
        //获取品牌
        this.getBrands()
        this.specReq.brandId = null
      }else{
        this.specReq.page = 1
        this.getData()
      }
    },
    sizeChangeHandle(val) {
      this.specReq.page = 1
      this.specReq.rows = val
      this.getData();
    },
    currentChangeHandle(val) {
      this.specReq.page = val
      this.getData();
    },
    // 新增 / 编辑
    handleAddOrUpdate (val) {
      console.log(val)
      //id, typeId, type, brandId, brand, specs, skus, rank, used, remark, types, brands
      setTimeout(() => {
        
        this.$refs.deviceSpecDetail.init(val, this.types, this.brands)
      }, 500)
      this.isShowDetail = true
    },
    // 删除
      handleDelete (val) {
        
        this.$confirm('确定删除“' + val.specs + '”吗？删除后该规格将无法使用', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delDeviceSpecs(val.id)
        }).catch(() => {
        })
      },
      delDeviceSpecs(id){
        apiDelDeviceSpecs({page: 1,
        rows: 10,
        sort: null,
        order: null,
        id: id,
        isDelete: 0,
        gmtCreate: null,
        gmtModified: null,
        specs: null,
        brandId: null,
        typeId: null,
        rank: null, //排序
        skus: null, //库存
        remark: null,
        used: 0}).then((data) => {
        console.log(data);
        if (data && data.code === 0) {
          this.$message('删除成功')
          this.getData()
        } else {
          this.$message.error('删除失败' + data.msg)
        }
        this.dataListLoading = false;
      });
      },
    getData() {
      console.log("获取数据")
      console.log(this.name)
      console.log(this.categorys)
      this.dataListLoading = true
      apiGetDeviceSpecs(this.specReq).then((data) => {
        console.log(data);
        if (data && data.code === 0) {
          this.data = data.rows;
          this.totalPage = data.total
          this.types = data.types
        } else {
          this.data = [];
        }
        this.dataListLoading = false;
      });
    },
    getBrands(){
      apiGetDeviceBrands(this.specReq).then((data) => {
        console.log(data);
        if (data && data.code === 0) {
          this.brands = data.brands
          this.specReq.page = 1
          this.getData()
        } else {
          this.brands = [];
        }
        this.dataListLoading = false;
      });
    },
    btnSearch() {
      this.specReq.page = 1
      this.getData();
    },

    rowClick(row, event, column) {},
  },
};
</script>

<style>
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 10px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5f2f1;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.div_height {
  height: 38px;
}
.div_width {
  width: 100%;
}
.width_100 {
  width: 100px;
}
.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  padding: 1em;
}
.flex {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border: 1px solid;
}
.flex_son {
  width: 100%;
  height: 100%;
  border: 0x solid;
}
</style>