<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="5" :xs="24">
        <div class="left" ref="box">
          <div class="name">科室名称</div>
          <div style="padding: 10px 0;">
            <el-input v-model="departmentName" placeholder="请输入科室名称" clearable @keyup.enter.native="getTreeselect" />
          </div>
          <div class="listitem" v-for="(item, index) in deptOptions" :key="index" @click="clickDepartmenitem(item)">
            <div :class="queryParams.departmentId == item.id ? 'allactive' : 'all'">
              {{ item.departmentName }}
            </div>
            <span class="count">{{ item.countNum }}</span>
          </div>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="19" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="70px">
          <el-form-item label="问卷标题" prop="questionnaireName">
            <el-input v-model="queryParams.questionnaireName" placeholder="请输入问卷标题" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="病种名称" prop="diseaseTypeName">
            <el-input v-model="queryParams.diseaseTypeName" 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="questionType">
            <template slot-scope="scope">
              <div v-if="scope.row.questionType == 'REGULAR_QUESTIONNAIRE'">普通问卷</div>
              <div v-if="scope.row.questionType == 'SATISFACTION_QUESTIONNAIRE'">满意度问卷</div>
            </template>
          </el-table-column>
          <el-table-column label="问卷状态" align="center" prop="questionnaireStatus">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.questionnaireStatus == 'PUBLISHED'">已发布</el-tag>
              <el-tag type="warning" v-else>未发布</el-tag>
            </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-switch v-model="scope.row.questionnaireStatus" active-color="#13ce66" inactive-color="#ff4949"
                active-value="PUBLISHED" inactive-value="UNPUBLISHED" @change="switchstatus($event, scope.row)">
              </el-switch> -->
              <el-button size="mini" type="text" @click="switchstatus('UNPUBLISHED', scope.row)"
                v-if="scope.row.questionnaireStatus == 'PUBLISHED'">取消发布</el-button>
              <el-button size="mini" type="text" @click="switchstatus('PUBLISHED', scope.row)" v-else>发布</el-button>
              <el-button size="mini" type="text" @click="handleClassification(scope.row)"
                v-hasPermi="['system:question:edit']">分类管理</el-button>
              <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                v-hasPermi="['system:question:edit']">编辑</el-button>
              <el-button size="mini" type="text" @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>
    <el-dialog title="提示" :visible.sync="classificationOpen" width="30%" :before-close="classificationOpenfalse">
      <el-form ref="form" :model="classificationform" label-width="80px" :rules="rules">
        <el-form-item label="科室名称" prop="departmentId">
          <el-select v-model="classificationform.departmentId" placeholder="请选择科室" clearable style="width:300px"
            @clear="cleardepartment" @change="changedepartment">
            <el-option v-for="item in departmentlist" :key="item.id" :label="item.departmentName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="病种名称">
          <el-select v-model="classificationform.diseaseTypeId" placeholder="请选择病种" clearable style="width:300px"
            @change="changediseaseType">
            <el-option v-for="item in diseaselist" :key="item.id" :label="item.diseaseTypeName" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="classificationupload">确 定</el-button>
        <el-button @click="classificationOpenfalse">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

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

export default {
  name: "Question",
  data() {
    return {
      classificationOpen: false,
      classificationform: {},
      //科室名称
      departmentName: '',
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 部门树选项
      deptOptions: undefined,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 问卷基本信息表格数据
      questionList: [],
      departmentlist: [],
      diseaselist: [],
      // 查询参数
      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,
      },
      rules: {
        departmentId: [
          { required: true, message: '请选择科室', trigger: 'change' }
        ],
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    departmentName(val) {
      // this.getTreeselect()
    }
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  methods: {
    clickDepartmenitem(item) {
      this.loading = true;
      this.queryParams.departmentId = item.id;
      this.getList();
    },
    classificationupload() {
      let query = {
        id: this.classificationform.id,
        diseaseTypeId: this.classificationform.diseaseTypeId,
        diseaseTypeName: this.classificationform.diseaseTypeName,
        departmentId: this.classificationform.departmentId,
        departmentName: this.classificationform.departmentName,
      }
      updateclassification(query).then(res => {
        this.classificationform = {}
        this.$modal.msgSuccess("修改成功");
        this.getList();
        this.classificationOpen = false
      })
    },
    //分类管理
    handleClassification(row) {
      this.classificationform = JSON.parse(JSON.stringify(row))
      if (this.classificationform.departmentId) {
        let query = {
          departmentId: this.classificationform.departmentId
        }
        diseaseList(query).then(res => {
          this.diseaselist = res.data
        })
      }
      this.classificationOpen = true
    },
    classificationOpenfalse() {
      this.classificationOpen = false
      this.classificationform = {}
    },
    cleardepartment() {
      this.classificationform.diseaseTypeId = ''
      this.classificationform.departmentName = ''
      this.classificationform.diseaseTypeName = ''
    },
    changedepartment(e) {
      this.classificationform.departmentName = this.departmentlist?.find(el => el.id == e)?.departmentName
      let query = {
        departmentId: e
      }
      diseaseList(query).then(res => {
        this.diseaselist = res.data
        this.classificationform.diseaseTypeId = ''
        this.classificationform.diseaseTypeName = ''
      })
    },
    changediseaseType(e) {
      this.classificationform.diseaseTypeName = this.diseaselist?.find(el => el.id == e)?.diseaseTypeName
    },
    //切换发布状态
    switchstatus(e, item) {
      if (!item.departmentId && !this.diseaseTypeId) {
        this.$message.error('请选择问卷所属的科室以及科室病种后发布 / 取消发布！');
        return
      }
      let text
      if (e == 'PUBLISHED') {
        text = '是否发布此问卷模板?'
      } else {
        text = '是否取消发布此问卷模板?'
      }
      this.$confirm(`${text}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        let query = {
          id: item.id,
          questionnaireStatus: e
        }
        updateclassification(query).then(res => {
          if (e == 'PUBLISHED') {
            this.$modal.msgSuccess("修改为已发布");
          } else if (e == 'UNPUBLISHED') {
            this.$modal.msgSuccess("修改为未发布");
          }
          this.getList();
        })
      })
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.departmentId = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 查询科室下拉树结构 */
    getTreeselect() {
      getDepartmentList({
        departmentName: this.departmentName
      }).then(response => {
        // response.data.forEach(e => {
        //   e.label = e.departmentName
        // })
        this.deptOptions = response.data;
      });
      selectUserDepartment().then(res => {
        this.departmentlist = res.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();
      this.getTreeselect();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$store.dispatch('tagsView/delView', this.$route).then(({ visitedViews }) => {
        this.$router.push({
          path: "/knowledge/addQuestionnaire",
        });
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$store.dispatch('tagsView/delView', this.$route).then(({ visitedViews }) => {
        this.$router.push({
          path: "/knowledge/addQuestionnaire",
          query: {
            id: row.id,
          },
        });
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问卷？').then(function () {
        return delQuestion(ids);
      }).then(() => {
        this.getTreeselect();
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/question/export', {
        ...this.queryParams
      }, `question_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.left {
  height: 500px;
  overflow: auto;

  .name {
    font-weight: 700;
    margin-bottom: 10px;
  }

  .listitem {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid #dcdfe6;
    position: relative;

    .count {
      display: inline-block;
      position: absolute;
      left: 210px;
      color: #a4a6aa;
      top: 50%;
      transform: translateY(-50%);
      font-size: 13px;
    }

    .all {
      height: 50px;
      line-height: 50px;
      padding-left: 13px;
    }

    .allactive {
      background: #e8f4ff;
      height: 50px;
      line-height: 50px;
      padding-left: 13px;
      border-left: 3px solid #4d9de7;
    }
  }
}
</style>