<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="UID" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <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="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.app_users_charge_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="四方平台订单号" prop="tradeNo">
        <el-input
          v-model="queryParams.tradeNo"
          placeholder="请输入四方平台订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值渠道" prop="channelId">
        <el-select v-model="queryParams.channelId" placeholder="请选择充值渠道">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="daterange">
        <el-date-picker
          v-model="daterange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChage"
        ></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
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:charge:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:charge:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:charge:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:charge:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="UID" align="center" prop="uid" />
      <el-table-column label="充值类型" align="center" prop="type">
        <template slot-scope="scope">
          {{scope.row.type==4?"手工充值":"在线充值"}}
        </template>
      </el-table-column>
      <el-table-column label="充值渠道" align="center" prop="channelId">
        <template slot-scope="scope">
          <span>{{ getTypeName(scope.row.channelId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值时间" align="center" prop="addtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addtime, '{y}-{m}-{d} {h}:{i}:{s}')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值金额" align="center" prop="coin" />
      <el-table-column label="货币类型" align="center" prop="coinType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_user_coin_type" :value="scope.row.coinType"/>
        </template>
      </el-table-column>
      <el-table-column label="赠送金额" align="center" prop="coinGive"></el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_users_charge_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="平台服务费" align="center" prop="platformService" width="100"/>

      <el-table-column label="订单号" align="center" prop="orderno" width="180"/>
      <el-table-column label="四方方平台订单号" align="center" prop="tradeNo" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:charge:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:charge:remove']"
          >删除</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="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="添加时间" prop="addtime">
          <el-date-picker clearable
            v-model="form.addtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择添加时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前剩余金币" prop="afterCoin">
          <el-input v-model="form.afterCoin" placeholder="请输入当前剩余金币" />
        </el-form-item>
        <el-form-item label="直属上级代理" prop="agentId">
          <el-input v-model="form.agentId" placeholder="请输入直属上级代理" />
        </el-form-item>
        <el-form-item label="支付环境" prop="ambient">
          <el-input v-model="form.ambient" placeholder="请输入支付环境" />
        </el-form-item>
        <el-form-item label="货币数" prop="coin">
          <el-input v-model="form.coin" placeholder="请输入货币数" />
        </el-form-item>
        <el-form-item label="赠送金额" prop="coinGive">
          <el-input v-model="form.coinGive" placeholder="请输入赠送金额" />
        </el-form-item>
        <el-form-item label="1是会员" prop="isAgent">
          <el-input v-model="form.isAgent" placeholder="请输入1是会员" />
        </el-form-item>
        <el-form-item label="是否被删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否被删除" />
        </el-form-item>
        <el-form-item label="是否水军充值 0否 1是" prop="isWater">
          <el-input v-model="form.isWater" placeholder="请输入是否水军充值 0否 1是" />
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="操作管理员用户的账号" prop="optUser">
          <el-input v-model="form.optUser" placeholder="请输入操作管理员用户的账号" />
        </el-form-item>
        <el-form-item label="商家订单号" prop="orderno">
          <el-input v-model="form.orderno" placeholder="请输入商家订单号" />
        </el-form-item>
        <el-form-item label="上级用户id" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入上级用户id" />
        </el-form-item>
        <el-form-item label="所属区域家族id" prop="pidLevel1">
          <el-input v-model="form.pidLevel1" placeholder="请输入所属区域家族id" />
        </el-form-item>
        <el-form-item label="所属家族id" prop="pidLevel2">
          <el-input v-model="form.pidLevel2" placeholder="请输入所属家族id" />
        </el-form-item>
        <el-form-item label="所属经纪人id" prop="pidLevel3">
          <el-input v-model="form.pidLevel3" placeholder="请输入所属经纪人id" />
        </el-form-item>
        <el-form-item label="直属上级id" prop="pidLevel4">
          <el-input v-model="form.pidLevel4" placeholder="请输入直属上级id" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="充值规则ID" prop="ruleId">
          <el-input v-model="form.ruleId" placeholder="请输入充值规则ID" />
        </el-form-item>
        <el-form-item label="状态 0:充值中 1：充值成功 2：充值失败" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态 0:充值中 1：充值成功 2：充值失败">
            <el-option
              v-for="dict in dict.type.app_users_charge_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="充值对象ID" prop="touid">
          <el-input v-model="form.touid" placeholder="请输入充值对象ID" />
        </el-form-item>
        <el-form-item label="三方平台订单号" prop="tradeNo">
          <el-input v-model="form.tradeNo" placeholder="请输入三方平台订单号" />
        </el-form-item>
        <el-form-item label="用户ID" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="平台服务费" prop="platformService">
          <el-input v-model="form.platformService" placeholder="请输入平台服务费" />
        </el-form-item>
        <el-form-item label="平台服务费率" prop="platformServiceRate">
          <el-input v-model="form.platformServiceRate" placeholder="请输入平台服务费率" />
        </el-form-item>
        <el-form-item label="充值渠道id" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入充值渠道id" />
        </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>
  </div>
</template>

<script>
import { listCharge, getCharge, delCharge, addCharge, updateCharge,channelList } from "@/api/business/charge";

export default {
  name: "Charge",
  dicts: ['app_users_charge_status','app_user_coin_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值记录表格数据
      chargeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      typeList:[],
      typeMap:{},
      daterange:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        addtime: null,
        afterCoin: null,
        agentId: null,
        ambient: null,
        coin: null,
        coinType: null,
        coinGive: null,
        isAgent: null,
        isDelete: null,
        isWater: null,
        money: null,
        optType: null,
        optUser: null,
        orderno: null,
        pid: null,
        pidLevel1: null,
        pidLevel2: null,
        pidLevel3: null,
        pidLevel4: null,
        remarks: null,
        ruleId: null,
        status: null,
        touid: null,
        tradeNo: null,
        type: null,
        uid: null,
        platformService: null,
        platformServiceRate: null,
        channelId: null,
        beginTime:null,
        endTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    var that = this;
    channelList().then(response => {
      if(response.data){
        for(var i in response.data){
          var item = response.data[i];
          that.typeMap[item.id.toString()] = item.name;
          that.typeList.push({
            value:item.id,
            label:item.name
          })
        }
      }
      this.getList();

    })
  },
  methods: {
    dateChage(val){
      if(undefined != val && null != val && val.length > 1){
        this.queryParams.beginTime = val[0];
        this.queryParams.endTime = val[1];
      }else{
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
    },
    getTypeName(id) {
      if(id == null || id == undefined){
        return "-";
      }
      if(undefined == this.typeMap[id.toString()] || null == this.typeMap[id.toString()]){
        return "-";
      }
      return this.typeMap[id.toString()];
    },
    /** 查询充值记录列表 */
    getList() {
      this.loading = true;
      listCharge(this.queryParams).then(response => {
        this.chargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        addtime: null,
        updateTime: null,
        afterCoin: null,
        agentId: null,
        ambient: null,
        coin: null,
        coinType: null,
        coinGive: null,
        isAgent: null,
        isDelete: null,
        isWater: null,
        money: null,
        optType: null,
        optUser: null,
        orderno: null,
        pid: null,
        pidLevel1: null,
        pidLevel2: null,
        pidLevel3: null,
        pidLevel4: null,
        remarks: null,
        ruleId: null,
        status: null,
        touid: null,
        tradeNo: null,
        type: null,
        uid: null,
        platformService: null,
        platformServiceRate: null,
        channelId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.daterange = [];
      this.queryParams.beginTime = null;
      this.queryParams.endTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCharge(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCharge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCharge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充值记录编号为"' + ids + '"的数据项？').then(function() {
        return delCharge(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/charge/export', {
        ...this.queryParams
      }, `charge_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
