<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="departmentName" placeholder="请输入科室名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="70px">
          <el-form-item label="病种名称" prop="diseaseTypeName">
            <el-input v-model="queryParams.diseaseTypeName" placeholder="请输入病种名称" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="问卷标题" prop="questionnaireName">
            <el-input v-model="queryParams.questionnaireName" 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-item style="float:right">
            <el-button type="primary" size="mini" @click="handleAdd"
              v-hasPermi="['system:question:add']">新增问卷模板</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="questionList">
          <el-table-column label="序号" type="index" width="55" align="center" />
          <el-table-column label="问卷模板名称" align="center" prop="questionnaireName" />
          <el-table-column label="问题个数" align="center" prop="questionCount" />
          <el-table-column label="问卷模板ID" align="center" prop="questionnaireId" />
          <el-table-column label="问卷状态" align="center" prop="questionnaireStatus">
            <template slot-scope="scope">
              <el-button size="mini" type="text" v-if="scope.row.questionnaireStatus == 'PUBLISHED'">已发布</el-button>
              <el-button size="mini" type="info" v-if="scope.row.questionnaireStatus == 'UNPUBLISHED'">未发布</el-button>
            </template>
          </el-table-column>
          <el-table-column label="病种名称" align="center" prop="diseaseTypeName" />
          <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:question:edit']">编辑</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:question: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-col>
    </el-row>
  </div>
</template>

<script>
import { listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion, getDepartmentList } from "@/api/system/question";

export default {
  name: "Question",
  data() {
    return {
      //科室名称
      departmentName: '',
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 部门树选项
      deptOptions: undefined,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 问卷基本信息表格数据
      questionList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        departmentId: null,
        departmentName: null,
        diseaseTypeId: null,
        diseaseTypeName: null,
        questionnaireName: null,
        questionnaireDescription: null,
        answeringMethod: null,
        questionnaireId: null,
        questionCount: null,
        questionnaireTotalScore: null,
        questionnaireStatus: null,
        questionnaireSort: null,
        questionnaireRemark: null,
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    departmentName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  methods: {
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.departmentId = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 查询科室下拉树结构 */
    getTreeselect() {
      getDepartmentList({}).then(response => {
        response.data.forEach(e => {
          e.label = e.departmentName
        })
        this.deptOptions = response.data;
      });
    },
    /** 查询问卷基本信息列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问卷？').then(function () {
        return delQuestion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/question/export', {
        ...this.queryParams
      }, `question_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
