<template>
  <div class="app-container">
    <el-form
      :model="queryForm"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="80px"
      @submit.native.prevent
    >

      <!-- <el-form-item label="编号" prop="typeNum">
        <el-input
          v-model="queryForm.typeNum"
          placeholder="请输入编号"
          @keyup.enter.native="handleQuery"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="名称" prop="typeName">
        <el-input
          v-model="queryForm.typeName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="排序号" prop="typeOrder">
        <el-input
          v-model="queryForm.typeOrder"
          placeholder="请输入排序号"
          @keyup.enter.native="handleQuery"
        ></el-input>
      </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 class="mb8" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="handleAdd">新增
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-if="refreshTable"
      :data="assetsList"
      v-loading="loading"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="资产分类名称" header-align="center" prop="typeName"></el-table-column>
      <el-table-column label="资产编号" align="center" prop="typeNum"></el-table-column>
      <el-table-column label="排序号" align="center" prop="typeOrder"></el-table-column>
      <el-table-column label="备注" align="center" prop="remark"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:assets:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:assets:remove']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:assets:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      width="800px"
      :visible.sync="open"
      :title="title"
      append-to-body
      @close="cancel"
    >
      <el-form ref="postForm" :model="postForm" :rules="rules" label-width="110px">
        <el-form-item label="资产分类名称" prop="typeName">
          <el-input v-model="postForm.typeName"></el-input>
        </el-form-item>
        <el-form-item label="资产编号" prop="typeNum">
          <el-input v-model="postForm.typeNum"></el-input>
        </el-form-item>
        <el-form-item label="排序号" prop="typeOrder">
          <!--          <el-input v-model="postForm.typeOrder"></el-input>-->
          <el-input-number v-model="postForm.typeOrder" controls-position="right" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="postForm.remark"></el-input>
        </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 {
  getClassList,
  queryClass,
  addClass,
  editClass,
  deleteClass
} from '@/api/smartSchool/logisticsManage/assetsClassification.js'

export default {
  name: "AssetsClassification",
  data() {
    return {
      queryForm: {
        typeName: '',// 资产分类名称
        typeNum: '',// 编号
        typeOrder: '',// 排序号
        remark: '',// 备注
      },
      // 遮罩层
      loading: false,
      // 资产信息列表
      assetsList: [],
      // 新增/修改表单
      postForm: {},
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 校验规则
      rules: {
        typeName: [{required: true, message: '资产分类不能为空', trigger: 'blur'}],
        typeNum: [{required: true, message: '资产编号不能为空', trigger: 'blur'}],
      }
    }
  },

  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true; // 加载效果
      getClassList(this.queryForm).then(response => {
        this.assetsList = this.handleTree(response.data, "id", 'pid', 'children');
        this.loading = false;
      })
    },
    // 展开/折叠
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },

    // 搜索
    handleQuery() {
      this.getList();
    },
    // 新增
    handleAdd(row) {
      this.reset();
      if (row != null && row.id) {
        this.postForm.pid = row.id;
      } else {
        this.postForm.pid = 0;
      }
      this.open = true;
      this.title = "添加资产类别";
    },
    // 修改
    handleUpdate(row) {
      this.reset();
      queryClass(row.id).then(response => {
        this.postForm = response.data;
        this.open = true;
        this.title = "修改资产信息";
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal.confirm(`是否确认删除此数据？`).then(() => {
        this.loading = true;
        return deleteClass(row.id);
      }).then(() => {
        this.getList();
        this.loading = false;
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        this.loading = false
      });
    },
    // 确定
    submitForm() {
      this.$refs["postForm"].validate(valid => {
        if (valid) {
          if (this.postForm.id != null) {
            editClass(this.postForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClass(this.postForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置提交表单
    reset() {
      this.postForm = {
        typeName: '',// 资产分类名称
        typeNum: '',// 编号
        typeOrder: '',// 排序号
        remark: '',// 备注
      };
      this.resetForm('postForm')
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
}
</script>

<style>

</style>
