<template>
  <div class="app-container" style="padding: 0px;">
    <span>
      <div class="head-container">
      <el-input
        v-model="queryParams.name"
        placeholder="请输入章节名称"
        clearable
        size="small"
        prefix-icon="el-icon-search"
        style="margin-bottom: 20px"
      />
      </div>
      <div class="head-container">
        <el-table
          ref="chapterList"
          v-loading="loading"
          :data="chapterList"
          style="margin-bottom: 20px"
          border
          class="menuTable"
          @row-click="handleNodeClick"
          :show-header="false">
          <el-table-column align="center" prop="code" width="50" :show-overflow-tooltip="true"/>
          <el-table-column align="center" prop="name"/>
        </el-table>
      </div>
    </span>
  </div>
</template>

<script>
import { listChapter, getChapter, delChapter, addChapter, updateChapter } from "@/api/rc/chapter";
import { listDept } from "@/api/system/dept";

export default {
  name: "Chapter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 章节表格数据
      chapterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        auditId: null,
        code: null,
        name: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 装置列表
      deptOptions: [],
    };
  },
  created() {
    // this.getList();
    this.getDeptList();
  },
  methods: {
    // 节点单击事件
    handleNodeClick(row) {
      this.$emit('questionnaire', row.id);
    },
    /** 查询章节列表 */
    getList(auditId) {
      this.loading = true;
      if (auditId != null) {
        this.queryParams.auditId = auditId;
      }
      listChapter(this.queryParams).then(response => {
        this.chapterList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询装置列表 */
    getDeptList() {
      listDept().then(response => {
        let data = response.data;
        for (let i = 0; i < data.length; i++) {
          // 非顶级节点
          if (data[i].parentId !== 0) {
            // 插入装置列表
            this.deptOptions.push({"dictLabel": data[i].deptName, "dictValue": data[i].deptId});
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        auditId: null,
        code: null,
        name: null,
        deptId: 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.open = true;
      this.title = "添加章节";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChapter(id).then(response => {
        // 字符串转数组
        if (response.data.deptId != null && response.data.deptId != "") {
            response.data.deptId = response.data.deptId.split(",").map(Number);
        } else {
            response.data.deptId = [];
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改章节";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 数组转字符串
          if (this.form.deptId != null) {
              this.form.deptId = this.form.deptId.toString();
          }
          if (this.form.id != null) {
            updateChapter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChapter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除章节编号为"' + ids + '"的数据项？').then(function() {
        return delChapter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rc/chapter/export', {
        ...this.queryParams
      }, `chapter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
  .menuTable {
    cursor: pointer;  /*鼠标悬停变小手*/
  }
</style>
