<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <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="prescribeType">
       <el-select v-model="queryParams.prescribeType" placeholder="处方类型" clearable size="small">
               <el-option
                 v-for="dict in prescribeType"
                 :key="dict.dictValue"
                 :label="dict.dictLabel"
                 :value="dict.dictValue"
               />
       </el-select>

      </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="医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          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="prescribeCode">
        <el-input
          v-model="queryParams.prescribeCode"
          placeholder="请输入处方单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="患者电话" prop="patientTel">
        <el-input
          v-model="queryParams.patientTel"
          placeholder="请输入患者电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="药品订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="药品订单状态" clearable size="small">
                <el-option
                  v-for="dict in orderStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
      </el-form-item>


        <el-form-item label="创建时间" prop="createTime">
           <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="change"></el-date-picker>
        </el-form-item>


        <el-form-item label="审核时间" prop="auditTime">
            <el-date-picker v-model="auditTime" 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:prescribe:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExportMessageFeedback"
          v-hasPermi="['his:prescribe:messageFeedbackExport']"
        >医疗信息反馈单</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <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 prescribeStatusOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table heiget="600" v-loading="loading" border :data="prescribeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="处方单编号" align="center" prop="prescribeCode" width="180px" />
      <el-table-column label="药品订单编号" align="center" prop="orderCode" width="180px" />
      <el-table-column label="处方类型" align="center" prop="prescribeType" >
            <template slot-scope="scope">
              <dict-tag :options="prescribeType" :value="scope.row.prescribeType"/>
            </template>
      </el-table-column>

      <el-table-column label="患者年龄" align="center" prop="patientAge" />
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="诊断" align="center" prop="diagnose" />
      <el-table-column label="医生名称" align="center" prop="doctorName" />
      <el-table-column label="药师名称" align="center" prop="doctorDrugName" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="prescribeStatusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="药品订单状态" align="center" prop="orderStatus" >
            <template slot-scope="scope">
              <dict-tag :options="orderStatusOptions" :value="scope.row.orderStatus"/>
            </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180" />
      <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="100px">
        <template slot-scope="scope">
           <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-drawer
        :with-header="false"
        size="75%"
         :title="show.title" :visible.sync="show.open">
     <prescribeDetails  ref="Details" />
   </el-drawer>
  </div>
</template>

<script>
import { listPrescribe, getPrescribe, delPrescribe, addPrescribe, updatePrescribe, exportPrescribe,exportMessageFeedback } from "@/api/his/prescribe";
import prescribeDetails from '../../components/his/prescribeDetails.vue';
import {allList}from "@/api/company/company";
import { getTask } from "@/api/common";
import {list as getAppMallOptions} from '@/api/course/coursePlaySourceConfig';
export default {
  name: "Prescribe",
  components: { prescribeDetails },
  data() {
    return {
      companyList:[],
      createTime:null,
      pjtUrl: process.env.VUE_APP_BASE_API+"/",
      show:{
              title:"订单详情",
              open:false,
            },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      auditTime:null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方表格数据
      prescribeList: [],
      orderStatusOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prescribeType: null,
        inquiryOrderId: null,
        storeOrderId: null,
        userId: null,
        patientId: null,
        prescribeCode: null,
        patientDescs: null,
        nowIllness: null,
        historyIllness: null,
        patientAge: null,
        patientName: null,
        weight: null,
        sTime:null,
        eTime:null,
        isHistoryAllergic: null,
        historyAllergic: null,
        liverUnusual: null,
        renalUnusual: null,
        isLactation: null,
        patientTel: null,
        patientGender: null,
        recordPic: null,
        prescribeImgUrl: null,
        auditReason: null,
        diagnose: null,
        doctorId: null,
        status: null,
        auditTime: null,
        auditSTime:null,
        auditETime:null,
        doctorName:null,
        orderStatus:null,
        appId:null
      },
       actName:"10",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      prescribeStatusOptions:[],
      prescribeType:[],
      orOptions:[],
      sexOptions:[],
      appMallOptions:[],
    };
  },
  created() {
    this.getAllCompany();
    this.getDicts("sys_prescribe_status").then(response => {
        this.prescribeStatusOptions = response.data;
      });
    this.getDicts("sys_prescribe_type").then(response => {
        this.prescribeType = response.data;
      });
    this.getDicts("sys_sex").then(response => {
        this.sexOptions = response.data;
      });
    this.getDicts("sys_company_or").then(response => {
        this.orOptions = response.data;
      });
    this.getDicts("sys_order_status").then(response => {
        this.orderStatusOptions = response.data;
      });

    this.getAppMallOptions();
    this.getList();
  },
  methods: {
    getAppMallOptions() {
      getAppMallOptions({pageNum:1,pageSize:100,isMall:1}).then(response => {
        this.appMallOptions = response.rows;
      })
    },
    getAllCompany() {
      allList().then(response => {
        this.companyList = response.rows;
      });
    },
    handleClickX(tab, event) {
     if(tab.name=="10"){
       this.queryParams.status=null;
     }else{
       this.queryParams.status=tab.name;
     }
      this.handleQuery();
    },
    handledetails(row){
         this.show.open=true;
         setTimeout(() => {
              this.$refs.Details.getDetails(row.prescribeId);
         }, 1);
     },
    /** 查询处方列表 */
    getList() {
      this.loading = true;
      listPrescribe(this.queryParams).then(response => {
        this.prescribeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    change(){
      if(this.createTime!=null){
        this.queryParams.sTime=this.createTime[0];
        this.queryParams.eTime=this.createTime[1];
      }else{
        this.queryParams.sTime=null;
        this.queryParams.eTime=null;
      }
      if(this.auditTime!=null){
        this.queryParams.auditSTime=this.auditTime[0];
        this.queryParams.auditETime=this.auditTime[1];
      }else{
        this.queryParams.auditSTime=null;
        this.queryParams.auditETime=null;
      }


    },
    // 表单重置
    reset() {
      this.form = {
        prescribeId: null,
        prescribeType: null,
        inquiryOrderId: null,
        storeOrderId: null,
        userId: null,
        patientId: null,
        prescribeCode: null,
        patientDescs: null,
        nowIllness: null,
        historyIllness: null,
        patientAge: null,
        patientName: null,
        weight: null,
        isHistoryAllergic: null,
        historyAllergic: null,
        liverUnusual: null,
        renalUnusual: null,
        isLactation: null,
        patientTel: null,
        patientGender: null,
        recordPic: null,
        prescribeImgUrl: null,
        auditReason: null,
        diagnose: null,
        doctorId: null,
        createTime: null,
        status: 0,
        auditTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.createTime=null;
      this.queryParams.sTime=null;
      this.queryParams.eTime=null;
      this.auditTime=null;
      this.queryParams.auditSTime=null;
      this.queryParams.auditETime=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.prescribeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处方";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prescribeId = row.prescribeId || this.ids
      getPrescribe(prescribeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处方";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.prescribeId != null) {
            updatePrescribe(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrescribe(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prescribeIds = row.prescribeId || this.ids;
      this.$confirm('是否确认删除处方编号为"' + prescribeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPrescribe(prescribeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
		var that=this;
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有处方数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPrescribe(queryParams);
        }).then(response => {
			if(response.code==200){
			that.msgSuccess(response.msg);
			that.taskId=response.data;
			that.time=setInterval(function(){
			  //查订单
			  getTask(that.taskId).then(res => {
				if(res.data.status==1){
				  that.exportLoading = false;
				  clearTimeout(that.time)
				  that.time=null;
				  that.download(res.data.fileUrl);
				}
			  });
			},10000);
		  }
        }).catch(() => {});
    },
     /** 导出按钮操作 */
     handleExportMessageFeedback() {
		 var that=this;
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有医疗信息反馈单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMessageFeedback(queryParams);
        }).then(response => {
			if(response.code==200){
			that.msgSuccess(response.msg);
			that.taskId=response.data;
			that.time=setInterval(function(){
			  //查订单
			  getTask(that.taskId).then(res => {
				if(res.data.status==1){
				  that.exportLoading = false;
				  clearTimeout(that.time)
				  that.time=null;
				  that.download(res.data.fileUrl);
				}
			  });
			},10000);
		  }
        }).catch(() => {});
    }
  }
};
</script>
