<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核状态" prop="companyId">
        <el-select v-model="queryParams.status" clearable>
          <el-option label="待审核" :value="0" />
          <el-option label="已通过" :value="1" />
          <el-option label="已拒绝" :value="2" />
        </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-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['company:CompanyRedPackageLogs:add']"-->
<!--        >充值</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['company:CompanyRedPackageLogs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="CompanyRedPackageLogsList">
      <el-table-column label="公司" align="center" prop="companyName" />
      <el-table-column label="操作类型" align="center" prop="operateType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.operateType == 0">充值</el-tag>
          <el-tag v-if="scope.row.operateType == 1">发红包</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="充值金额" align="center" prop="upMoney" />
      <el-table-column label="手续费" align="center" prop="handlMoney" />
      <el-table-column label="手续费比例" align="center" prop="handlPri">
        <template slot-scope="scope">{{scope.row.handlPri}}%</template>
      </el-table-column>
      <el-table-column label="实际金额" align="center" prop="money" />
      <el-table-column label="审核状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="warning">待审核</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success">审核通过</el-tag>
          <el-tooltip class="item" effect="dark" :content="scope.row.rejectMemo" placement="top" v-if="scope.row.status == 2">
            <el-tag type="danger">审核拒绝</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="凭证照片" align="center" prop="images" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.images.split(',')[0]"
            :preview-src-list="scope.row.images.split(',')">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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"
            v-if="scope.row.status == 0"
            @click="rewOpen(scope.row)"
            v-hasPermi="['company:CompanyRedPackageLogs:edit']"
          >审核</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="100px">
        <el-form-item label="充值金额" prop="upMoney">
          <el-input v-model="form.upMoney" @change="handlMoneyChange" type="number" placeholder="请输入充值金额" style="width: 200px">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="手续费" prop="handlMoney">
          <el-input v-model="form.handlMoney" disabled placeholder="请输入充值金额" style="width: 200px">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="手续费比例" prop="handlPri">
          <el-input v-model="pri" disabled placeholder="请输入充值金额" style="width: 200px">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="到账金额" prop="money">
          <el-input v-model="form.money" disabled placeholder="请输入充值金额"  style="width: 200px">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="凭证照片" prop="images">
          <image-upload v-model="form.images" :limit="7" />
        </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>
    <!-- 添加或修改公司红包余额日志对话框 -->
    <el-dialog title="充值审核" :visible.sync="rewData.open" width="500px" append-to-body>
      <el-form ref="form" :model="rewData.form" :rules="rules" label-width="100px">
        <el-form-item label="审核" prop="status">
          <el-radio-group v-model="rewData.form.status">
            <el-radio :label="1">审核通过</el-radio>
            <el-radio :label="2">审核拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark" v-if="rewData.form.status == 1">
          <el-input type="textarea" :rows="3" class="el-input" v-model="rewData.form.remark" />
        </el-form-item>
        <el-form-item label="拒绝原因" prop="rejectMemo" v-if="rewData.form.status == 2">
          <el-input type="textarea" :rows="3" class="el-input" v-model="rewData.form.rejectMemo" />
        </el-form-item>
<!--        <el-form-item label="拒绝凭证" prop="rejectImages" v-if="rewData.form.status == 2">-->
<!--          <image-upload v-model="rewData.form.rejectImages" :limit="7" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rewFun">确 定</el-button>
        <el-button @click="cancelRew">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyRedPackageLogs, getCompanyRedPackageLogs, delCompanyRedPackageLogs, getMoneyPri, recharge, exportCompanyRedPackageLogs, rew } from "@/api/company/CompanyRedPackageLogs";

export default {
  name: "CompanyRedPackageLogs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      pri: 0,
      // 公司红包余额日志表格数据
      CompanyRedPackageLogsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        operateType: null,
        type: null,
        upMoney: null,
        handlMoney: null,
        handlPri: null,
        money: null,
        status: null,
        images: null,
        rejectMemo: null,
        rejectImages: null,
      },
      // 表单参数
      form: {},
      rewData: {
        open: false,
        form:{},
        row:{},
      },
      // 表单校验
      rules: {
        upMoney:[
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    getMoneyPri().then(e => {
      this.pri =e.data;
    });
    this.getList();
  },
  methods: {
    /** 查询公司红包余额日志列表 */
    getList() {
      this.loading = true;
      listCompanyRedPackageLogs(this.queryParams).then(response => {
        this.CompanyRedPackageLogsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handlMoneyChange(){
      let money = this.form.upMoney
      if(money != null && Number(money) > 0){
        this.form.handlMoney = (money * (this.pri / 100).toFixed(2)).toFixed(2);
        this.form.money = money - this.form.handlMoney;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelRew() {
      this.rewData.open = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        operateType: null,
        type: null,
        upMoney: null,
        handlMoney: null,
        handlPri: null,
        money: null,
        status: 0,
        images: null,
        rejectMemo: null,
        rejectImages: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      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
      getCompanyRedPackageLogs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司红包余额日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          recharge(this.form).then(response => {
            this.msgSuccess("充值成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除公司红包余额日志编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompanyRedPackageLogs(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有公司红包余额日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCompanyRedPackageLogs(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    rewOpen(row){
      this.rewData.open = true;
      getCompanyRedPackageLogs(row.id).then(e => {
        this.rewData.row = e.data;
        this.rewData.form = {
          id: row.id,
          status: 1,
          remark: "",
          rejectMemo: "",
          rejectImages: "",
        };
      })
    },
    rewFun(){
      rew(this.rewData.form).then(response => {
        this.msgSuccess("审核成功");
        this.rewData.open = false;
        this.getList();
      });
    },
  }
};
</script>
