<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="关联订单号" label-width="100" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入关联订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第三方流水号" label-width="100" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入第三方流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收支类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择收支类型" clearable>
          <el-option
            v-for="dict in dict.type.rl_change_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="busType">
        <el-select v-model="queryParams.busType" placeholder="请选择业务类型" clearable>
          <el-option
            v-for="dict in dict.type.rl_business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属人" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入所属人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="ownerPhone">
        <el-input
          v-model="queryParams.ownerPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="变动原因" prop="reason">
        <el-input
          v-model="queryParams.reason"
          placeholder="请输入变动原因"
          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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:changeBalance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="changeBalanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="变动id" align="center" prop="changeId" />
      <el-table-column label="关联订单号" align="center" prop="orderNo" />
      <el-table-column label="第三方流水号" align="center" prop="outTradeNo" />
      <el-table-column label="收支类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rl_change_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="busType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rl_business_type" :value="scope.row.busType"/>
        </template>
      </el-table-column>
      <el-table-column label="变动前金额" align="center" prop="beforeBalance" />
      <el-table-column label="代理商结余" align="center" prop="afterBalance" />
      <el-table-column label="变动金额" align="center" prop="amount" />
      <el-table-column label="所属人名称" align="center" prop="ownerName" />
      <el-table-column label="所属人类型" align="center" prop="ownerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rl_user_type" :value="scope.row.ownerType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="所属人id" align="center" prop="ownerId" />-->
      <el-table-column label="手机号" align="center" prop="ownerPhone" />
      <el-table-column label="变动原因" align="center" prop="reason" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </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="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入关联订单号" />
        </el-form-item>
        <el-form-item label="第三方流水号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入第三方流水号" />
        </el-form-item>
        <el-form-item label="收支类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择收支类型">
            <el-option
              v-for="dict in dict.type.rl_change_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型" prop="busType">
          <el-select v-model="form.busType" placeholder="请选择业务类型">
            <el-option
              v-for="dict in dict.type.rl_business_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="变动前金额" prop="beforeBalance">
          <el-input v-model="form.beforeBalance" placeholder="请输入变动前金额" />
        </el-form-item>
        <el-form-item label="代理商结余" prop="afterBalance">
          <el-input v-model="form.afterBalance" placeholder="请输入代理商结余" />
        </el-form-item>
        <el-form-item label="变动金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入变动金额" />
        </el-form-item>
        <el-form-item label="所属人名称" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入所属人名称" />
        </el-form-item>
        <el-form-item label="所属人id" prop="ownerId">
          <el-input v-model="form.ownerId" placeholder="请输入所属人id" />
        </el-form-item>
        <el-form-item label="手机号" prop="ownerPhone">
          <el-input v-model="form.ownerPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="变动原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入变动原因" />
        </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 { listChangeBalance, getChangeBalance, delChangeBalance, addChangeBalance, updateChangeBalance } from "@/api/system/changeBalance";

export default {
  name: "ChangeBalance",
  dicts: ['rl_change_type', 'rl_business_type','rl_user_type'],
  props: ['userId'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 余额变动表格数据
      changeBalanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        outTradeNo: null,
        type: null,
        busType: null,
        beforeBalance: null,
        afterBalance: null,
        amount: null,
        ownerName: null,
        ownerId: null,
        ownerPhone: null,
        reason: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询余额变动列表 */
    getList() {
      if (this.userId) {
        this.queryParams.userId = this.userId;
      }
      this.loading = true;
      listChangeBalance(this.queryParams).then(response => {
        this.changeBalanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮 
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        changeId: null,
        orderNo: null,
        outTradeNo: null,
        type: null,
        busType: null,
        beforeBalance: null,
        afterBalance: null,
        amount: null,
        ownerName: null,
        ownerId: null,
        ownerPhone: null,
        createTime: null,
        reason: 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.changeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加余额变动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const changeId = row.changeId || this.ids
      getChangeBalance(changeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改余额变动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.changeId != null) {
            updateChangeBalance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChangeBalance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const changeIds = row.changeId || this.ids;
      this.$modal.confirm('是否确认删除余额变动编号为"' + changeIds + '"的数据项？').then(function() {
        return delChangeBalance(changeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/changeBalance/export', {
        ...this.queryParams
      }, `changeBalance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
