<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companyId" >
        <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" filterable size="small">
          <el-option v-for="(option, index) in companyList" :key="index" :value="option.dictValue" :label="option.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工姓名" prop="companyUserName">
        <el-input
          v-model="queryParams.companyUserName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <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="推送时间">
        <el-date-picker v-model="createTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime"></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-tabs type="card" v-model="actName" @tab-click="handleClickX">
      <el-tab-pane label="全部" name="10"></el-tab-pane>
      <el-tab-pane v-for="(item,index) in writeStatusOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table   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="tempName"  width="150px" show-overflow-tooltip/>
      <el-table-column label="病人名称" align="center" prop="patientName" />
      <el-table-column label="病人电话" align="center" prop="patientPhone" />
      <el-table-column label="订单编号" align="center" prop="orderCode" width="170px"/>
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="所属员工" align="center" prop="companyUserName" />
      <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="planTime" width="150" />
      <el-table-column label="期次" align="center" prop="num">
        <template slot-scope="scope">
          <span>{{ scope.row.num }} / {{ scope.row.totalNum }} 期</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150px">
        <template slot-scope="scope">
          <el-button
             size="mini"
             type="text"
             @click="handledetails(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="followMsg(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>

       <el-dialog title="聊天记录" :visible.sync="msgDialog.open" width="80%"  >

         <msgDetails  ref="msgDetails" :msgDialogClose="msgDialogClose" /><strong></strong>
         <span slot="footer" class="dialog-footer">
           <el-button @click="msgDialog.open=false">取 消</el-button>
           <el-button type="primary" @click="msgDialog.open=false">确 定</el-button>
         </span>
       </el-dialog>
  </div>
</template>

<script>
import { getFollowList  } from "@/api/follow";
import followDetails from '@/views/components/follow/followDetails.vue';
import msgDetails from '@/views/components/msg/followMsgDetails.vue';
import { getCompanyList, } from "@/api/inquiryOrder";
export default {
  name: "Follow",
  components: { followDetails,msgDetails },
  data() {
    return {
      actName:"10",
      show:{
              title:"填写随访",
              open:false,
            },
      formJson:[],
      msgDialog:{
        open:false,
        title:"随访记录"
      },
      createTime:null,
      // 遮罩层
      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,
      },
      companyList:[],
      // 表单参数
      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;
    });
    getCompanyList().then(response => {
      this.companyList = response.list;
    });
    // this.getTemp();
  },
  methods: {
    followMsg(row){
      const userId = row.userId;
      const followDoctorId =row.doctorId;
      const doctorName = row.doctorName;
      const patientName = row.patientName;
      setTimeout(() => {
        this.$refs.msgDetails.getDetails(userId,followDoctorId,doctorName,patientName);
      }, 500);
      this.msgDialog.open = true;
    },
    msgDialogClose(){
      this.dialogVisible = false;
    },
    changeTime(){
        if(this.createTime!=null){
          this.queryParams.beginTime=this.createTime[0];
          this.queryParams.endTime=this.createTime[1];
        }else{
          this.queryParams.beginTime=null;
          this.queryParams.endTime=null;
        }
      },
    handleClickX(tab, event) {
    if(tab.name=="10"){
      this.queryParams.writeStatus=null;
    }else{
      this.queryParams.writeStatus=tab.name;
    }
     this.handleQuery();
   },
    handledetails(row){
        this.show.open=true;
        setTimeout(() => {
              this.$refs.Details.getDetails(row.followId,row.tempName);
        }, 1);
    },
    getTemp(){
      getAllFollowTempName().then(response => {
        this.tempOptions = response.rows;
      });
    },
    /** 查询随访管理列表 */
    getList() {
      this.loading = true;
      getFollowList(this.queryParams).then(response => {
        this.followList = response.data.list;
        this.total = response.data.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();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.createTime=null;
      this.queryParams.beginTime=null;
      this.queryParams.endTime=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>
