<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="订单号 患者姓名" prop="orderSn">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入订单号 患者姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyId" >
            <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable 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="companyUserNickName">
               <el-input  v-model="queryParams.companyUserNickName"  placeholder="请输入员工名称" clearable size="small" @keyup.enter.native="handleQuery"/>
          </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-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 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" />
      <el-table-column label="患者姓名" align="center" width="120" >
        <template slot-scope="scope">
          <span>{{ JSON.parse(scope.row.patientJson).patientName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="患者电话" align="center" width="120" >
        <template slot-scope="scope">
          <span>{{ JSON.parse(scope.row.patientJson).mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="患者性别" align="center" width="80" >
        <template slot-scope="scope">
          <span>{{ JSON.parse(scope.row.patientJson).sex==1?'男':'女' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="患者年龄" align="center" width="80" >
        <template slot-scope="scope">
          <span>{{ JSON.parse(scope.row.patientJson).age }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status"  width="80" >
        <template slot-scope="scope">
          <dict-tag :options="inquiryStatusOptions" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" width="150" >
          <template slot-scope="scope">
            <div style="display: flex; align-items: center;">
              <dict-tag :options="inquiryTypeOptions" :value="scope.row.inquiryType" />
              <span style="margin: 0 5px;"></span>
              <dict-tag :options="inquiryOrderOptions" :value="scope.row.orderType" />
            </div>
          </template>
      </el-table-column>
      <el-table-column label="订单子类型" align="center" prop="inquirySubType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="inquirySubTypeOptions" :value="scope.row.inquirySubType" />
        </template>
      </el-table-column>
      <el-table-column label="病情描述" align="center" prop="title" show-overflow-tooltip width="200" />
      <el-table-column label="公司" align="center" prop="companyName" />
      <el-table-column label="员工" align="center" prop="nickName" />
      <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="createTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180px">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.status == 2"
            type="success"
            plain
            round
            size="medium"
            @click="handleReceiveOrder(scope.row)"
          >接单</el-button>
          <el-button
          v-if="scope.row.status == 2"
            type="danger"
            plain
            round
            size="medium"
            @click="handleRefuseOrder(scope.row)"
          >拒单</el-button>
          <el-button
              size="mini"
              type="text"
              @click="handledetails(scope.row)"
                icon="el-icon-share"
            >详情
          </el-button>
          <el-button
              v-if="scope.row.status==3"
              size="mini"
              type="text"
              @click="handleContinue(scope.row)"
                icon="el-icon-share"
            >继续问诊
          </el-button>
          <el-button
            v-if="scope.row.status==1"
            size="mini"
            type="text"
            @click="handleCloseOrder(scope.row)"
            icon="el-icon-share"
          >关闭订单
          </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-drawer :with-header="false" size="75%" :title="show.title" :visible.sync="show.open">
     <inquiryOrderDetails  ref="Details" @closeDetails="closeDetails"/>
   </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 { getOrderUserPhone,getCompanyList,getInquiryOrderList,receiveOrder,refuseOrder,closeOrder } from "@/api/inquiryOrder";
import inquiryOrderDetails from '../../components/order/inquiryOrderDetails.vue';
import msgDetails from '@/views/components/msg/followMsgDetails.vue';
import way from '@/utils/way.js';
export default {
  name: "inquiryOrder",
  components: { inquiryOrderDetails ,msgDetails},
  data() {
    return {

      show:{
        title:"问诊详情",
        open:false,
          },
      companyList:[],
      startTime:null,
      msgDialog:{
        open:false,
        title:"聊天记录"
      },
      // 遮罩层
      loading: true,
      actName:"10",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      inquirySubTypeOptions:[],
      createTimeRange:[],
      payTimeRange:[],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问诊订单表格数据
      inquiryOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      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,
        doctorName:null,
        patientName:null,
        sendName:null,
      },
      // 表单参数
      form: {},
      receiveOrderForm:{},
      refuseOrderForm:{},
      // 表单校验
      rules: {
      },
      inquiryStatusOptions:[
      { dictValue: "2", dictLabel: '待接诊' },
      { dictValue: "3", dictLabel: '问诊中' },
      { dictValue: "4", dictLabel: '已结束' },
      { dictValue: "1", dictLabel: '待支付' },
      { dictValue: "-1", dictLabel: '已取消' },
      ],
      inquiryTypeOptions:[],
      inquiryPayOptions:[],
      inquiryOrderOptions:[],
      orOptions:[],
    };
  },

  created() {
      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;
      });
      getCompanyList().then(response => {
        this.companyList = response.list;

      });
    this.getList();
  },
  methods: {
    closeDetails(){
      this.show.open=false;
    },
    handleContinue(){
      way.$emit('open',1);
    },
    handleCloseOrder(row){
      console.log(row)
      closeOrder(row.orderId).then(response => {
        this.$message({
          message: "关闭成功",
          type: "success"
        });
        this.getList();
      })
    },
    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;
    },
    getMappedValue(key, options) {
      return options[key];
    },
    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;
      }

    },
    handleReceiveOrder(row){
      const orderId = row.orderId
      this.receiveOrderForm.orderId = orderId;
      console.log(this.receiveOrderForm)
      this.$confirm('确认接单吗？', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return receiveOrder(this.receiveOrderForm);
        }).then(() => {
          this.getList();
          this.msgSuccess("接单成功");
        }).catch(() => {});
    },

    handleRefuseOrder(row){
      const orderId = row.orderId
      this.refuseOrderForm.orderId = orderId;
      this.$confirm('确认拒单吗？', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return refuseOrder(this.refuseOrderForm);
        }).then(() => {
          this.getList();
          this.msgSuccess("拒单成功");
        }).catch(() => {});
    },

    handledetails(row){

        this.show.open=true;
        setTimeout(() => {
              this.$refs.Details.getDetails(row.orderId);
        }, 1);
    },
    /** 查询问诊订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.isReceive = 1;
      getInquiryOrderList(this.queryParams).then(response => {
        this.inquiryOrderList = response.data.list;
        console.log(this.inquiryOrderList)
        this.total = response.data.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: 1,
        doctorName:null,
        patientName:null,
        sTime:null,
        eTime:null,
      };

      this.resetForm("form");

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.createTimeRange=null;
      this.payTimeRange=null;
      this.startTime=null;
      this.queryParams.keyword=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 = "修改问诊订单";
      });
    },
  handleClickX(tab, event) {
   if(tab.name=="10"){
     this.queryParams.status=null;
   }else{
     this.queryParams.status=tab.name;
   }
    this.handleQuery();
  },
    /** 提交按钮 */
    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(() => {});
    },

  }
};
</script>
