<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="所属部门" prop="deptId">
          <treeselect style="width:220px" v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择所属部门" />
      </el-form-item>
      <el-form-item label="订单号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          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="员工姓名" prop="companyUserNickName">
        <el-input
        style="width:220px"
          v-model="queryParams.companyUserNickName"
          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="sendName">
           <el-input
             v-model="queryParams.sendName"
             placeholder="请输入发卷人"
             clearable
             size="small"
             @keyup.enter.native="handleQuery"
           />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
         <el-select v-model="queryParams.orderType" placeholder="请选择" clearable size="small">
                 <el-option
                   v-for="dict in inquiryOrderOptions"
                   :key="dict.dictValue"
                   :label="dict.dictLabel"
                   :value="dict.dictValue"
                 />
               </el-select>
      </el-form-item>
      <el-form-item label="订单子类型" prop="inquirySubType">
         <el-select v-model="queryParams.inquirySubType" placeholder="请选择" clearable size="small">
                 <el-option
                   v-for="dict in inquirySubTypeOptions"
                   :key="dict.dictValue"
                   :label="dict.dictLabel"
                   :value="dict.dictValue"
                 />
               </el-select>
      </el-form-item>
      <el-form-item label="会员电话" prop="phone">
           <el-input
             v-model="queryParams.phone"
             placeholder="请输入会员电话"
             clearable
             size="small"
             @keyup.enter.native="handleQuery"
           />
       </el-form-item>



      <el-form-item label="咨询开始时间" prop="startTime">
         <el-date-picker v-model="startTime" 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="createTimeRange">
        <el-date-picker style="width:205.4px" clearable size="small" v-model="createTimeRange"  type="daterange"  value-format="yyyy-MM-dd"  start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
      </el-form-item>

      <el-form-item label="支付时间" prop="payTimeRange">
        <el-date-picker style="width:205.4px" clearable size="small" v-model="payTimeRange" type="daterange" value-format="yyyy-MM-dd"  start-placeholder="开始日期"  end-placeholder="结束日期"> </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="['store:inquiryOrder: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="['store:inquiryOrder: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 inquiryStatusOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" border :data="inquiryOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderSn" width="200px"/>
      <el-table-column label="问诊标题" align="center" prop="title" show-overflow-tooltip />
      <el-table-column label="病人名称" align="center" prop="patientName" />
      <el-table-column label="医生名称" align="center" prop="doctorName" />
      <el-table-column label="员工" align="center" prop="companyUserName" />
      <el-table-column label="公司" align="center" prop="companyName" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="inquiryStatusOptions" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="orderType">
            <template slot-scope="scope">
              <dict-tag :options="inquiryOrderOptions" :value="scope.row.orderType"/>
            </template>
      </el-table-column>
      <el-table-column label="订单子类型" align="center" prop="inquirySubType" width="90px">
            <template slot-scope="scope">
              <dict-tag :options="inquirySubTypeOptions" :value="scope.row.inquirySubType"/>
            </template>
      </el-table-column>
      <el-table-column label="支付类型" align="center" prop="payType" width="90px">
        <template slot-scope="scope">
          <dict-tag :options="inquiryPayOptions" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否支付" align="center" prop="isPay" >
        <template slot-scope="scope">
          <dict-tag :options="orOptions" :value="scope.row.isPay"/>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="money" />
      <el-table-column label="支付金额" align="center" prop="payMoney" />
      <el-table-column label="优惠金额" align="center" prop="discountMoney" />
      <el-table-column label="优惠卷发送人" align="center" prop="sendName" width="120px"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180"/>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180"/>
      <el-table-column label="审核人" align="center" prop="userName" width="180"/>
      <el-table-column label="问诊开始时间" align="center" prop="startTime" width="180" />
      <el-table-column label="结束时间" align="center" prop="finishTime" width="180" />
      <el-table-column label="是否评价" align="center" prop="isPing" >
        <template slot-scope="scope">
          <dict-tag :options="orOptions" :value="scope.row.isPing"/>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="pingStar" width="170px">
        <template slot-scope="scope" >
          <el-rate v-if="scope.row.pingStar!=null"
            v-model="scope.row.pingStar"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}">
          </el-rate>
        </template>

      </el-table-column>
      <el-table-column label="科室名称" align="center" prop="deptName" />
      <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)"
               icon="el-icon-share"
            >详情
        </el-button>
        <el-button
          v-if="scope.row.isPay==0"
          size="mini"
          type="text"
          @click="handlePayOrder(scope.row)"
          icon="el-icon-coin"
          v-hasPermi="['store:inquiryOrder:wxaCodeInquiryOrder']"
        >生成付款二维码
        </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">
     <inquiryOrderDetails  ref="Details" />
   </el-drawer>

    <el-dialog :title="payInquiryOrder.title" v-if="payInquiryOrder.open"  :visible.sync="payInquiryOrder.open" width="450px"  append-to-body>
      <div style="padding-bottom:15px;" >
        <img :src="codeImage" width="400px">
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="downloadImage(codeImage, payInquiryOrder.name+'.png')">下载二维码</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listinquiryOrder, getinquiryOrder, delinquiryOrder, addinquiryOrder, updateinquiryOrder, exportinquiryOrder,exportMessageFeedback,getWxaCodeInquiryOrderUnLimit } from "@/api/store/inquiryOrder";
import inquiryOrderDetails from '../components/inquiryOrderDetails.vue';
import { treeselect } from "@/api/company/companyDept";
import Treeselect from "@riophae/vue-treeselect";
import { getTask } from "@/api/common";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
        this.$refs.tree.filter(val);
      },
  },
  name: "ofast",
  components: { inquiryOrderDetails ,Treeselect},
  data() {
    return {
      // 部门树选项
      deptOptions: [],
	  createTimeRange:[],
	  payTimeRange:[],
      // 部门名称
      deptName: undefined,
      actName:"10",
      show:{
        title:"问诊详情",
        open:false,
      },
       startTime:null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问诊订单表格数据
      inquiryOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      codeImage:null,
      payInquiryOrder:{
        open:false,
        title:"付款二维码",
        name:null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSn: null,
        title: null,
        imgs: null,
        userId: null,
        patientId: null,
        orderType: null,
        money: null,
        payMoney: null,
        payType: null,
        isPay: null,
        doctorId: null,
        payTime: null,
        status: null,
        startTime: null,
        sTime:null,
        eTime:null,
        finishTime: null,
        isPing: null,
        pingStar: null,
        pingContent: null,
        departmentId: null,
        inquiryType: null,
        doctorName:null,
        patientName:null,
        sendName:null,
        phone:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      inquiryStatusOptions:[],
      inquiryTypeOptions:[],
      inquiryPayOptions:[],
      inquiryOrderOptions:[],
      inquirySubTypeOptions:[],
      orOptions:[],
    };
  },
  created() {
      this.getTreeselect();
      this.getDicts("sys_inquiry_status").then(response => {
          this.inquiryStatusOptions = response.data;
        });
      this.getDicts("sys_inquiry_type").then(response => {
          this.inquiryTypeOptions = response.data;
        });
      this.getDicts("sys_inquiry_pay").then(response => {
          this.inquiryPayOptions = response.data;
        });
      this.getDicts("sys_inquiry_order_type").then(response => {
          this.inquiryOrderOptions = response.data;
        });
      this.getDicts("sys_company_or").then(response => {
          this.orOptions = response.data;
        });
      this.getDicts("sys_inquiry_sub_type").then(response => {
        this.inquirySubTypeOptions = response.data;
      });
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    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.orderId);
        }, 1);
    },

    handlePayOrder(row){

      let loadingRock = this.$loading({
        lock: true,
        text: '生成二维码中~~请不要刷新页面！！',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      getWxaCodeInquiryOrderUnLimit(row.orderId).then(response => {
        this.codeImage=response.url
        this.payInquiryOrder.open=true;
        this.payInquiryOrder.name=row.title;
        loadingRock.close();
      }).finally(res=>{
        loadingRock.close();
      })
    },

    downloadImage(imageSrc, fileName) {
      const link = document.createElement('a');
      link.href = imageSrc;
      link.download = fileName || '付款二维码.png';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
    /** 查询问诊订单列表 */
    getList() {
      this.loading = true;
	  if(this.createTimeRange!=null&&this.createTimeRange.length==2){
	    this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
	  }
	  else{
	    this.queryParams.createTimeRange=null;
	  }
	  if(this.payTimeRange!=null&&this.payTimeRange.length==2){
	    this.queryParams.payTimeRange=this.payTimeRange[0]+"--"+this.payTimeRange[1]
	  }
	  else{
	    this.queryParams.payTimeRange=null;
	  }
      listinquiryOrder(this.queryParams).then(response => {
        this.inquiryOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderSn: null,
        title: null,
        imgs: null,
        userId: null,
        patientId: null,
        orderType: null,
        money: null,
        payMoney: null,
        payType: null,
        isPay: null,
        doctorId: null,
        createTime: null,
        payTime: null,
        status: null,
        startTime: null,
        finishTime: null,
        remark: null,
        isPing: null,
        pingStar: null,
        pingContent: null,
        departmentId: null,
        inquiryType: 2,
        doctorName:null,
        patientName:null,
        phone:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    change(){
      if(this.startTime!=null){
        this.queryParams.sTime=this.startTime[0];
        this.queryParams.eTime=this.startTime[1];
      }else{
        this.queryParams.sTime=null;
        this.queryParams.eTime=null;
      }

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
	  this.createTimeRange=null;
	  this.payTimeRange=null;
      this.startTime=null;
      this.queryParams.sTime=null;
      this.queryParams.eTime=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问诊订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getinquiryOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问诊订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateinquiryOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addinquiryOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$confirm('是否确认删除问诊订单编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delinquiryOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
		var that=this;
		if(this.createTimeRange!=null&&this.createTimeRange.length==2){
		  this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
		}
		else{
		  this.queryParams.createTimeRange=null;
		}
		if(this.payTimeRange!=null&&this.payTimeRange.length==2){
		  this.queryParams.payTimeRange=this.payTimeRange[0]+"--"+this.payTimeRange[1]
		}
		else{
		  this.queryParams.payTimeRange=null;
		}
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有问诊订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportinquiryOrder(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;
		if(this.createTimeRange!=null&&this.createTimeRange.length==2){
		  this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
		}
		else{
		  this.queryParams.createTimeRange=null;
		}
		if(this.payTimeRange!=null&&this.payTimeRange.length==2){
		  this.queryParams.payTimeRange=this.payTimeRange[0]+"--"+this.payTimeRange[1]
		}
		else{
		  this.queryParams.payTimeRange=null;
		}
      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>
