<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="服务名称" prop="goodsTitle">
        <el-input v-model="queryParams.goodsTitle" placeholder="请输入服务名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="客户姓名" prop="cusName">
        <el-input v-model="queryParams.cusName" placeholder="请输入服务名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.REPAIR_PAY_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="订单号" align="center" prop="orderNo"/>
      <el-table-column label="服务名称" align="center" prop="goodsTitle"/>
      <el-table-column label="消费类型" align="center" prop="goodsType">
        <template v-slot="scope">
          <span v-if="scope.row.goodsType == '1'">会员充值</span>
          <span v-if="scope.row.goodsType == '2'">维修服务</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" prop="cusName"/>
      <el-table-column label="客户手机号" align="center" prop="cusPhone"/>
      <el-table-column label="商品原价（元）" align="center" prop="goodsPrice"/>
      <el-table-column label="实付金额（元）" align="center" prop="payMoney"/>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.REPAIR_PAY_TYPE" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template v-slot="scope">
          <el-tag type="warning" v-if="scope.row.orderStatus === '0'">待支付</el-tag>
          <el-tag type="success" v-if="scope.row.orderStatus === '1'">已支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button v-if="scope.row.orderStatus === '0'" size="mini" type="text" icon="el-icon-edit" @click="toPay(scope.row)"
                     v-hasPermi="['repair:order-info:update']">支付
          </el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"-->
<!--                     v-hasPermi="['repair:order-info:update']">修改-->
<!--          </el-button>-->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['repair:order-info:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 展示形式：二维码 URL -->
    <el-dialog title="微信扫一扫" :visible.sync="qrCode.visible" width="700px" append-to-body
               :close-on-press-escape="false">
      <qrcode-vue :value="qrCode.url" size="500" level="L" />
    </el-dialog>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <OrderInfoForm ref="formRef" @success="getList"/>
  </div>
</template>

<script>
import * as OrderInfoApi from '@/api/repair/orderinfo';
import OrderInfoForm from './OrderInfoForm.vue';
import QrcodeVue from 'qrcode.vue'
export default {
  name: "OrderInfo",
  components: {
    OrderInfoForm,
    QrcodeVue
  },
  data() {
    return {
      qrCode: { // 展示形式：二维码
        url: '',
        title: '',
        visible: false,
      },
      options:[{
        value:'0',
        label:'待支付'
      },{
        value:'1',
        label:'已支付'
      }],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修模块 订单列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        orderNo: null,
        goodsTitle: null,
        goodsType: null,
        cusName: null,
        payType: null,
        orderStatus:null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await OrderInfoApi.getOrderInfoPage(this.queryParams);
        this.list = res.data.records;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    async toPay(row){
      if (row.payType == '01'){
        const orderId = row.id
        let data = {
          orderId:orderId
        }
        const res = await OrderInfoApi.toPay(data);
        console.log(res,208)
        this.qrCode = {
          url: res.data.code_url,
          visible: true
        }
      } else{
        await this.$modal.confirm('本订单为线下支付订单或挂账订单，点击确定前请确认客户是否已支付')
        let data = {
          orderNo:row.orderNo,
        }
        await OrderInfoApi.updateOrderInfo(data);
        this.$modal.msgSuccess("支付成功");
        this.getList()
      }

    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除维修模块 订单编号为"' + row.orderNo + '"的数据项?')
      try {
        await OrderInfoApi.deleteOrderInfo(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有维修模块 订单数据项?');
      try {
        this.exportLoading = true;
        const data = await OrderInfoApi.exportOrderInfoExcel(this.queryParams);
        this.$download.excel(data, '维修模块 订单.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
  }
};
</script>
