<template>
  <div  style="background-color: #f0f2f5; padding-bottom: 20px; min-height: 100%; ">
    <div style="padding: 20px; background-color: #fff;">
      订单列表
    </div>
    <div class="my-content"  >
    <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="title" show-overflow-tooltip width="200" />
      <el-table-column label="公司" align="center" prop="companyId" />
      <!--<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>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </div>
    <el-drawer :with-header="false" size="75%" :title="show.title" :visible.sync="show.open" append-to-body >
     <inquiryOrderDetails  ref="Details" />
   </el-drawer>
  </div>
</template>

<script>
import { getOrderUserPhone,getCompanyList,getInquiryOrderList,receiveOrder,refuseOrder } from "@/api/inquiryOrder";
import inquiryOrderDetails from '@/views/components/order/inquiryOrderDetails.vue';
export default {
  name: "inquiryOrder",
  components: { inquiryOrderDetails },
  data() {
    return {

      show:{
        title:"问诊详情",
        open:false,
          },
      companyList:[],
      startTime:null,
      // 遮罩层
      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: '已结束' },
      ],
      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;

      });
  },
  methods: {
    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;
      }

    },
    getDetails(id){
      this.queryParams.userId=id;
      this.getList();
    },
    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;
        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>
<style lang="scss" scoped>
.my-content{
    background-color: #fff;
    padding: 20px;
    margin: 20px;
    .my-title{
      margin-bottom: 20px;
      color: #524b4a;
      font-weight: bold;
    }

}
.el-descriptions-item__label.is-bordered-label{
  font-weight: normal;
}
.el-descriptions-item__content {
  max-width: 500px;
  min-width: 100px;
}
</style>
