<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="硬件编号" prop="hardwareTypeCode">
        <el-input
          v-model="queryParams.hardwareTypeCode"
          placeholder="请输入硬件编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型名称" prop="hardwareTypeName">
        <el-input
          v-model="queryParams.hardwareTypeName"
          placeholder="请输入类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="enableFlag">
        <el-select
          v-model="queryParams.enableFlag"
          placeholder="请选择状态"
          clearable
        >
          <el-option label="启用" value="y"></el-option>
          <el-option label="禁用" value="n"></el-option>

          <!--          <el-option-->
          <!--            v-for="dict in dict.type.mes_order_status"-->
          <!--            :key="dict.value"-->
          <!--            :label="dict.label"-->
          <!--            :value="dict.value"-->
          <!--          />-->
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 50px">
        <el-button
          v-no-more-click
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-no-more-click
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="machinerytypeList"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        label="硬件编号"
        width="220"
        align="center"
        prop="hardwareTypeCode"
      />
      <el-table-column
        label="类型名称"
        width="150px"
        align="center"
        prop="hardwareTypeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="enableFlag">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enableFlag == 'y'">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>

          <!--          <dict-tag-->
          <!--            :options="dict.type.mes_order_status"-->
          <!--            :value="scope.row.enableFlag"-->
          <!--          />-->
        </template>
      </el-table-column>
      <!--      <el-table-column label="硬件层级" align="center" prop="level">-->
      <!--        <template slot-scope="scope">-->
      <!--          {{-->
      <!--            scope.row.level == "1"-->
      <!--              ? "一"-->
      <!--              : scope.row.level == "2"-->
      <!--                ? "二"-->
      <!--                : scope.row.level == "3"-->
      <!--                  ? "三"-->
      <!--                  : scope.row.level-->
      <!--          }}级-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-no-more-click
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:hw:type:edit']"
            >编辑</el-button
          >
          <el-button
            v-no-more-click
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['mes:hw:type:add']"
            >新增</el-button
          >
          <el-button
            v-no-more-click
            v-if="scope.row.parentTypeId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:hw:type:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改设备类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24" v-if="form.parentTypeId !== 0">
            <el-form-item label="父类型" prop="parentTypeId">
              <treeselect
                v-model="form.parentTypeId"
                :options="machinerytypeOptions"
                :normalizer="normalizer"
                placeholder="请选择父类型"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="硬件编号" prop="hardwareTypeCode">
              <el-input
                v-model="form.hardwareTypeCode"
                placeholder="请输入硬件编号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="30">
              <el-switch
                v-model="autoGenFlag"
                active-color="#13ce66"
                active-text="自动生成"
                @change="handleAutoGenChange(autoGenFlag)"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="硬件名称" prop="machineryTypeName">
              <el-input
                v-model="form.hardwareTypeName"
                placeholder="请输入硬件名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="enableFlag">
              <el-radio v-model="form.enableFlag" label="y">启用</el-radio>
              <el-radio v-model="form.enableFlag" label="n">禁用</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button v-no-more-click @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHanrwareType,
  getHardwareTypeInfo,
  delHardwareType,
  addHardwareType,
  updateHardwareType,
  delHardware,
} from "@/api/mes/hw/hadrwareType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { genCode } from "@/api/system/autocode/rule";
export default {
  name: "hardwareType",
  dicts: ["sys_yes_no"],
  components: {
    Treeselect,
  },
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 设备类型表格数据
      machinerytypeList: [],
      // 设备类型树选项
      machinerytypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        parentTypeId: null,
        ancestors: null,
        current: 1,
        size: -1,
        hardwareTypeCode: "",
        hardwareTypeName: "",
        enableFlag: "",
      },
      // 表单参数
      form: {
        parentTypeId: 0,
      },
      EditId: null, //编辑传递id
      // 表单校验
      rules: {
        hardwareTypeCode: [
          { required: true, message: "硬件编号不能为空", trigger: "blur" },
        ],
        hardwareTypeName: [
          { required: true, message: "硬件名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备类型列表 */
    getList() {
      this.loading = true;
      listHanrwareType(this.queryParams).then((response) => {
        this.machinerytypeList = this.handleTree(
          response.data.records,
          "id",
          "parentTypeId"
        );
        this.loading = false;
        // console.log(this.machinerytypeList, "machinerytypeList");
      });
    },
    /** 转换设备类型数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.hardwareTypeName,
        children: node.children,
      };
    },
    /** 查询设备类型下拉树结构 */
    getTreeselect() {
      listHanrwareType().then((response) => {
        debugger;
        this.machinerytypeOptions = [];
        const data = this.handleTree(
          response.data.records,
          "id",
          "parentTypeId"
        )[0];
        // console.log(data, "父类型回显");
        this.machinerytypeOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        level: "",
        parentTypeId: 1,
        ancestors: null,
        enableFlag: "",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        hardwareTypeCode: "",
        hardwareTypeName: "",
      };
      this.resetForm("form");
      this.autoGenFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 层级计数器函数
    handleChange(value) {
      // console.log(value);
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentTypeId = row.id;
      } else {
        this.form.parentTypeId = 0;
      }
      this.form.enableFlag = "n";
      this.open = true;
      this.title = "新增硬件类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentTypeId = row.id;
      }
      getHardwareTypeInfo(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑硬件类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateHardwareType(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHardwareType(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("HARDWARE_TYPE_CODE").then((response) => {
          this.form.hardwareTypeCode = response;
        });
      } else {
        this.form.hardwareTypeCode = null;
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const repairIds = row.id || this.ids;
      const repairCodes = row.hardwareTypeCode;
      this.$modal
        .confirm('是否删除硬件编号为"' + repairCodes + '"的硬件类型？')
        .then(function () {
          return delHardwareType(repairIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
