<template>
  <div class="app-container" ref="appPanel">
    <div ref="toolPanel" class="toolPanel">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <div class="toolTitle">物料类别</div>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:financialClass:add']"
          >新增
          </el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠
          </el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索
          </el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >刷新
          </el-button
          >
        </el-col>
      </el-row>
      <right-toolbar ref="toolBarRef" @customCols="customColsHandler" :showSearch.sync="showSearch"
                     showColumnsType="transfer" :columns="columns"></right-toolbar>

    </div>
    <div class="search-container" ref="searchPanel">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="mini"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="物料分类编码" prop="materialClassCode">
          <el-input
            v-model="queryParams.materialClassCode"
            placeholder="请输入物料分类编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料分类名称" prop="materialClassName">
          <el-input
            v-model="queryParams.materialClassName"
            placeholder="请输入物料分类名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="类型" prop="dataType">
          <el-select
            v-model="queryParams.dataType"
            placeholder="请选择类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.financial_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <div ref="mainTablePanel" :style="{ height: mainTableDivHeight }">
        <el-table
          v-if="refreshTable"
          v-loading="loading"
          border
          size="mini"
          :height="mainTableHeight"
          :data="financialClassList"
           row-key="id"
          :default-expand-all="isExpandAll"
          :key="rndKey"
          @header-dragend="handleHeaderDragEnd"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
          <template>
            <el-table-column v-for="item in columns" :key="item.seq" :label="item.label" align="center"
                             :prop="item.columnName" v-if="item.visible" :width="item.width">
              <template v-slot="scope">
                <dict-tag v-if="item.columnName === 'dataType'" v-slot="scope" :options="dict.type.financial_type"
                          :value="scope.row[item.columnName]"/>
                <span v-else>{{ scope.row[item.columnName] }}</span>
              </template>
            </el-table-column>
          </template>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:financialClass:edit']"
              >修改
              </el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="handleAdd(scope.row)"
                v-hasPermi="['system:financialClass:add']"
              >新增
              </el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:financialClass:remove']"
              >删除
              </el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 添加或修改财务分类对话框 -->
    <el-dialog
      id="dialog-container"
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <div class="edit-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="85px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="父级ID" prop="parentId">
                <treeselect
                  v-model="form.parentId"
                  :options="financialClassOptions"
                  :normalizer="normalizer"
                  placeholder="请选择父级ID"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="物料分类编码" prop="materialClassCode">
                <el-input
                  v-model="form.materialClassCode"
                  placeholder="请输入物料分类编码"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="物料分类名称" prop="materialClassName">
                <el-input
                  v-model="form.materialClassName"
                  placeholder="请输入物料分类名称"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="类型" prop="dataType">
                <el-select v-model="form.dataType" placeholder="请选择类型">
                  <el-option
                    v-for="dict in dict.type.financial_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <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 {listCOLUMN} from "@/api/system/column";
import {
  addFinancialClass,
  delFinancialClass,
  getFinancialClass,
  listFinancialClass,
  updateFinancialClass,
} from "@/api/system/financialClass";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import tableMixin from "@/api/tableMixin"


export default {
  name: "FinancialClass",
  dicts: ["financial_type"],
  mixins: [tableMixin],
  components: {
    Treeselect,
  },
  data() {
    return {
      rndKey: 0,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 财务分类表格数据
      financialClassList: [],
      // 财务分类树选项
      financialClassOptions: [],
      // 弹出层标题
      title: "",
      mainTableHeight: "800px",
      mainTableDivHeight: "800px",
      businessCodeTrk: "financialClass",
      rightToolbarShow: false,

      tableNo: 0,
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: null,
        materialClassCode: null,
        materialClassName: null,
        dataType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      columns: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.computedTableHeight();
  },
  watch: {
    showSearch: function () {
      this.computedTableHeight();
    },
  },
  methods: {
    /** 查询用户展示字段定义管理列表 */
    computedTableHeight() {
      this.$nextTick(() => {
        var totalHeight = this.$refs.appPanel.clientHeight;
        var toolHeight = this.$refs.toolPanel.clientHeight;
        var searchHeight = this.$refs.searchPanel.clientHeight;

        var tablePanelHeight = totalHeight - toolHeight - searchHeight - 14 - 5;

        var mainHeight = tablePanelHeight;
        this.mainTableDivHeight = mainHeight + "px";
        this.mainTableHeight = mainHeight - 25 + "px";
      });
    },
    /** 查询财务分类列表 */
    getList() {
      this.loading = true;
      listFinancialClass(this.queryParams).then((response) => {
        this.financialClassList = this.handleTree(
          response.data.list,
          "id",
          "parentId"
        );
        if (this.columns.length == 0) this.columns = response.data.columns;
        // console.log(this.columns)
        // console.log( response.data)
        // console.log( response.data.column)
        this.loading = false;
      });
    },
    /** 转换财务分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.materialClassName,
        children: node.children,
      };
    },
    /** 查询财务分类下拉树结构 */
    getTreeselect() {
      listFinancialClass().then((response) => {
        this.financialClassOptions = [];
        const data = {id: 0, materialClassName: "顶级节点", children: []};
        data.children = this.handleTree(response.data, "id", "parentId");
        this.financialClassOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        materialClassCode: null,
        materialClassName: null,
        dataType: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
        this.form.dataType = "1";
      } else {
        this.form.parentId = 0;
        this.form.dataType = "0";
      }
      this.open = true;
      this.title = "添加财务分类";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getFinancialClass(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) {
            updateFinancialClass(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFinancialClass(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除财务分类编号为"' + row.id + '"的数据项？')
        .then(function () {
          return delFinancialClass(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
  },
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 84px);
}
</style>
