<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="businessParam">
        <el-input
          v-model="queryParams.businessParam"
          placeholder="请输入业务参数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟踪ID" prop="traceId">
        <el-input
          v-model="queryParams.traceId"
          placeholder="请输入跟踪ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入发生金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级金额" prop="oneAmount">
        <el-input
          v-model="queryParams.oneAmount"
          placeholder="请输入一级金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消费方用户" prop="sourceUserId">
        <el-input
          v-model="queryParams.sourceUserId"
          placeholder="请输入消费方用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消费方用户编码" prop="sourceUsercode">
        <el-input
          v-model="queryParams.sourceUsercode"
          placeholder="请输入消费方用户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消费方用户手机" prop="sourcePhone">
        <el-input
          v-model="queryParams.sourcePhone"
          placeholder="请输入消费方用户手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级分销方" prop="oneUserId">
        <el-input
          v-model="queryParams.oneUserId"
          placeholder="请输入一级分销方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级分销方" prop="oneUsercode">
        <el-input
          v-model="queryParams.oneUsercode"
          placeholder="请输入一级分销方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级分销方" prop="onePhone">
        <el-input
          v-model="queryParams.onePhone"
          placeholder="请输入一级分销方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级分销方抽成比例" prop="oneRate">
        <el-input
          v-model="queryParams.oneRate"
          placeholder="请输入一级分销方抽成比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否参与分销" prop="oneJoin">
        <el-input
          v-model="queryParams.oneJoin"
          placeholder="请输入是否参与分销"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态  0-否 1-是" prop="admin">
        <el-input
          v-model="queryParams.admin"
          placeholder="请输入状态  0-否 1-是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理员ID" prop="adminId">
        <el-input
          v-model="queryParams.adminId"
          placeholder="请输入管理员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="adminName">
        <el-input
          v-model="queryParams.adminName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分销人ID" prop="opId">
        <el-input
          v-model="queryParams.opId"
          placeholder="请输入分销人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分销人名称" prop="opName">
        <el-input
          v-model="queryParams.opName"
          placeholder="请输入分销人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分销时间" prop="opCreate">
        <el-date-picker clearable
          v-model="queryParams.opCreate"
          type="date"
          value-format="yyyy-MM-dd"
          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-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="['xq:consumeLog: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="['xq:consumeLog: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="['xq:consumeLog: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="['xq:consumeLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consumeLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" v-if="true"/>
      <el-table-column label="类型 1-VIP" align="center" prop="type" />
      <el-table-column label="业务参数" align="center" prop="businessParam" />
      <el-table-column label="跟踪ID" align="center" prop="traceId" />
      <el-table-column label="发生金额" align="center" prop="amount" />
      <el-table-column label="一级金额" align="center" prop="oneAmount" />
      <el-table-column label="消费方用户" align="center" prop="sourceUserId" />
      <el-table-column label="消费方用户编码" align="center" prop="sourceUsercode" />
      <el-table-column label="消费方用户手机" align="center" prop="sourcePhone" />
      <el-table-column label="一级分销方" align="center" prop="oneUserId" />
      <el-table-column label="一级分销方" align="center" prop="oneUsercode" />
      <el-table-column label="一级分销方" align="center" prop="onePhone" />
      <el-table-column label="一级分销方抽成比例" align="center" prop="oneRate" />
      <el-table-column label="是否参与分销" align="center" prop="oneJoin" />
      <el-table-column label="状态  0-待计算分销 1-已计算分销" align="center" prop="calculateStatus" />
      <el-table-column label="状态  -1-无须分销 0-待分销 1-已分销" align="center" prop="status" />
      <el-table-column label="状态  0-否 1-是" align="center" prop="admin" />
      <el-table-column label="管理员ID" align="center" prop="adminId" />
      <el-table-column label="名称" align="center" prop="adminName" />
      <el-table-column label="分销人ID" align="center" prop="opId" />
      <el-table-column label="分销人名称" align="center" prop="opName" />
      <el-table-column label="分销时间" align="center" prop="opCreate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.opCreate, '{y}-{m}-{d}') }}</span>
        </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"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xq:consumeLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xq:consumeLog: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="businessParam">
          <el-input v-model="form.businessParam" placeholder="请输入业务参数" />
        </el-form-item>
        <el-form-item label="跟踪ID" prop="traceId">
          <el-input v-model="form.traceId" placeholder="请输入跟踪ID" />
        </el-form-item>
        <el-form-item label="发生金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入发生金额" />
        </el-form-item>
        <el-form-item label="一级金额" prop="oneAmount">
          <el-input v-model="form.oneAmount" placeholder="请输入一级金额" />
        </el-form-item>
        <el-form-item label="消费方用户" prop="sourceUserId">
          <el-input v-model="form.sourceUserId" placeholder="请输入消费方用户" />
        </el-form-item>
        <el-form-item label="消费方用户编码" prop="sourceUsercode">
          <el-input v-model="form.sourceUsercode" placeholder="请输入消费方用户编码" />
        </el-form-item>
        <el-form-item label="消费方用户手机" prop="sourcePhone">
          <el-input v-model="form.sourcePhone" placeholder="请输入消费方用户手机" />
        </el-form-item>
        <el-form-item label="一级分销方" prop="oneUserId">
          <el-input v-model="form.oneUserId" placeholder="请输入一级分销方" />
        </el-form-item>
        <el-form-item label="一级分销方" prop="oneUsercode">
          <el-input v-model="form.oneUsercode" placeholder="请输入一级分销方" />
        </el-form-item>
        <el-form-item label="一级分销方" prop="onePhone">
          <el-input v-model="form.onePhone" placeholder="请输入一级分销方" />
        </el-form-item>
        <el-form-item label="一级分销方抽成比例" prop="oneRate">
          <el-input v-model="form.oneRate" placeholder="请输入一级分销方抽成比例" />
        </el-form-item>
        <el-form-item label="是否参与分销" prop="oneJoin">
          <el-input v-model="form.oneJoin" placeholder="请输入是否参与分销" />
        </el-form-item>
        <el-form-item label="状态  0-否 1-是" prop="admin">
          <el-input v-model="form.admin" placeholder="请输入状态  0-否 1-是" />
        </el-form-item>
        <el-form-item label="管理员ID" prop="adminId">
          <el-input v-model="form.adminId" placeholder="请输入管理员ID" />
        </el-form-item>
        <el-form-item label="名称" prop="adminName">
          <el-input v-model="form.adminName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="分销人ID" prop="opId">
          <el-input v-model="form.opId" placeholder="请输入分销人ID" />
        </el-form-item>
        <el-form-item label="分销人名称" prop="opName">
          <el-input v-model="form.opName" placeholder="请输入分销人名称" />
        </el-form-item>
        <el-form-item label="分销时间" prop="opCreate">
          <el-date-picker clearable
            v-model="form.opCreate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择分销时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConsumeLog, getConsumeLog, delConsumeLog, addConsumeLog, updateConsumeLog } from "@/api/xq/consumeLog";

export default {
  name: "ConsumeLog",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分销记录表格数据
      consumeLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        businessParam: undefined,
        traceId: undefined,
        amount: undefined,
        oneAmount: undefined,
        sourceUserId: undefined,
        sourceUsercode: undefined,
        sourcePhone: undefined,
        oneUserId: undefined,
        oneUsercode: undefined,
        onePhone: undefined,
        oneRate: undefined,
        oneJoin: undefined,
        calculateStatus: undefined,
        status: undefined,
        admin: undefined,
        adminId: undefined,
        adminName: undefined,
        opId: undefined,
        opName: undefined,
        opCreate: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型 1-VIP不能为空", trigger: "change" }
        ],
        businessParam: [
          { required: true, message: "业务参数不能为空", trigger: "blur" }
        ],
        traceId: [
          { required: true, message: "跟踪ID不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "发生金额不能为空", trigger: "blur" }
        ],
        oneAmount: [
          { required: true, message: "一级金额不能为空", trigger: "blur" }
        ],
        sourceUserId: [
          { required: true, message: "消费方用户不能为空", trigger: "blur" }
        ],
        sourceUsercode: [
          { required: true, message: "消费方用户编码不能为空", trigger: "blur" }
        ],
        sourcePhone: [
          { required: true, message: "消费方用户手机不能为空", trigger: "blur" }
        ],
        oneUserId: [
          { required: true, message: "一级分销方不能为空", trigger: "blur" }
        ],
        oneUsercode: [
          { required: true, message: "一级分销方不能为空", trigger: "blur" }
        ],
        onePhone: [
          { required: true, message: "一级分销方不能为空", trigger: "blur" }
        ],
        oneRate: [
          { required: true, message: "一级分销方抽成比例不能为空", trigger: "blur" }
        ],
        oneJoin: [
          { required: true, message: "是否参与分销不能为空", trigger: "blur" }
        ],
        calculateStatus: [
          { required: true, message: "状态  0-待计算分销 1-已计算分销不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态  -1-无须分销 0-待分销 1-已分销不能为空", trigger: "change" }
        ],
        admin: [
          { required: true, message: "状态  0-否 1-是不能为空", trigger: "blur" }
        ],
        adminId: [
          { required: true, message: "管理员ID不能为空", trigger: "blur" }
        ],
        adminName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        opId: [
          { required: true, message: "分销人ID不能为空", trigger: "blur" }
        ],
        opName: [
          { required: true, message: "分销人名称不能为空", trigger: "blur" }
        ],
        opCreate: [
          { required: true, message: "分销时间不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询分销记录列表 */
    getList() {
      this.loading = true;
      listConsumeLog(this.queryParams).then(response => {
        this.consumeLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        businessParam: undefined,
        traceId: undefined,
        amount: undefined,
        oneAmount: undefined,
        sourceUserId: undefined,
        sourceUsercode: undefined,
        sourcePhone: undefined,
        oneUserId: undefined,
        oneUsercode: undefined,
        onePhone: undefined,
        oneRate: undefined,
        oneJoin: undefined,
        calculateStatus: undefined,
        status: undefined,
        admin: undefined,
        adminId: undefined,
        adminName: undefined,
        opId: undefined,
        opName: undefined,
        opCreate: undefined,
        remark: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      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.loading = true;
      this.reset();
      const id = row.id || this.ids
      getConsumeLog(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改分销记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateConsumeLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addConsumeLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分销记录编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delConsumeLog(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xq/consumeLog/export', {
        ...this.queryParams
      }, `consumeLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
