<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <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="10" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单手机号" prop="userMobile" label-width="100px">
        <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="11" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="下单时间">
        <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 label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small" style="width: 240px">
          <el-option v-for="(v,i) in statusList" :key="i" :label="v.value" :value="v.code" />
        </el-select>
      </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="small" v-model="queryParams.daterange" style="margin-bottom: 30px;" @input="daterangeChange">
          <el-radio-button v-for="(v,i) in queryStatusList" :key="i" :label="v.value">{{v.label}}</el-radio-button>
        </el-radio-group>
      </el-col>
      <el-col :span="1.5" v-if="queryParams.daterange=='range'">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="handleQuery"></el-date-picker>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="3">
        合计应付金额：{{sumPrice.totalPrice||0}}
      </el-col>
      <el-col :span="3">
        合计退款金额：{{sumPrice.refundPrice||0}}
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <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="订单号" 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="totalPrice" />
      <el-table-column label="退款数" align="center" prop="refundNum">
        <template slot-scope="{row}">
          <span>{{row.refundNum||'0'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="refundPrice">
        <template slot-scope="{row}">
          <span>{{row.refundPrice||'/'}}</span>
        </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="createTime">
        <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" 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>
        </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="goOrderDetails(scope.row)">查看订单</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { statisticsPage, statisticsSumPrice } from "@/api/finance"
import { orderEnum } from "@/api/hospitalOrder.js"
import { hospitalSimpleList } from "@/api/hospital.js"
import { getDateRange } from "@/utils/ruoyi"
export default {
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [{}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: undefined,
        daterange: 'all'
      },
      // 表单参数
      form: {},
      queryStatusList: [
        { label: '今日', value: 'today' },
        { label: '本周', value: 'week' },
        { label: '本月', value: 'month' },
        { label: '本年', value: 'year' },
        { label: '全部', value: 'all' },
        { label: '指定日期', value: 'range' },
      ],
      statusList: [],
      shopList: [],
      sumPrice: {}
    }
  },
  created () {
    this.getList();
    this.getOrderEnum();
    this.getShopList();
    this.getStatistics();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    goOrderDetails (row) {
      this.$router.push({
        path: '/order/orderList/details',
        query: { id: row.id }
      })
    },
    daterangeChange () {
      this.daterangeCreateTime = [];
      this.queryParams.startTime = undefined;
      this.queryParams.endTime = undefined;
      if (this.queryParams.daterange == 'all') {
        this.handleQuery();
      } else if (this.queryParams.daterange == 'range') {

      } else {
        let obj = getDateRange(this.queryParams.daterange)
        this.queryParams.startTime = obj.start;
        this.queryParams.endTime = obj.end;
      }
      this.handleQuery();
    },
    getOrderEnum () {
      orderEnum().then(res => {
        this.statusList = res.data.orderStatus;
      })
    },
    getShopList () {
      hospitalSimpleList().then(res => {
        this.shopList = res.data
      })
    },
    getList () {
      this.loading = true;
      this.queryParams["createTimeStart"] = undefined;
      this.queryParams["createTimeEnd"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["createTimeStart"] = this.daterangeCreateTime[0];
        this.queryParams["createTimeEnd"] = this.daterangeCreateTime[1];
      }
      statisticsPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getStatistics () {
      statisticsSumPrice().then(res => {
        this.sumPrice = res.data;
      })
    },
    //  导出
    handleExport () {
      this.download('/order/statistics/export', {
        ...this.queryParams
      }, `财务列表_${new Date().getTime()}.xlsx`)
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
}
</script>

<style lang='scss' scoped>
::v-deep .el-radio-group {
  margin-bottom: 10px !important;
}
</style>