<template>
  <div class="customersRechargeOnffline">
    <el-card class="myCard">
      <div class="container">
        <div class="leftForm">
          <el-form ref="form" :model="form" label-width="120px" class="form" :rules="rules">
            <el-form-item label="请选择充值客户:" style="width: 80%;" prop="" required="" label-width="130px">
              <el-select v-model="form.id" filterable placeholder="请选择客户" clearable>
                <el-option v-for="item in orgList" :key="item.id" :label="item.orgname" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="充值金额:" prop="balance">
              <el-input v-model="form.balance"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="samll" type="primary" @click="onSubmit('form')">立即充值</el-button>
              <el-button size="samll" @click="cancel('form')">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="rightTable">
          <el-table :data="tableData" height="100%" border style="width: 100%" v-loading="loading">
            <el-table-column prop="customerid" label="客户名称" width="100px">
            </el-table-column>
            <el-table-column prop="recharge_amt" label="充值金额">
            </el-table-column>
            <el-table-column prop="recharge_path" label="充值途径">
            </el-table-column>
            <el-table-column prop="action" label="操作方式">
            </el-table-column>
            <el-table-column prop="recharge_date" label="充值日期">
            </el-table-column>

            <el-table-column prop="recharge_timestamp" label="支付完成时间">
            </el-table-column>
            <el-table-column prop="" label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="sechargeSubmit(scope.row)"
                           :disabled="scope.row.RECHARGE_REVERSE||scope.row.action=='充值冲账'?true:false">充值冲账
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  getOrganizationAPI,
  addLedgerAPI,
  getReachargelogAPI,
  editReachargelogAPI
} from '@/api/finance/customerRechargeManagement'

export default {
  data() {
    return {
      loading: true,
      form: {
        balance: "",
        id: "",
        orgname: ''
      },
      rules: {
        balance: [{required: true, trigger: "blur", message: '请输入充值金额'}, {
          pattern: /^[+]?(0|([1-9]\d*))?$/,
          message: '请输入数字'
        }],
      },
      page: 1,
      tableData: [],
      orgList: [],//客户列表
      searchList: [],
      id: sessionStorage.getItem("userId"),
      isDisabled: false,
    }
  },
  created() {
    this.getOrganization()
    this.getReachargelog()
  },
  methods: {
    getReachargelog() {//展示所有充值记录
      getReachargelogAPI({page: this.page}).then(res => {
        this.loading = false
        if (res.status == true) {
          this.tableData = res.data.rows
        }
      })
    },
    getOrganization() {//展示所有客户
      getOrganizationAPI({type: 1, id: this.id}).then(res => {
        if (res.status == true) {
          // console.log(res);
          this.orgList = res.data
          this.form.id = res.data.id
        }

      })
    },
    // search() {//搜索
    //   getOrganizationAPI({ type: 1, orgname: this.form.orgname }).then(res => {
    //     console.log(res);
    //     this.searchList = res.data
    //   })
    // },
    onSubmit(formName) {//充值
      let params = {
        balance: this.form.balance,
        id: this.form.id,
        userid: this.id
      }
      if (params.id) {
        addLedgerAPI(params).then(res => {

          if (res.status == true) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.$refs.form.resetFields()
            this.getReachargelog()
            this.form.balance = ''
            this.form.id = ''
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        })
      } else {
        this.$message({
          message: '请注意信息完整',
          type: "error",
        });
      }

    },
    cancel(formName) {//取消充值
      this.$refs.form.resetFields()
    },
    sechargeSubmit(row) {//充值冲账
      // console.log(row);
      let params = {
        balance: row.recharge_amt,
        id: row.id,
        orgid: row.orgid,
        userid: this.id
      }
      this.$confirm('此操作将冲账金额' + row.recharge_amt + "元,是否继续?", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        editReachargelogAPI(params).then(res => {
          if (res.status == true) {
            this.getReachargelog()
            this.$message({
              type: 'success',
              message: res.msg
            });
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消冲账'
        });
      });
    }
  }
}
</script>

<style lang="scss" scoped>

.customersRechargeOnffline {
  .myCard {
    .container {
      display: flex;
      justify-content: center;
      overflow: auto;
      height: 80vh;
      padding: 10px;
      min-width: 1200px;
      width: 100%;

      .leftForm {
        width: 35%;
        height: 100%;
        overflow: auto;

        .form {
          width: 90%;

          .radio {
            display: flex;
            flex-direction: column;
            height: 300px;
            width: 400px;
            overflow: hidden;
            overflow-y: scroll;
          }
        }
      }

      .rightTable {
        width: 65%;
        height: 100%;
        overflow: auto;
      }
    }
  }
}
</style>
