<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="分类编号" prop="classMastId">
        <el-input
          v-model="queryParams.classMastId"
          placeholder="请输入分类编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="基本分类" prop="classMastType">
        <el-select v-model="queryParams.classMastType" placeholder="请选择基本分类" clearable>
          <el-option
            v-for="dict in dict.type.material_class_fast"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="分类名称" prop="classNameMast">
        <el-input
          v-model="queryParams.classNameMast"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者部门" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入创建者部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者id" prop="createUserId">
        <el-input
          v-model="queryParams.createUserId"
          placeholder="请输入创建者id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改着id" prop="updateUserId">
        <el-input
          v-model="queryParams.updateUserId"
          placeholder="请输入修改着id"
          clearable
          @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="['materialClass:materialClassMaster:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['materialClass:materialClassMaster:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['materialClass:materialClassMaster:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['materialClass:materialClassMaster:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialClassList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="基本分类" align="center" prop="classMastType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.material_class_fast" :value="scope.row.classMastType"/>
        </template>
      </el-table-column>
      <el-table-column label="分类编号" align="center" prop="classMastId" />
      <!--<el-table-column label="主键" align="center" prop="id" />-->

      <el-table-column label="分类名称" align="center" prop="classNameMast" />
      <el-table-column label="子分类数量" align="center" prop="classCount" />
      <!--<el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者部门" align="center" prop="deptId" />-->
      <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="['materialClass:materialClassMaster:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['materialClass:materialClassMaster:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物资分类一级分类对话框 -->
    <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="12">
            <el-form-item label="基本大类" prop="classMastType" >
              <el-select v-model="form.classMastType" placeholder="请选择基本大类" :disabled="true">
                <el-option
                  v-for="dict in dict.type.material_class_fast"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类编号" prop="classMastId">
              <el-input v-model="form.classMastId" placeholder="请输入分类编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="分类名称" prop="classNameMast">
          <el-input v-model="form.classNameMast" placeholder="请输入分类名称" />
        </el-form-item>
       <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>-->
        <el-divider content-position="center">二级分类信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddMaterialClassSub">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMaterialClassSub">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="materialClassSubList" :row-class-name="rowMaterialClassSubIndex" @selection-change="handleMaterialClassSubSelectionChange" ref="materialClassSub">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="分类编号" prop="classNameSub" width="180">
            <template slot-scope="scope">
              <el-input v-model="scope.row.classSubId" placeholder="请输入二级分类编号" />
            </template>
          </el-table-column>
          <el-table-column label="分类名称" prop="classNameSub" width="220">
            <template slot-scope="scope">
              <el-input v-model="scope.row.classNameSub" placeholder="请输入二级分类名称" />
            </template>
          </el-table-column>

        </el-table>
      </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 { listMaterialClass, getMaterialClass, delMaterialClass, addMaterialClass, updateMaterialClass } from "@/api/ehs/materialClassMaster";

  export default {
    name: "MaterialClass",
    dicts: ['material_class_fast'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedMaterialClassSub: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 物资分类一级分类表格数据
        materialClassList: [],
        // 物资分类-二级分类表格数据
        materialClassSubList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          classMastId: null,
          classMastType: null,
          classNameMast: null,
          deptId: null,
          createUserId: null,
          updateUserId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          classMastId: [
            { required: true, message: "分类编号不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询物资分类一级分类列表 */
      getList() {
        this.loading = true;
        listMaterialClass(this.queryParams).then(response => {
          this.materialClassList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          classMastId: null,
          id: null,
          classMastType: null,
          classNameMast: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          deptId: null,
          createUserId: null,
          updateUserId: null
        };
        this.materialClassSubList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加物资分类一级分类";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const classMastId = row.id || this.ids
        getMaterialClass(classMastId).then(response => {
          this.form = response.data;
          this.materialClassSubList = response.data.materialClassSubList;
          this.open = true;
          this.title = "修改物资分类一级分类";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.materialClassSubList = this.materialClassSubList;
            if (this.form.id != null) {
              updateMaterialClass(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMaterialClass(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const classMastIds = row.id || this.ids;
        this.$modal.confirm('是否确认删除物资分类一级分类编号为"' + classMastIds + '"的数据项？').then(function() {
          return delMaterialClass(classMastIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 物资分类-二级分类序号 */
      rowMaterialClassSubIndex({ row, rowIndex }) {
        row.index = rowIndex + 1;
      },
      /** 物资分类-二级分类添加按钮操作 */
      handleAddMaterialClassSub() {
        let obj = {};
        obj.classMastId = "";
        obj.classNameSub = "";
        obj.remark = "";
        obj.deptId = "";
        this.materialClassSubList.push(obj);
      },
      /** 物资分类-二级分类删除按钮操作 */
      handleDeleteMaterialClassSub() {
        if (this.checkedMaterialClassSub.length == 0) {
          this.$modal.msgError("请先选择要删除的物资分类-二级分类数据");
        } else {
          const materialClassSubList = this.materialClassSubList;
          const checkedMaterialClassSub = this.checkedMaterialClassSub;
          this.materialClassSubList = materialClassSubList.filter(function(item) {
            return checkedMaterialClassSub.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handleMaterialClassSubSelectionChange(selection) {
        this.checkedMaterialClassSub = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('materialClass/materialClass/export', {
          ...this.queryParams
        }, `materialClass_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
