<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <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="deliverySn">
        <el-input
          v-model="queryParams.deliverySn"
          placeholder="请输入快递单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
         <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable size="small" >
         <el-option
                v-for="item in PayOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="收货人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入收货人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          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="orderType">
        <el-select v-model="queryParams.orderType" placeholder="状态" clearable size="small">
                <el-option
                  v-for="dict in orderTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
      </el-form-item>
      <el-form-item label="物流状态" prop="deliveryStatus">
         <el-select   v-model="queryParams.deliveryStatus" placeholder="请选择物流状态" clearable size="small" >
         <el-option
                v-for="item in deliveryStatusOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.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="changeTime"></el-date-picker>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
           <el-date-picker v-model="payTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="changePayTime"></el-date-picker>
    </el-form-item>
    <el-form-item label="发货时间" prop="deliverySendTime">
           <el-date-picker v-model="deliverySendTime" 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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-if="isExport"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>
    <el-tabs type="card" v-model="actName" @tab-click="tabClick">
      <el-tab-pane label="全部订单" name="0"></el-tab-pane>
      <el-tab-pane v-for="(item,index) in orderOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderCode" width="180px"/>
      <el-table-column label="店铺" align="center" prop="storeName" />
      <el-table-column label="就诊人" align="center" prop="patientName" />
      <el-table-column label="应收金额" align="center" prop="payPrice" />
      <el-table-column label="实收金额" align="center" prop="payMoney" />
      <el-table-column label="支付方式" align="center" prop="payType" >
        <template slot-scope="scope">
              <dict-tag :options="PayOptions" :value="scope.row.payType"/>
         </template>
      </el-table-column>
      <el-table-column label="所属公司" align="center" prop="companyName" width="180" />
      <el-table-column label="员工" align="center" prop="companyUserName" width="180" />
      <el-table-column label="下单时间" align="center" prop="createTime" width="180" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180" />
      <el-table-column label="订单状态" align="center" prop="status" >
        <template slot-scope="scope">
              <dict-tag :options="orderOptions" :value="scope.row.status"/>
          </template>
      </el-table-column>
      <el-table-column label="物流状态" align="center" prop="deliveryStatus" >
       <template slot-scope="scope">
             <dict-tag :options="deliveryStatusOptions" :value="scope.row.deliveryStatus"/>
        </template>
      </el-table-column>
      <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">
     <storeOrderDetails  ref="Details" />
   </el-drawer>
  </div>
</template>

<script>
import { getStoreOrderById, getMyStoreOrderList} from "@/api/storeOrder";
import storeOrderDetails from '../../components/order/storeOrderDetails.vue';
import { getToken } from "@/utils/auth";
export default {
  name: "Order",
  components: { storeOrderDetails },
  data() {
    return {
      isExport:false,
      actName:"10",
      show:{
              title:"订单详情",
              open:false,
            },

      importExpress: {
              // 是否显示弹出层
              open: false,
              // 弹出层标题
              title: "",
              // 是否禁用上传
              isUploading: false,
              // 是否更新已经存在的用户数据
              updateSupport: 0,
              // 设置上传的请求头部
              headers: { Authorization: "Bearer " + getToken() },
              // 上传的地址
              url: process.env.VUE_APP_BASE_API + "/store/storeOrder/importExpress"
            },
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/store/storeOrder/importData"
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      createTime:null,
      payTime:null,
      deliverySendTime:null,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isDoctor:1,
        storeId: null,
        orderCode: null,
        userId: null,
        userName: null,
        userPhone: null,
        userAddress: null,
        cartId: null,
        totalNum: null,
        totalPrice: null,
        payPrice: null,
        payMoney: null,
        isPay: null,
        payTime: null,
        payType: null,
        status: null,
        refundStatus: null,
        refundImg: null,
        refundExplain: null,
        refundTime: null,
        refundReason: null,
        refundMoney: null,
        deliveryCode: null,
        deliveryName: null,
        deliverySn: null,
        isDel: null,
        costPrice: null,
        verifyCode: null,
        shippingType: null,
        isChannel: null,
        isPrescribe: null,
        prescribeId: null,
        finishTime: null,
        patientName: null,
        doctorName: null,
        sTime:null,
        eTime:null,
        paysTime:null,
        payeTime:null,
        deliveryTime: null,
        tuiMoney: null,
        tuiMoneyStatus: null,
        tuiUserId: null,
        orderCreateType: null,
        companyUserName:null,
        companyName:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
       PayOptions:[],
       orderOptions:[],
       payStatusOptions:[],
       refundOptions:[],
       channelOptions:[],
       orderTypeOptions:[],
       tuiOptions:[],
       orOptions:[],
       storeOPtions:[],
       deliveryPayStatusOptions:[],
       deliveryStatusOptions:[],
    };
  },
  created() {
    this.getDicts("sys_store_pay_type").then(response => {
      this.PayOptions = response.data;
    });
    this.getDicts("sys_store_order_type").then(response => {
      this.orderTypeOptions = response.data;
    });
    this.getDicts("sys_order_status").then(response => {
      this.orderOptions = response.data;
    });
     
    this.getDicts("sys_store_order_delivery_status").then(response => {
      this.deliveryStatusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    handledetails(row){
      this.show.open=true;
      setTimeout(() => {
            this.$refs.Details.getDetails(row.orderId,row.nickName,row.storeName);
      }, 1);
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      getMyStoreOrderList(this.queryParams).then(response => {
        this.orderList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    tabClick(tab, event) {
      if(tab.name=="0"){
        this.queryParams.status=null;
      }else{
        this.queryParams.status=tab.name;
      }
      this.handleQuery();
    },
    changeTime(){
      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.deliverySendTime!=null){
        this.queryParams.deliverySendsTime=this.deliverySendTime[0];
        this.queryParams.deliverySendeTime=this.deliverySendTime[1];
      }else{
        this.queryParams.deliverySendsTime=null;
        this.queryParams.deliverySendeTime=null;
      }

    },
    changePayTime(){
      if(this.payTime!=null){
        this.queryParams.sTime=this.payTime[0];
        this.queryParams.eTime=this.payTime[1];
      }else{
        this.queryParams.paysTime=null;
        this.queryParams.payeTime=null;
      }

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
       this.createTime=null;
      this.queryParams.sTime=null;
      this.queryParams.eTime=null;
       this.payTime=null;
      this.queryParams.paysTime=null;
      this.queryParams.payeTime=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
