<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问题描述" prop="queTextOrPid">
        <el-input
          v-model="queryParams.queTextOrPid"
          placeholder="请输入问题描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="答案内容" prop="queResultText">
        <el-input
          v-model="queryParams.queResultText"
          placeholder="请输入答案内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题级别" prop="queLevel">
        <el-select v-model="queryParams.queLevel" placeholder="请选择问题级别" clearable size="small">
          <el-option
            v-for="dict in dict.type.que_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="启用或禁用" prop="enableOrDisable">
        <el-input
          v-model="queryParams.enableOrDisable"
          placeholder="请输入启用或禁用"
          clearable
          size="small"
          @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="['yiqing:question:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="questionList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="问题分组ID" prop="queGroupId"/>
      <el-table-column label="问题描述" align="center" prop="queTextOrPid"/>
      <el-table-column label="答案编号" align="center" prop="queResultNo"/>
      <el-table-column label="答案内容" align="center" prop="queResultText"/>
      <el-table-column label="问题级别" align="center" prop="queLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.que_level" :value="scope.row.queLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="启用或禁用" align="center" prop="enableOrDisable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_enabled_disable" :value="scope.row.enableOrDisable"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <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="['yiqing:question:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['yiqing:question:add']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yiqing:question:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改疫情登记问题对话框 -->
    <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="问题分组ID" prop="queGroupId">
          <el-input v-model="form.queGroupId" placeholder="请输入问题分组ID"/>
        </el-form-item>
        <el-form-item label="问题描述" prop="queTextOrPid">
          <treeselect v-model="form.queTextOrPid" :options="questionOptions" :normalizer="normalizer"
                      placeholder="请选择问题描述"/>
        </el-form-item>
        <el-form-item label="答案编号" prop="queResultNo">
          <el-input v-model="form.queResultNo" placeholder="请输入答案编号"/>
        </el-form-item>
        <el-form-item label="答案内容" prop="queResultText">
          <el-input v-model="form.queResultText" placeholder="请输入答案内容"/>
        </el-form-item>
        <el-form-item label="问题级别" prop="queLevel">
          <el-select v-model="form.queLevel" placeholder="请选择问题级别">
            <el-option
              v-for="dict in dict.type.que_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="启用或禁用" prop="enableOrDisable">
          <el-select v-model="form.enableOrDisable" placeholder="请选择问题级别">
            <el-option
              v-for="dict in dict.type.sys_enabled_disable"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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 { addQuestion, delQuestion, getQuestion, listQuestion, updateQuestion } from '@/api/yiqing/question'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  export default {
    name: 'Question',
    dicts: ['que_level', 'is_delete', 'sys_enabled_disable'],
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 疫情登记问题表格数据
        questionList: [],
        // 疫情登记问题树选项
        questionOptions: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          queGroupId: null,
          queTextOrPid: null,
          queResultNo: null,
          queResultText: null,
          queLevel: null,
          enableOrDisable: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询疫情登记问题列表 */
      getList() {
        this.loading = true
        listQuestion(this.queryParams).then(response => {
          this.questionList = this.handleTree(response.data, 'id', 'queTextOrPid')
          this.loading = false
        })
      },
      /** 转换疫情登记问题数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children
        }
        return {
          id: node.id,
          label: node.queTextOrPid,
          children: node.children
        }
      },
      /** 查询疫情登记问题下拉树结构 */
      getTreeselect() {
        listQuestion().then(response => {
          this.questionOptions = []
          const data = { id: 0, queTextOrPid: '顶级节点', children: [] }
          data.children = this.handleTree(response.data, 'id', 'queTextOrPid')
          this.questionOptions.push(data)
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          queGroupId: null,
          queTextOrPid: null,
          queResultNo: null,
          queResultText: null,
          queLevel: null,
          enableOrDisable: null,
          isDelete: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset()
        this.getTreeselect()
        if (row != null && row.id) {
          this.form.queTextOrPid = row.id
        } else {
          this.form.queTextOrPid = 0
        }
        this.open = true
        this.title = '添加疫情登记问题'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.getTreeselect()
        if (row != null) {
          this.form.queTextOrPid = row.id
        }
        getQuestion(row.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) {
              updateQuestion(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addQuestion(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除疫情登记问题编号为"' + row.id + '"的数据项？').then(function() {
          return delQuestion(row.id)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      }
    }
  }
</script>
