<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="姓名：" prop="userName">
        <el-input
          v-model.trim="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号：" prop="userPhone">
        <el-input
          v-model.trim="queryParams.userPhone"
          placeholder="请输入手机号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式：" prop="payType">
         <el-select v-model="queryParams.payType" style="width: 240px" placeholder="请选择支付方式" clearable size="small">
          <el-option label="现金支付" :value="1"/>
          <el-option label="微信" :value="2"/>
          <el-option label="支付宝" :value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态：" prop="orderFlag">
        <el-select v-model="queryParams.orderFlag" style="width: 240px" placeholder="请选择支付方式" clearable size="small">
          <el-option label="未支付" :value="0"/>
          <el-option label="支付等待中" :value="1"/>
          <el-option label="支付失败" :value="2"/>
          <el-option label="卡充值失败" :value="3"/>
          <el-option label="支付成功" :value="4"/>
          <el-option label="取消支付" :value="5"/>
          <el-option label="已成功" :value="6"/>
        </el-select>
      </el-form-item>
      <el-form-item label="支付金额：" prop="Money">
        <el-input-number
          v-model="queryParams.startMoney"
          clearable
          size="small"
          type="number"
          style="width: 240px"
          @keyup.enter.native="handleQuery"></el-input-number> ~
        <el-input-number
          v-model="queryParams.endMoney"
          placeholder=""
          clearable
          size="small"
          type="number"
          style="width: 240px"
          @keyup.enter.native="handleQuery"></el-input-number>
      </el-form-item>
      <el-form-item label="支付时间：">
         <el-date-picker
          v-model="dateRange"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          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-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="adminList">
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="userPhone"/>
      <el-table-column label="金额" align="center" prop="money"/>
      <el-table-column label="支付状态" align="center" prop="orderFlag">
        <template slot-scope="scope">
          <span v-if="scope.row.orderFlag == 0" style="color:#909399">未支付</span>
          <span v-if="scope.row.orderFlag == 1" style="color:#000000">支付等待中</span>
          <span v-if="scope.row.orderFlag == 2" style="color:#E6A23C">支付失败</span>
          <span v-if="scope.row.orderFlag == 3" style="color:#F56C6C">卡充值失败</span>
          <span v-if="scope.row.orderFlag == 4" style="color:#409EFF">支付成功</span>
          <span v-if="scope.row.orderFlag == 5" style="color:#303133">取消支付</span>
          <span v-if="scope.row.orderFlag == 6" style="color:#67C23A">已成功</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <span>{{ scope.row.payType == 1 ? '现金支付':scope.row.payType ==2 ? '微信':scope.row.payType ==3 ? '支付宝': '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值来源" align="center" prop="topSource" />
      <el-table-column label="时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope" >
          <el-button v-if="scope.row.orderFlag ==3" 
            size="small"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >变更状态</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改订单状态对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
        <el-form-item label="支付状态标识" prop="orderFlag">
          <el-input v-model="form.orderFlag" placeholder="支付状态标识" @input="onInput()"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listDetal, exportDetail, updataChannel } from "@/api/recharge/detailed";

export default {
  name: "admin",
  data() {
    return {
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        userName: undefined,
        payType: undefined,
        topChannelName: undefined,
        startMoney: undefined,
        endMoney: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        aliasName: [
          { required: true, message: "渠道标识不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //强制el-input刷新
    onInput(){
        this.$forceUpdate();
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listDetal(this.addDateRange(this.queryParams,this.dateRange,'section')).then(response => {
          this.adminList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ids: undefined,
        name: undefined,
        topChannelName: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      Object.assign(this.form, row.id ? this.adminList.find(val=>val.id === row.id) : this.adminList.find(val=>val.id === this.ids[0]))
      updataChannel(this.form).then(response => {
        this.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.startMoney = undefined;
      this.queryParams.endMoney = undefined;
      this.queryParams.startTime = undefined;
      this.queryParams.endTime = undefined;
      this.queryParams.payType = undefined;
      this.queryParams.userName = undefined;
      this.queryParams.userPhone = undefined;
      this.queryParams.orderFlag = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.$confirm('是否确认导出数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDetail(queryParams);
        }).then(res => {
          this.downloadBlob(res)
          this.exportLoading = false;
        }).catch(() => {
          this.exportLoading = false;
        });
    },
  }
};
</script>
