<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="父级类别id" prop="parentCategoryId">
        <el-input
          v-model="queryParams.parentCategoryId"
          placeholder="请输入父级类别id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="类别名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入类别名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别编码" prop="categoryCode">
        <el-input
          v-model="queryParams.categoryCode"
          onKeyUp="value=value.replace(/[\W]/g,'')"
          placeholder="请输入类别编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="类别排序" prop="categorySort">
        <el-input
          v-model="queryParams.categorySort"
          placeholder="请输入类别排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别概述" prop="categoryRemark">
        <el-input
          v-model="queryParams.categoryRemark"
          placeholder="请输入类别概述"
          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="['system:agencyCategory: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="['system:agencyCategory: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="['system:agencyCategory: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="['system:agencyCategory:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="agencyCategoryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="父级类别id" align="center" prop="parentCategoryId" /> -->
      <el-table-column label="类别名称" align="center" prop="categoryName" />
      <el-table-column label="类别编码" align="center" prop="categoryCode" />
      <!-- <el-table-column label="类别等级" align="center" prop="categoryLevel">
        <template slot-scope="scope">
          {{ scope.row.status == "1" ? "一级类别" : "" }}
          {{ scope.row.status == "2" ? "二级类别" : "" }}
          {{ scope.row.status == "3" ? "三级类别" : "" }}
        </template>
      </el-table-column> -->
      <el-table-column label="类别排序" align="center" prop="categorySort" />

      <el-table-column
        label="类别概述"
        align="center"
        prop="categoryRemark"
        show-overflow-tooltip
      />
      <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:agencyCategory:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:agencyCategory: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父级类别" prop="parentCategoryId">
          <el-cascader
            :props="{ checkStrictly: true }"
            :options="treeOptions"
            @change="change($event)"
            style="width: 250px; color: black"
            v-model="form.parentCategoryId"
            :show-all-levels="false"
            :placeholder="
              form.parentCategoryName
                ? form.parentCategoryName
                : '请选择父级类别'
            "
            clearable
            ref="cascader"
            :key="isResouceShow"
          ></el-cascader>
          <!-- <el-input v-model="form.parentCategoryId" placeholder="请输入父级类别id" /> -->
        </el-form-item>
        <el-form-item label="类别名称" prop="categoryName">
          <el-input
            maxlength="50"
            v-model="form.categoryName"
            style="width: 250px"
            placeholder="请输入类别名称"
          />
        </el-form-item>
        <el-form-item label="类别编码" prop="categoryCode" v-if="disabled">
          <el-input
            maxlength="50"
            :disabled="disabled"
            onKeyUp="value=value.replace(/[\W]/g,'')"
            v-model="form.categoryCode"
            style="width: 250px"
            placeholder="请输入类别编码"
          />
        </el-form-item>
        <!-- <el-form-item label="类别等级" prop="categoryLevel">
          <el-select
            v-model="form.categoryLevel"
            placeholder="请选择"
            style="width: 250px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="类别排序" prop="categorySort">
          <el-input-number
            v-model="form.categorySort"
            controls-position="right"
            :min="0"
            :max="99999"
            placeholder="请输入类别排序"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="类别概述" prop="categoryRemark">
          <el-input
            maxlength="100"
            v-model="form.categoryRemark"
            style="width: 250px"
            placeholder="请输入类别概述"
          />
        </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 {
  listAgencyCategory,
  getAgencyCategory,
  delAgencyCategory,
  addAgencyCategory,
  updateAgencyCategory,
} from "@/api/system/agencyCategory";
import { agencyCategoryList } from "@/api/system/agency";
export default {
  name: "AgencyCategory",
  data() {
    return {
      treeOptions: [],
      disabled: true,
      // 遮罩层
      loading: true,
      isResouceShow: 1,

      // 选中数组
      ids: [],
      options: [
        {
          value: "1",
          label: "一级类别",
        },
        {
          value: "2",
          label: "二级类别",
        },
        {
          value: "3",
          label: "三级类别",
        },
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机构类别表格数据
      agencyCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentCategoryId: null,
        categoryName: null,
        categoryCode: null,
        categoryLevel: null,
        categorySort: null,
        categoryRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryName: [
          { required: true, message: "类别名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.gettreelistitem();
  },
  methods: {
    // 父级类别
    gettreelistitem() {
      agencyCategoryList().then((res) => {
        // 接口请求
        this.treeOptions = res.data.map((item) => {
          return { value: item.id, ...item };
        });
      });
    },
    change(e) {
      console.log(e, "9");
      if (e) {
        this.form.parentCategoryId = e[e.length - 1];
      }
    },
    /** 查询机构类别列表 */
    getList() {
      this.loading = true;
      listAgencyCategory(this.queryParams).then((response) => {
        this.agencyCategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentCategoryId: null,
        categoryName: null,
        categoryCode: null,
        categoryLevel: null,
        categorySort: undefined,
        categoryRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      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.disabled = false;
      this.open = true;
      this.title = "添加机构类别";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disabled = true;
      const id = row.id || this.ids;
      getAgencyCategory(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) {
            updateAgencyCategory(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              ++this.isResouceShow;
              this.getList();
              this.gettreelistitem();
            });
          } else {
            addAgencyCategory(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              ++this.isResouceShow;
              this.getList();
              this.gettreelistitem();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除机构类别编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAgencyCategory(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/agencyCategory/export",
        {
          ...this.queryParams,
        },
        `机构类别_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.el-cascader .el-input input::-webkit-input-placeholder {
  color: #cccfd6;
}
</style>
