<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="锁具机构类型编码" prop="locksetTypeCode">
        <el-input
          v-model="queryParams.locksetTypeCode"
          placeholder="请输入锁具机构类型编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="锁具机构类型名称" prop="locksetTypeName">
        <el-input
          v-model="queryParams.locksetTypeName"
          placeholder="请输入锁具机构类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-no-more-click
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:md:locksettype:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-no-more-click
          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="deptList"
      row-key="locksetTypeId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        prop="locksetTypeCode"
        label="锁具机构类型编码"
        width="260"
      ></el-table-column>
      <el-table-column
        prop="locksetTypeName"
        label="锁具机构类型名称"
        width="260"
      ></el-table-column>
      <el-table-column
        label="锁具机构类型图标"
        align="center"
        prop="locksetTypeIcon"
      >
        <template slot-scope="scope">
          <img
            v-if="scope.row.locksetTypeIcon"
            :src="scope.row.locksetTypeIcon"
            alt=""
            style="width: 50px; height: 50px"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="锁具机构类型图片"
        align="center"
        prop="locksetTypeImg"
      >
        <template slot-scope="scope">
          <img
            v-if="scope.row.locksetTypeImg"
            :src="scope.row.locksetTypeImg"
            alt=""
            style="width: 50px; height: 50px"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="locksetTypeDescription"-->
<!--        label="锁具机构类型描述"-->
<!--        width="260"-->
<!--      ></el-table-column>-->
      <el-table-column
        prop="locksetTypeSpec"
        label="锁具机构类型规格"
        width="260"
      ></el-table-column>
      <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:md:locksettype:edit']"
            >修改
          </el-button>
          <el-button
            v-no-more-click
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['mes:md:locksettype: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:md:locksettype:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="父级" prop="parentTypeId">
              <treeselect
                style="width: 318px"
                v-model="form.parentTypeId"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="选择父级"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="19">
            <el-form-item label="锁具机构类型编码" prop="locksetTypeCode">
              <el-input
                style="width: 318px"
                v-model="form.locksetTypeCode"
                placeholder="请输入锁具机构类型编码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label-width="80">
              <el-switch
                style="width: 170px"
                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="18">
            <el-form-item label="锁具机构类型名称" prop="locksetTypeName">
              <el-input
                style="width: 318px"
                v-model="form.locksetTypeName"
                placeholder="请输入锁具机构类型名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="锁具机构类型图标" prop="locksetTypeIcon">
              <ImageUploadSingle
                :limit="1"
                :value="form.locksetTypeIcon"
                :fileSize="5"
                @onUploaded="handleIconUplaoded"
                @onRemoved="handleIconRemoved"
              ></ImageUploadSingle>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="锁具机构类型图片" prop="locksetTypeImg">
              <ImageUploadSingle
                :limit="1"
                :value="form.locksetTypeImg"
                :fileSize="5"
                @onUploaded="handleImgUplaoded"
                @onRemoved="handleImgRemoved"
              ></ImageUploadSingle>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="锁具机构类型描述" prop="locksetTypeSpec">
              <el-input
                type="text"
                style="width: 318px"
                v-model="form.locksetTypeSpec"
                placeholder="请输入锁具机构类型规格"
              />
            </el-form-item>
<!--            <el-form-item label="锁具机构类型描述" prop="locksetTypeDescription">-->
<!--              <el-input-->
<!--                type="textarea"-->
<!--                style="width: 318px"-->
<!--                v-model="form.locksetTypeDescription"-->
<!--                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 {
  listLockType,
  getLockTypeInfo,
  addLockType,
  updateLockType,
  delLockType,
} from "@/api/mes/locktype/locktype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { genCode } from "@/api/system/autocode/rule";

export default {
  name: "Dept",
  dicts: ["sys_normal_disable"],
  components: { Treeselect },
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 地图预览弹框
      dialogVisibleMap: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        current: 1,
        size: -1,
        locksetTypeCode: null,
        locksetTypeName: null,
        locksetTypeId: null,
      },
      // 总条数
      total: 0,
      // 表单参数
      form: {
        parentTypeId: 0,
      },
      WorkShopListOptions: null, //所属车间下拉数据
      EditId: null, //判断是否是编辑弹框的值
      // 表单校验
      rules: {
        // parentId: [
        //   { required: true, message: '父级不能为空', trigger: 'blur' }
        // ],

        locksetTypeCode: [
          { required: true, message: "锁具机构类型编号不能为空", trigger: "blur" },
        ],
        locksetTypeName: [
          { required: true, message: "锁具机构类型名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      listLockType(this.queryParams).then((response) => {
        debugger;
        var depts = this.handleTree(
          response.data.records,
          "locksetTypeId",
          "parentTypeId",
          "children"
        );
        this.deptList = depts;
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.locksetTypeId,
        label: node.locksetTypeName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        parentTypeId: undefined,
        locksetTypeId: undefined,
        locksetTypeName: undefined,
        materialsTypeId: 1,
        locksetTypeDescription: undefined,
        locksetTypeCode: undefined,
        locksetTypeIcon: undefined,
        locksetTypeIcon: undefined,
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: -1,
        locksetTypeCode: null,
        locksetTypeName: null,
        locksetTypeId: null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentTypeId = row.locksetTypeId;
      } else {
        this.form.parentTypeId = 0;
      }
      this.open = true;
      this.title = "添加锁具机构类型";
      listLockType().then((response) => {
        this.deptOptions = this.handleTree(
          response.data.records,
          "locksetTypeId",
          "parentTypeId",
          "children"
        );
        console.log(this.deptOptions, "this.deptOptions");
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.EditId = row.locksetTypeId || this.ids;
      getLockTypeInfo(row.locksetTypeId).then((response) => {
        this.form = response.data;
        if(this.form.parentTypeId==0){
          this.form.parentTypeId=null
        }
        this.open = true;
        this.title = "修改锁具机构类型";
      });
      listLockType().then((response) => {
        this.deptOptions = this.handleTree(
          response.data.records,
          "locksetTypeId",
          "parentTypeId",
          "children"
        );
      });
    },
    //图片上传成功
    handleImgUplaoded(imgUrl) {
      console.log(imgUrl, "编辑拿到的图片");
      this.form.mapImg = imgUrl[0].url;
    },
    //图片移除
    handleImgRemoved(imgUrl) {
      this.form.mapImg = null;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.locksetTypeId != undefined) {
            updateLockType(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form, "新增作业区域参数");
            addLockType(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //图片上传成功
    handleImgUplaoded(imgUrl) {
      this.form.locksetTypeImg = imgUrl[0].url;
    },
    //图标上传成功
    handleIconUplaoded(imgUrl) {
      this.form.locksetTypeIcon = imgUrl[0].url;
    },
    handleIconUplaoded1(imgUrl) {
      this.form.locksetTypeImg = imgUrl[0].url;
    },
    //图标上传成功
    handleIconUplaoded2(imgUrl) {
      this.form.locksetTypeIcon = imgUrl[0].url;
    },

    // 图标移除
    handleIconRemoved(imgUrl) {
      this.form.locksetTypeIcon = null;
    },
    // 图标移除
    handleIconRemoved1(imgUrl) {
      this.form.locksetTypeImg = null;
    },
    handleIconRemoved2(imgUrl) {
      this.form.locksetTypeIcon = "";
    },
    //图片移除
    handleImgRemoved(imgUrl) {
      this.form.locksetTypeImg = null;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除编号为"' + row.locksetTypeCode + '"的数据项？')
        .then(function () {
          return delLockType(row.locksetTypeId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("LOCKSET_TYPE_CODE").then((response) => {
          this.form.locksetTypeCode = response;
        });
      } else {
        this.form.locksetTypeCode = null;
      }
    },
  },
};
</script>
