<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="下单医院" prop="hospitalId">
        <el-select v-model="queryParams.hospitalId" filterable placeholder="请选择下单医院" size="small" clearable style="width: 100%">
          <el-option v-for="(v,i) in shopList" :key="i" :label="v.name" :value="v.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" maxlength="30" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单手机号" prop="userMobile">
        <el-input v-model="queryParams.userMobile" placeholder="请输入下单手机号" maxlength="11" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceName">
        <el-input v-model="queryParams.serviceName" placeholder="请输入服务名称" maxlength="30" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单时间" prop="">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" 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-radio-group size="mini" v-model="queryParams.status" style="margin-bottom: 30px;" @input="handleQuery">
          <el-radio-button v-for="(v,i) in queryStatusList" :key="i" :label="v.code">{{v.value}}</el-radio-button>
        </el-radio-group>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleExport">导出订单</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="商家ID" align="center" prop="businessId" /> -->
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="销售价格" align="center" prop="sellingPrice" />
      <el-table-column label="下单数量" align="center" prop="orderNum" />
      <el-table-column label="单位" align="center" prop="serviceUnit" width="60">
        <template slot-scope="{row}">
          <div v-for="(v, i) in serviceUnitList" :key="i" v-if="v.code==row.serviceUnit">{{v.value}}</div>
        </template>
      </el-table-column>
      <el-table-column label="应付金额" align="center" prop="totalPrice">
        <template slot-scope="{row}">
          {{row.totalPrice||"--"}}
        </template>
      </el-table-column>
      <el-table-column label="下单手机号" align="center" prop="userMobile" />
      <el-table-column label="下单医院" align="center" prop="hospitalName" />
      <el-table-column label="所在科室" align="center" prop="hospitalDepartmentName" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="{row}">
          <div v-for="(v,i) in statusList" :key="i" :type="v.tag" v-if="row.status==v.code">{{v.value}}</div>
          <!-- <el-tag v-if="row.orderStatus==10">待使用</el-tag>
          <el-tag v-if="row.orderStatus==20&&row.refundStatus!=2&&row.refundStatus!=1" type="success">已完成</el-tag>
          <el-tag v-if="row.orderStatus==30&&row.refundStatus==2" type="info">已退款</el-tag>
          <el-tag v-if="row.orderStatus==20&&row.refundStatus==1" type="info">已完成（有退款）</el-tag>
          <el-tag v-if="row.orderStatus==0" type="danger">待支付</el-tag>
          <el-tag v-if="row.orderStatus==30&&row.refundStatus!=2" type="info">已取消</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="140">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="goDetails(scope.row)">查看</el-button>
          <!-- <el-button size="mini" type="text" v-if="scope.row.reservationStatus&&scope.row.orderType==1&&!scope.row.reservationTime" @click="openDialog(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-dialog title="配置预约时间" :visible.sync="dialogVisible" width="650px" @close="close">
      <el-form :model="reserveForm" label-width="100px" size="small">
        <el-form-item label="选择日期" required>
          <el-select v-model="reserveForm.reservationDate" placeholder="请选择日期" style="width: 100%" @change="reservationDateChange">
            <el-option v-for="(v, i) in reserveData.reservationDateList" :key="i" :disabled="!v.canReservation" :value="v.reservationDate" :label="v.reservationDate"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择场次" required v-if="reserveData.reservationDateList&&reserveData.reservationDateList.length > 0">
          <el-select v-model="reserveForm.reservationTime" placeholder="请选择场次" style="width: 100%">
            <el-option v-for="(v, i) in reserveData.reservationDateList[reservationDateIndex].reservationTimeList" :key="i" :disabled="!v.openStatus" :value="v.timeAll" :label="v.timeAll"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { businessProductOrderPage, businessProductOrderChange, reservation } from '@/api/order.js'
import { hospitalSimpleList } from "@/api/hospital.js"
import { orderPage, orderEnum } from "@/api/hospitalOrder.js"
import { serviceProductEnum } from "@/api/service.js"
export default {
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: undefined,
        orderNumber: undefined,
        userPhone: undefined,
        businessProductName: undefined,
        // orderStatus: null,
        orderType: null,
        queryStatus: undefined
      },
      daterangeCreateTime: [],
      statusList: [],
      queryStatusList: [
        { value: '全部', code: undefined },
        // { label: '待确认', value: 10 },
        // { label: '待服务', value: 20 },
        // { label: '服务中', value: 40 },
        // { label: '已完成', value: 0 },
        // { label: '已退款', value: 0 },
        // { label: '已取消', value: 30 },
      ],
      serviceUnitList: [],
      tabPosition: 0,
      shopList: [],
      dialogVisible: false,
      reserveForm: {
        reservationDate: undefined,
        reservationTime: undefined

      },
      reserveData: {
        reservationDateList: [],
        reservationTimeList: []
      },
      btnLoading: false,
      reservationDateIndex: 0
    }
  },
  created () {
    this.getList();
    this.getOrderEnum();
    this.getServiceUnitList()
    this.getShopList()
  },
  methods: {
    goDetails (row) {
      this.$router.push({
        path: '/order/orderList/details',
        query: { id: row.id }
      })
    },
    openDialog (item) {
      this.dialogVisible = true;
      this.reserveForm.orderId = item.orderId
      reservation(item.businessProductId).then(res => {
        this.reserveData = res.data;
      })
    },
    getOrderEnum () {
      orderEnum().then(res => {
        this.statusList = res.data.orderStatus;
        this.queryStatusList.push(...res.data.orderStatus)
      })
    },
    // 获取服务单位列表
    getServiceUnitList () {
      serviceProductEnum().then(res => {
        this.serviceUnitList = res.data.serviceUnit
      })
    },
    reservationDateChange (e) {
      this.reserveData.reservationDateList.map((v, i) => {
        if (v.reservationDate == e) {
          this.reservationDateIndex = i
        }
      })
      this.reserveForm.reservationTime = null
    },
    submit () {
      if (!this.reserveForm.reservationDate) {
        this.$message.error("请选择预约日期");
        return;
      }
      if (!this.reserveForm.reservationTime) {
        this.$message.error("请选择预约时间段");
        return;
      }
      this.btnLoading = true
      businessProductOrderChange(this.reserveForm).then(res => {
        this.$message.success("设置成功");
        this.getList(); // 刷新列表
        this.close();
      }).finally(() => {
        this.btnLoading = false
      })
    },
    close () {
      this.reserveData = {
        reservationDateList: [],
        reservationTimeList: []
      }
      this.reserveForm = {
        reservationDate: undefined,
        reservationTime: undefined
      }
      this.dialogVisible = false;
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //  导出订单
    handleExport () {
      // /shoporder/shopOrder/export
      this.download('/order/order/export', {
        ...this.queryParams
      }, `订单列表_${new Date().getTime()}.xlsx`)
    },
    dealStatus (val) {
      return this.statusList.find(item => item.value == val).label
    },
    getShopList () {
      hospitalSimpleList().then(res => {
        this.shopList = res.data
      })
    },
    getList () {
      this.loading = true;
      this.queryParams["startTime"] = undefined;
      this.queryParams["endTime"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["startTime"] = this.daterangeCreateTime[0];
        this.queryParams["endTime"] = this.daterangeCreateTime[1];
      }
      orderPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset () {
      this.form = {
        name: undefined,
        shelvedStatus: undefined,
        orderIndex: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>

<style lang='scss' scoped>
</style>