<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="病人名称" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入病人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病人电话" prop="patientPhone">
        <el-input
          v-model="queryParams.patientPhone"
          placeholder="请输入病人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="填写状态" prop="writeStatus">
        <el-select v-model="queryParams.writeStatus" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in writeStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推送时间" prop="planTime">
        <el-date-picker v-model="planTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="change"></el-date-picker>
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:follow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="600" v-loading="loading" border :data="followList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板名称" align="center" prop="name"  width="120px" show-overflow-tooltip/>
      <el-table-column label="订单编号" align="center" prop="orderCode" width="170px"/>
      <el-table-column label="就诊人" align="center" prop="patientName" />
      <el-table-column label="推送状态" align="center" prop="pushStatus">
        <template slot-scope="scope">
          <dict-tag :options="pushStatusOptions" :value="scope.row.pushStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="填写状态" align="center" prop="writeStatus">
        <template slot-scope="scope">
          <dict-tag :options="writeStatusOptions" :value="scope.row.writeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="填写人" align="center" prop="writeUserName" width="120px"/>
      <el-table-column label="推送时间" align="center" prop="planTime" width="150" />
      <el-table-column label="用户昵称" align="center" prop="nickName" width="120px"/>
      <el-table-column label="医生名称" align="center" prop="doctorName" />
      <el-table-column label="用户电话" align="center" prop="phone" width="120px"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150px">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.writeStatus==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['his:follow:edit']"
          >填写</el-button>
          <el-button
             size="mini"
             type="text"
             @click="handledetails(scope.row)"
          >详情</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="800px" append-to-body >
      <div v-show="formJson != null&&formJson!=''" style="margin-left: 40px;">
       <div    :class="'questionnaire_'+index"   v-for="(item, index) in formJson"   :key="index" >
            <div class="flex-1" style="margin-bottom: 10px;">
              <span :class="item.require ? 'require' : 'noRequire'">{{index+1}}.{{item.question}}</span>
            <div>
                <div v-if="'1' == item.type" style="margin-top: 10px;">
                   <el-row v-for="(answerItem,i) in item.options"  :key="i">
                      <el-col :span="23" :offset="1"> <el-radio  v-model="item.answers" :label="item.options[i]"></el-radio></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="23" :offset="1">
                        <el-checkbox-group v-model="item.answers">
                           <el-checkbox :label="item.options[i]" ></el-checkbox>
                        </el-checkbox-group>
                      </el-col>
                    </el-row>
                </div>
                <div v-if="'3' == item.type" >
                    <el-row>
                      <el-col :span="23" :offset="1"><el-input v-model="item.answers"  type="textarea"></el-input></el-col>
                    </el-row>
                </div>
              </div>
            </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
       <el-drawer
            :with-header="false"
            size="75%"
             :title="show.title" :visible.sync="show.open">
         <followDetails  ref="Details" />
       </el-drawer>
  </div>
</template>

<script>
import { listFollow, getFollow, delFollow, addFollow, updateFollow, exportFollow } from "@/api/store/follow";
// import {getAllFollowTempName } from "@/api/his/followTemp";
import followDetails from '@/views/store/components/followDetails.vue';
export default {
  name: "Follow",
  components: { followDetails },
  data() {
    return {
      planTime:null,
      show:{
              title:"填写随访",
              open:false,
            },
      formJson:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      tempOptions:[],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 随访管理表格数据
      followList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 0待推送 1已推送 2 已取消字典
      pushStatusOptions: [],
      // 0未填写 1已填写字典
      writeStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: null,
        patientPhone: null,
        pushStatus: null,
        writeStatus: null,
        createTime: null,
        planTime:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_follow_push_status").then(response => {
      this.pushStatusOptions = response.data;
    });
    this.getDicts("sys_follow_write_status").then(response => {
      this.writeStatusOptions = response.data;
    });
    // this.getTemp();
  },
  methods: {
    handledetails(row){
            this.show.open=true;
            setTimeout(() => {
                 this.$refs.Details.getDetails(row.followId);
            }, 1);
        },
    // getTemp(){
    //   getAllFollowTempName().then(response => {
    //     this.tempOptions = response.rows;
    //   });
    // },
    /** 查询随访管理列表 */
    getList() {
      this.loading = true;
      listFollow(this.queryParams).then(response => {
        this.followList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        followId: null,
        tempId: null,
        patientId: null,
        patientName: null,
        patientPhone: null,
        userId: null,
        doctorId: null,
        totalNum: null,
        num: null,
        pushStatus: null,
        writeStatus: 0,
        planTime: null,
        formJson: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    change(){
      if(this.planTime!=null){
        this.queryParams.sTime=this.planTime[0];
        this.queryParams.eTime=this.planTime[1];
      }else{
        this.queryParams.sTime=null;
        this.queryParams.eTime=null;
      }

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.sTime=null;
      this.queryParams.eTime=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.followId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加随访管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const followId = row.followId || this.ids
      getFollow(followId).then(response => {
        this.form = response.data;
        if(this.form!=null&&this.form!=''&&this.form.writeStatus==0){
                  this.formJson=JSON.parse(this.form.formJson );
        }
        this.open = true;
        this.title = "修改随访管理";
      });
    },
    update(row){
      var that=this;
      this.$confirm('是否确定取消?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        var data={
       followId:row.followId,
       pushStatus:2
           }
        return updateFollow(data);
      }).then(() => {
        this.msgSuccess("操作成功");
        this.getList();
      }).catch(function() {});
    },
    /** 提交按钮 */
    submitForm() {
          if (this.form.followId != null) {
            this.form.formJson=JSON.stringify(this.formJson);
            updateFollow(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFollow(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const followIds = row.followId || this.ids;
      this.$confirm('是否确认删除随访管理编号为"' + followIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFollow(followIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有随访管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportFollow(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
