<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:deviceCategory:add']"
        >新增</el-button>
      </el-col>
        <el-col :span="1.5">
            <el-button
                    type="info"
                    plain
                    icon="el-icon-sort"
                    size="mini"
                    @click="toggleExpandAll"
            >展开/折叠</el-button>
        </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deviceCategoryList"
      row-key="categoryId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="分类名称" align="left" prop="name"/>
      <el-table-column label="默认图标" align="left" prop="defaultIcon" width="80">
        <template slot-scope="scope">
          <el-image
            style="width: 25px; height: 25px"
            :src="url + scope.row.defaultIcon" v-if="scope.row.defaultIcon" />
        </template>
      </el-table-column>
      <el-table-column label="异常图标" align="center" prop="errorIcon" width="80">
        <template slot-scope="scope">
          <el-image
            style="width: 25px; height: 25px"
            :src="url + scope.row.errorIcon" v-if="scope.row.errorIcon" />
        </template>
      </el-table-column>
      <el-table-column label="选中图标" align="center" prop="selectedIcon" width="80">
        <template slot-scope="scope">
          <el-image
            style="width: 25px; height: 25px"
            :src="url + scope.row.selectedIcon" v-if="scope.row.selectedIcon" />
        </template>
      </el-table-column>
      <el-table-column label="最小级数" align="center" prop="minZoom" width="80"/>
      <el-table-column label="最大级数" align="center" prop="maxZoom" width="80"/>
      <el-table-column label="数据类型" align="center" prop="dataType" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_category_data_type" :value="scope.row.dataType"/>
        </template>
      </el-table-column>
      <el-table-column label="填充色" align="center" prop="fillColor" width="80"/>
      <el-table-column label="边框色" align="center" prop="borderColor" width="80">
        <template slot-scope="scope">
          <el-tag :color="scope.row.borderColor" size="mini" v-if="scope.row.borderColor">{{scope.row.borderColor}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="宽度" align="center" prop="width" width="80"/>
      <el-table-column label="序号" align="center" prop="orderNum" width="80"/>
      <el-table-column label="预设分类" align="center" prop="preset" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.preset"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:deviceCategory:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.parentId == 0 || !scope.row.parentId"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['business:deviceCategory:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:deviceCategory:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改设备分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="上级分类" prop="parentId">
          <el-select v-model="form.parentId"  placeholder="请选择" @change="$forceUpdate()" clearable>
            <el-option
              v-for="item in deviceCategoryOptions"
              :key="item.categoryId"
              :label="item.name"
              :value="item.categoryId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-radio-group v-model="form.dataType">
            <el-radio
              v-for="dict in dict.type.device_category_data_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>

        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="默认图标" v-if="form.dataType === 1" prop="defaultIcon">
          <imageUpload v-model="form.defaultIcon" :limit="1" :file-size="10" :file-type="['.png', '.jpg', '.jpeg', '.gif']"/>
        </el-form-item>
        <el-form-item label="异常图标" v-if="form.dataType === 1" prop="errorIcon">
          <imageUpload v-model="form.errorIcon" :limit="1" :file-size="10" :file-type="['.png', '.jpg', '.jpeg', '.gif']"/>
        </el-form-item>
        <el-form-item label="选中图标" v-if="form.dataType === 1" prop="selectedIcon">
          <imageUpload v-model="form.selectedIcon" :limit="1" :file-size="10" :file-type="['.png', '.jpg', '.jpeg', '.gif']"/>
        </el-form-item>
        <el-form-item label="最小级数" prop="minZoom">
            <el-input-number v-model="form.minZoom" controls-position="right"   :min="1" :max="22" placeholder="请输入最小级数" />
        </el-form-item>
        <el-form-item label="最大级数" prop="maxZoom">
            <el-input-number v-model="form.maxZoom" controls-position="right"   :min="1" :max="22" placeholder="请输入最大级数" />
        </el-form-item>
        <el-form-item label="填充色" v-if="form.dataType === 3" prop="fillColor">
            <el-color-picker v-model="form.fillColor"></el-color-picker>
        </el-form-item>
        <el-form-item label="边框色" v-if="form.dataType !== 1" prop="borderColor">
            <el-color-picker v-model="form.borderColor"></el-color-picker>
        </el-form-item>
        <el-form-item label="宽度" prop="width" v-if="form.dataType === 2">
            <el-input-number v-model="form.width" controls-position="right"   :min="0" placeholder="请输入宽度" />
        </el-form-item>
        <el-form-item label="序号" prop="orderNum">
            <el-input-number v-model="form.orderNum" controls-position="right"   :min="0" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="预设分类" prop="preset">
            <el-switch v-model="form.preset"></el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceCategory, getDeviceCategory, delDeviceCategory, addDeviceCategory, updateDeviceCategory, exportDeviceCategory ,listParentDeviceCategory} from "@/api/business/deviceCategory";
export default {
  name: "DeviceCategory",
  dicts: ['sys_yes_no', 'device_category_data_type'],
  components: {

  },
  data() {
    return {
      url: BASE_URL,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 设备分类表格数据
      deviceCategoryList: [],
      // 设备分类树选项
      deviceCategoryOptions: [],
      // 弹出层标题
      title: "",
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        defaultIcon:[
          { required: true, message: "默认图标不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        fillColor: [
          { required: true, message: "填充色不能为空", trigger: "change" }
        ],
        borderColor: [
          { required: true, message: "边框色不能为空", trigger: "change" }
        ],
        width: [
          { required: true, message: "宽度不能为空", trigger: "change" }
        ],
        minZoom: [
          { required: true, message: "最小级数不能为空", trigger: "blur" }
        ],
        maxZoom: [
          { required: true, message: "最大级数不能为空", trigger: "blur" }
        ],

        dataType: [
          { required: true, message: "数据类型不能为空", trigger: "change" }
        ],
        orderNum: [
          { required: true, message: "序号不能为空", trigger: "blur" }
        ],
        preset: [
          { required: true, message: "预设分类不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getDeviceCategorySelect(){
      listParentDeviceCategory().then(res => {
        this.deviceCategoryOptions = res.data
      })
    },
    /** 查询设备分类列表 */
    getList() {
      this.loading = true;
      listDeviceCategory(this.queryParams).then(response => {
        this.deviceCategoryList = this.handleTree(response.data, "categoryId", "parentId");
        this.loading = false;
      });
    },
    /** 转换设备分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.categoryId,
        label: node.name,
        children: node.children
      };
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        categoryId: null,
        parentId: null,
        name: null,
        defaultIcon: null,
        errorIcon: null,
        selectedIcon: null,
        minZoom: 1,
        maxZoom: 22,
        dataType: 1,
        fillColor: null,
        borderColor: null,
        width: null,
        orderNum: 0,
        preset: true,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getDeviceCategorySelect();
      if (row != null && row.categoryId) {
        this.form.parentId = row.categoryId;
      } else {
        this.form.parentId = null;
      }
      this.open = true;
      this.title = "添加设备分类";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
          this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //this.reset();
      this.getDeviceCategorySelect();
      getDeviceCategory(row.categoryId).then(response => {
        this.form = response.data;
        if (this.form.parentId == 0){
          this.form.parentId = null;
        }
        this.open = true;
        this.title = "修改设备分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.categoryId != null) {
            updateDeviceCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除设备分类编号为"' + row.categoryId + '"的数据项？').then(function() {
        return delDeviceCategory(row.categoryId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
