<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="问题" prop="question">
        <el-input v-model="queryParams.question" placeholder="请输入问题" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="科室名称" prop="departmentName">
        <el-input v-model="queryParams.departmentName" placeholder="请输入科室名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="科室id" prop="departmentNo">
        <el-input v-model="queryParams.departmentNo" placeholder="请输入科室id" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="身体部位标识" prop="bodyAreaType">
        <el-select v-model="queryParams.bodyAreaType" placeholder="请选择身体部位标识" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="身体部位名称" prop="bodyArerName">
        <el-input v-model="queryParams.bodyArerName" placeholder="请输入身体部位名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="问题类型" prop="questionType">
        <el-select v-model="queryParams.questionType" placeholder="请选择问题类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="问题类型名称" prop="questionTypeName">
        <el-input v-model="queryParams.questionTypeName" placeholder="请输入问题类型名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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:dz-question:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['system:dz-question:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="问题" align="center" prop="question" />
      <el-table-column label="科室名称" align="center" prop="departmentName" />
      <el-table-column label="科室id" align="center" prop="departmentNo" />
      <el-table-column label="身体部位标识" align="center" prop="bodyAreaType" />
      <el-table-column label="身体部位名称" align="center" prop="bodyArerName" />
      <el-table-column label="问题类型" align="center" prop="questionType" />
      <!-- <el-table-column label="问题类型名称" align="center" prop="questionTypeName" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <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:dz-question:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:dz-question:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :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="100px">
        <!-- <el-form-item label="科室名称" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入科室名称" />
        </el-form-item>
        <el-form-item label="科室id" prop="departmentNo">
          <el-input v-model="form.departmentNo" placeholder="请输入科室id" />
        </el-form-item> -->
        <el-form-item label="科室分类" prop="departmentName">
          <el-select v-model="departmentName" placeholder="请选择科室分类" @change="selectKs">
            <el-option
              v-for="item in toplist"
              :key="item.departcode"
              :label="item.name"
              :value="item.departcode + '+' + item.name">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.departcode }}</span>
            </el-option>            
          </el-select>
        </el-form-item>
        <el-form-item label="部位分类" prop="bodyArerName">
          <el-select v-model="bodyArerName" placeholder="请选择部位分类" @change="selectOne">
            <el-option
              v-for="item in typelist"
              :key="item.bodyAreaType"
              :label="item.bodyAreaName"
              :value="item.bodyAreaType + '+' + item.bodyAreaName">
              <span style="float: left">{{ item.bodyAreaName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.bodyAreaType }}</span>
            </el-option>            
          </el-select>
        </el-form-item>
        
        <el-form-item label="问题类型" prop="questionTypeName">
          <el-select v-model="questionTypeName" placeholder="请选择问题类型" @change="selectSType">
            <el-option
              v-for="item in arealist"
              :key="item.questionType"
              :label="item.questionTypeName"
              :value="item.questionType + '+' + item.questionTypeName">
              <span style="float: left">{{ item.questionTypeName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.questionType }}</span>
            </el-option>            
          </el-select>

        </el-form-item>
        <el-form-item label="问题" prop="question">
          <el-input v-model="form.question" placeholder="请输入问题" />
        </el-form-item>

        <!-- <el-form-item label="问题类型名称" prop="questionTypeName">
          <el-input v-model="form.questionTypeName" 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 { createDzQuestion, updateDzQuestion, deleteDzQuestion, getDzQuestion, getDzQuestionPage, exportDzQuestionExcel } from "@/api/system/dzQuestion";
// import { getKsTypePage} from "@/api/system/ksType";

import { getKsInfoDz} from "@/api/system/ksInfo";
import { getDzTypePageGroup,getDzTypeGroup } from "@/api/system/dzType";

export default {
  name: "DzQuestion",
  components: {
  },
  data() {
    return {
      departmentName: null,
      bodyArerName: null,
      questionTypeName:null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      areatotal: 0,
      // 科室信息列表
      arealist: [],
      // 总条数
      toptotal: 0,
      // 科室信息列表
      toplist: [],
      typetotal: 0,
      typelist: [],
      // 总条数
      total: 0,
      // 导诊管理列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryTopParams: {
        pageNo: 1,
        pageSize: 50
      },

      queryParams: {
        pageNo: 1,
        pageSize: 10,
        question: null,
        departmentName: null,
        departmentNo: null,
        bodyAreaType: null,
        bodyArerName: null,
        questionType: null,
        questionTypeName: null,
        createTime: [],
      },
      queryTypeParams: {
        pageNo: 1,
        pageSize: 50,
        bodyAreaType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        departmentName: [{ required: true, message: "科室分类不能为空", trigger: "blur" }],
        bodyArerName: [{ required: true, message: "部位分类不能为空", trigger: "blur" }],
        questionTypeName: [{ required: true, message: "问题分类不能为空", trigger: "blur" }],
        question: [{ required: true, message: "问题不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
    this.getTopList();
    this.getTypeList();
  },
  methods: {
    selectSType(value){
      this.form.questionType = value.split("+")[0];
      this.form.questionTypeName = value.split("+")[1];

      this.questionTypeName = value.split("+")[1];
    },

    selectOne(value) {     //change 触发事件
      this.form.bodyAreaType = value.split("+")[0];
      this.form.bodyArerName = value.split("+")[1];

      this.bodyAreaName1 = value.split("+")[1];

      this.queryTypeParams.bodyAreaType = value.split("+")[0];
      this.getAreaGroup();

      this.questionTypeName = "";
      this.form.questionTypeName = "";
    },
    selectKs(value) {     //change 触发事件
      this.form.departmentNo = value.split("+")[0];
      this.form.departmentName = value.split("+")[1];

      this.departmentName  = value.split("+")[1];
    }, 

    
    getAreaGroup() {
      // 执行查询
      getDzTypeGroup(this.queryTypeParams).then(response => {
        this.arealist = response.data.list;
        this.areatotal = response.data.total;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getDzQuestionPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询列表 */
    getTopList() {
      // 执行查询
      getKsInfoDz(this.queryTopParams).then(response => {
        this.toplist = response.data.list;
        this.toptotal = response.data.total;
      });
    },
    getTypeList() {
      // 执行查询
      getDzTypePageGroup(this.queryTypeParams).then(response => {
        this.typelist = response.data.list;
        this.typetotal = response.data.total;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        question: undefined,
        departmentName: undefined,
        departmentNo: undefined,
        bodyAreaType: undefined,
        bodyArerName: undefined,
        questionType: undefined,
        questionTypeName: undefined,
      };
      this.resetForm("form");
      this.departmentName = null;
      
      this.bodyArerName = null;

      this.questionTypeName = null;      
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加导诊管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      this.departmentName = row.departmentName;
      
      this.bodyArerName = row.bodyArerName;

      this.questionTypeName = row.questionTypeName;
      this.queryTypeParams.bodyAreaType = row.bodyAreaType;
      this.getAreaGroup();

      getDzQuestion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改导诊管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateDzQuestion(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createDzQuestion(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除导诊管理编号为"' + id + '"的数据项?').then(function() {
          return deleteDzQuestion(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有导诊管理数据项?').then(() => {
          this.exportLoading = true;
          return exportDzQuestionExcel(params);
        }).then(response => {
          this.$download.excel(response, '导诊管理.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
