<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="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="积分" prop="points">
        <el-input
          v-model="queryParams.points"
          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="onePoints">
        <el-input
          v-model="queryParams.onePoints"
          placeholder="请输入分销方积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源ID" prop="sourceId">
        <el-input
          v-model="queryParams.sourceId"
          placeholder="请输入来源ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['cai:pointRecordLog: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="['cai:pointRecordLog: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="['cai:pointRecordLog: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="['cai:pointRecordLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointRecordLogList" @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="用户" align="center" prop="userId" />
      <el-table-column label="积分" align="center" prop="points" />
      <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="onePoints" />
      <el-table-column label="状态  -1-无须分销 0-待分销 1-已分销" align="center" prop="status" />
      <el-table-column label="来源" align="center" prop="sourceType" />
      <el-table-column label="来源ID" align="center" prop="sourceId" />
      <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="['cai:pointRecordLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cai:pointRecordLog: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="userId">
          <el-input v-model="form.userId" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="积分" prop="points">
          <el-input v-model="form.points" 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="onePoints">
          <el-input v-model="form.onePoints" placeholder="请输入分销方积分" />
        </el-form-item>
        <el-form-item label="来源ID" prop="sourceId">
          <el-input v-model="form.sourceId" placeholder="请输入来源ID" />
        </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 { listPointRecordLog, getPointRecordLog, delPointRecordLog, addPointRecordLog, updatePointRecordLog } from "@/api/cai/pointRecordLog";

export default {
  name: "PointRecordLog",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户积分分销表格数据
      pointRecordLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        points: undefined,
        sourceUserId: undefined,
        sourceUsercode: undefined,
        sourcePhone: undefined,
        oneUserId: undefined,
        oneUsercode: undefined,
        onePhone: undefined,
        oneRate: undefined,
        onePoints: undefined,
        status: undefined,
        sourceType: undefined,
        sourceId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户不能为空", trigger: "blur" }
        ],
        points: [
          { 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" }
        ],
        onePoints: [
          { required: true, message: "分销方积分不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态  -1-无须分销 0-待分销 1-已分销不能为空", trigger: "change" }
        ],
        sourceType: [
          { required: true, message: "来源不能为空", trigger: "change" }
        ],
        sourceId: [
          { required: true, message: "来源ID不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户积分分销列表 */
    getList() {
      this.loading = true;
      listPointRecordLog(this.queryParams).then(response => {
        this.pointRecordLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        points: undefined,
        sourceUserId: undefined,
        sourceUsercode: undefined,
        sourcePhone: undefined,
        oneUserId: undefined,
        oneUsercode: undefined,
        onePhone: undefined,
        oneRate: undefined,
        onePoints: undefined,
        status: undefined,
        sourceType: undefined,
        sourceId: undefined,
        remark: undefined,
        createTime: 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
      getPointRecordLog(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) {
            updatePointRecordLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addPointRecordLog(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 delPointRecordLog(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cai/pointRecordLog/export', {
        ...this.queryParams
      }, `pointRecordLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
