<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="副标题" prop="secondName">
        <el-input
          v-model="queryParams.secondName"
          placeholder="请输入副标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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="['his:followTemp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['his:followTemp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['his:followTemp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:followTemp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="followTempList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="副标题" align="center" prop="secondName" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
       <el-table-column label="创建时间" align="center" prop="createTime" />
      <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="['his:followTemp:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['his:followTemp: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-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal ="false">

      <el-row>
        <el-col :span="3">
              <div style="margin-bottom: 20px;"> <el-button type="primary" @click='addQuestionnaire("1")' icon="el-icon-edit">单选题</el-button> </div>
              <div style="margin-bottom: 20px;"> <el-button type="primary" @click='addQuestionnaire("2")' icon="el-icon-edit">多选题</el-button></div>
              <div style="margin-bottom: 20px;"> <el-button type="primary" @click='addQuestionnaire("3")' icon="el-icon-edit">填空题</el-button> </div>
        </el-col>
        <el-col :span="21">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
            <el-row>
              <el-col :span="12">
                <el-form-item label="模板名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入模板名称" />
                </el-form-item>
              </el-col>
               <el-col :span="12">
                 <el-form-item label="副标题" prop="secondName">
                   <el-input v-model="form.secondName" placeholder="请输入副标题" />
                 </el-form-item>
               </el-col>
            </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="form.sort"  :min="0" label="请输入排序"></el-input-number>
                  </el-form-item>
                </el-col>
                 <el-col :span="12">
                   <el-form-item label="状态" prop="status">
                     <el-radio-group v-model="form.status">
                       <el-radio
                         v-for="dict in statusOptions"
                         :key="dict.dictValue"
                         :label="parseInt(dict.dictValue)"
                       >{{dict.dictLabel}}</el-radio>
                     </el-radio-group>
                   </el-form-item>
                 </el-col>
              </el-row>

      <div style="height: 500px; overflow-y: auto;">
        <div style="height: 100%;">
          <div class="containerx">
            <span class="dashed-line"></span>
            <span class="centered-text">   内容区域，请从左侧栏选择，编辑填充</span>
            <span class="dashed-line"></span>
          </div>
             <el-form-item label="模板内容" prop="formJson">
           <div v-show="list.length != 0">
             <div    :class="'questionnaire_'+index"   v-for="(item, index) in list"   :key="index" >
                 <div class="flex-1">
                   <el-row>
                     <el-col :span="1"><span :class="item.require ? 'require' : 'noRequire'">{{index+1}}.</span></el-col>
                     <el-col :span="12"><input class="input-border" type="text" v-model="item.question" placeholder="请输入标题" /></el-col>
                     <el-col :span="1" :offset="8"><i class="el-icon-delete-solid" @click="delQuestionnaire(index)" ></i></el-col>
                   </el-row>
                 <div>
                     <!-- 判断为单选题型 -->
                     <div v-if="'1' == item.type">
                        <el-row v-for="(answerItem,i) in item.options"  :key="i">
                           <el-col :span="1"> <input type="radio" :name='"answerchoice["+index+"]"' class="answerchoice"/></el-col>
                           <el-col :span="8"> <input  class="input-border" type="text"  v-model="item.options[i]"/></el-col>
                           <el-col :span="1" :offset="12"><i class="el-icon-delete"  @click='delAanwer("radio",index,i)' ></i></el-col>
                        </el-row>
                        <el-row>
                          <el-col :span="4">  <el-link type="primary" class="el-icon-plus" :underline="false" @click='addAanwer("radio",index,i)'>添加单个选项</el-link></el-col>
                          <el-col :span="1">    <el-checkbox v-model="item.require">必选</el-checkbox> </el-col>
                        </el-row>
                     </div>
                     <!-- 判断为多选题型 -->
                     <div v-if="'2' == item.type">
                         <el-row v-for="(answerItem,i) in item.options"  :key="i">
                            <el-col :span="1">   <input type="checkbox" name="answer" class="answerchoice" /></el-col>
                            <el-col :span="8">   <input class="input-border" type="text"  v-model="item.options[i]" /></el-col>
                            <el-col :span="1" :offset="12"> <i  class="el-icon-delete"  @click='delAanwer("checkbox",index,i)' ></i></el-col>
                         </el-row>
                         <el-row>
                           <el-col :span="4">  <el-link type="primary" class="el-icon-plus" :underline="false" @click='addAanwer("radio",index,i)'>添加单个选项</el-link></el-col>
                           <el-col :span="1">    <el-checkbox v-model="item.require">必选</el-checkbox> </el-col>
                         </el-row>
                     </div>
                     <!-- 判断为填空题型 -->
                     <div v-if="'3' == item.type">
                         <el-row>
                           <el-col :span="11" :offset="1">    <el-input type="textarea"  :rows="2"   :disabled="true"/></el-col>
                         </el-row>
                         <el-row>
                           <el-col :span="4" >    <el-checkbox v-model="item.require" style="margin-left: 6px;">必选</el-checkbox> </el-col>
                         </el-row>
                     </div>
                   </div>
                 </div>
             </div>
           </div>
             </el-form-item>
      </div>
        </div>
           </el-form>
        </el-col>
      </el-row>
            <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 { listFollowTemp, getFollowTemp, delFollowTemp, addFollowTemp, updateFollowTemp, exportFollowTemp } from "@/api/his/followTemp";

export default {
  name: "FollowTemp",
  data() {

    return {
     list: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 随访模板表格数据
      followTempList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        secondName: null,
        status: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ],
        secondName: [
          { required: true, message: "副标题不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_company_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询随访模板列表 */
    getList() {
      this.loading = true;
      listFollowTemp(this.queryParams).then(response => {
        this.followTempList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tempId: null,
        name: null,
        secondName: null,
        formJson: null,
        sort: null,
        status: 1,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.list=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tempId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加随访模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tempId = row.tempId || this.ids
      getFollowTemp(tempId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改随访模板";
        if(this.form.formJson!=null){
           this.list=JSON.parse(this.form.formJson );
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.list==null||this.list==''){
         this.msgError("模板内容不能为空");
         return;
      }
      this.form.formJson=JSON.stringify(this.list);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tempId != null) {
            updateFollowTemp(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFollowTemp(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tempIds = row.tempId || this.ids;
      this.$confirm('是否确认删除随访模板编号为"' + tempIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFollowTemp(tempIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有随访模板数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportFollowTemp(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    addQuestionnaire(type, index, operation) {
      // 添加单选题
      if (type == '1') {
        var singleChoice = {
          question: '请输入问题',
          options: ['选项1', '选项2'],
          type: '1',
          // sort: this.list.length + 1,
          require:false,
        }
        // 在当前题下方添加
        if (index != undefined && undefined == operation) {
          this.list.splice(index + 1, 0, 1)
        }
          this.list.push(singleChoice)
      }
      // 添加多选题
      else if (type == '2') {
        var mutipleChoice = {
          question: '请输入问题',
          options: ['选项1', '选项2'],
          type: '2',
          // sort: this.list.length + 1
          require:false,
        }
        // 在当前题下方添加
        if (index != undefined && undefined == operation) {
          this.list.splice(index + 1, 0, mutipleChoice)
        }
          this.list.push(mutipleChoice)
      }
      // 添加多选题 end
      else if (type == '3') {
        var scoring = {
          question: '请输入问题',
          options: null,
          type: '3',
          // sort: this.list.length + 1
          require:false,
        }
        // 在当前题下方添加
        if (index != undefined && undefined == operation) {
          this.list.splice(index + 1, 0, scoring)
        }
          this.list.push(scoring)
      }
    },


    //删除一题
    delQuestionnaire(id) {
      this.list.splice(id, 1)
    },

    //添加一项答案
    addAanwer(type, index, option) {
      var options = this.list[index].options.length + 1
      this.list[index].options.push('选项' + options)
    },

    //删除一项答案
    delAanwer(type, index, option) {
      if (this.list[index].options.length > 2) {
        this.list[index].options.splice(option, 1)
      } else {
        this.msgError("至少留下两项");
      }
    }
  }
};
</script>
<style>
 .btn-bgWhite {
   border: 1px solid #999999;
   background: #fff;
   border-radius: 2px;
 }
 .btn-bgWhite:hover,
 .btn-bgBlue:hover,
 .btn-bgYello:hover {
   background: #333;
   color: #fff;
 }
 .btn-bgBlue {
   background: #3582f8;
   border-radius: 2px;
   color: #fff;
 }
 .btn-bgYello {
   background: #e7a42f;
   border-radius: 2px;
   color: #fff;
 }
 .text-right {
   text-align: right;
 }
 .text-left {
   text-align: left;
 }
 .form-control {
   /* height: 40px; */
   background: #ffffff;
   border: 1px solid #d8d8d8;
   border-radius: 3px;
   box-shadow: none;
   margin: 15px;
   padding: 5px;
 }
 input:focus {
   outline: none;
 }
 .input-group-fan .form-control {
   margin: 0;
   padding: 10px 15px;
 }
 .input-group-fan select {
   display: block;
   height: 40px;
   width: 330px;
   border: 1px solid #d8d8d8;
   padding: 0 10px;
   border-radius: 3px;
 }
 .input-group-fan select option {
   padding: 5px;
 }
 .input-group-fan label {
   font-size: 14px;
   color: #333333;
 }
 .input-group-fan {
   margin: 15px 0;
   width: 100%;
 }
 .input-group-fan input {
   float: none !important;
   width: 500px !important;
   display: block !important;
 }
.input-border {
   border: 1px dashed #eee;
   margin-bottom: 10px !important;
   padding: 5px;
   width: 400px;
 }
 .require:before {
     content: '* ';
     color: red;
 }
 .noRequire:before{
   content: '* ';
   color: white;
 }
 .answerchoice{
    margin-left: 6px;
 }

.containerx {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dashed-line {
  flex-grow: 1;
  border-top: 1px dashed #000;
}

.centered-text {
  margin: 0 10px;
  color: #07a8ff;
}
</style>
