<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <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="targetUsercode">
        <el-input
            v-model="queryParams.targetUsercode"
            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="unionUsercode">
        <el-input
            v-model="queryParams.unionUsercode"
            placeholder="请输入蜜瓜号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链路号" prop="traceId">
        <el-input
            v-model="queryParams.traceId"
            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">
      <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="type" width="50">
        <template v-slot="scope">
          <cai-dict-tag :options="consumeLogTypeList" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80" >
        <template v-slot="scope">
          <cai-dict-tag :options="consumeLogStatusList" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="消费方" align="center" prop="amount" width="200">
        <template v-slot="scope">
          <span style="font-weight: bold">蜜瓜号:</span><span>{{scope.row.sourceUsercode}}</span> <br>
          <span style="font-weight: bold">手机号:</span><span>{{scope.row.sourcePhone}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="发生金额" align="center" prop="amount" width="80" />
      <el-table-column label="主播" align="center" prop="anchor" width="200" >
        <template v-slot="scope">
          <span style="font-weight: bold">蜜瓜号:</span><span>{{scope.row.targetUsercode}}</span> <br>
          <span style="font-weight: bold">手机号:</span><span>{{scope.row.targetPhone}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="主播收益" align="center" prop="anchorIncome"width="120"  >
        <template v-slot="scope">
          <span style="font-weight: bold">费率:</span><span>{{scope.row.targetRate}}</span> <br>
          <span style="font-weight: bold">金额:</span><span>{{scope.row.anchorAmount}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="邀请人" align="center" prop="one" width="200" >
        <template v-slot="scope">
          <span style="font-weight: bold">蜜瓜号:</span><span>{{scope.row.oneUsercode}}</span> <br>
          <span style="font-weight: bold">手机号:</span><span>{{scope.row.onePhone}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="邀请人收益" align="center" prop="amount" width="200" >
        <template v-slot="scope">
          <span style="font-weight: bold">分销:</span><span>{{scope.row.oneJoin ? '参与' : '不参与'}}</span><br>
          <span style="font-weight: bold">费率:</span><span>{{scope.row.oneRate}}</span> <br>
          <span style="font-weight: bold">金额:</span><span>{{scope.row.oneAmount}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="工会" align="center" prop="union" width="200" >
        <template v-slot="scope">
          <span style="font-weight: bold">蜜瓜号:</span><span>{{scope.row.unionUsercode}}</span> <br>
          <span style="font-weight: bold">手机号:</span><span>{{scope.row.unionPhone}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="工会收益" align="center" prop="amount" width="200" >
        <template v-slot="scope">
          <span style="font-weight: bold">分销:</span><span>{{scope.row.unionJoin ? '参与' : '不参与'}}</span><br>
          <span style="font-weight: bold">费率:</span><span>{{scope.row.unionRate}}</span> <br>
          <span style="font-weight: bold">金额:</span><span>{{scope.row.unionAmount}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="是否计算" align="center" prop="calculateStatus" width="80">
        <template v-slot="scope">
          <cai-dict-tag :options="consumeLogCalculateStatusList" :value="scope.row.calculateStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="分销时间" align="center" prop="opCreate" width="180" />
      <el-table-column label="链路号" align="center" prop="traceId" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80" fixed="right">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleInfo(scope.row)"
          >详情</el-button>
          <el-button
              size="mini"
              type="text"
              v-if="scope.row.status === 0"
              @click="handleUpdate(scope.row)"
          >分销</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"
    />
    <consume-log-info v-if="consumeLogInfoVisible" ref="consumeLogInfo" />
  </div>
</template>

<script>
import {fenxiaoConsumeLog, listConsumeLog} from "@/api/cai/consumeLog";
import {consumeLogCalculateStatusList, consumeLogStatusList, consumeLogTypeList} from "@/constant/statusMap";
import ConsumeLogInfo from "@/views/cai/consumeLog/consume-log-info";

export default {
  name: "ConsumeLog",
  components: {
    ConsumeLogInfo
  },
  data() {
    return {
      consumeLogTypeList,consumeLogStatusList,consumeLogCalculateStatusList,
      consumeLogInfoVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分销记录表格数据
      consumeLogList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        unionUsercode: undefined,
        oneUsercode: undefined,
        targetUsercode: undefined,
        sourceUsercode: undefined,
        traceId: undefined
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询分销记录列表 */
    getList() {
      this.loading = true;
      listConsumeLog(this.queryParams).then(response => {
        this.consumeLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    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
    },
    handleInfo(row) {
      this.consumeLogInfoVisible = true
      this.$nextTick(() => {
        this.$refs.consumeLogInfo.init(row.id)
      })
    },
    handleUpdate(row){
      this.$modal.confirm('确定要分销该数据项？').then(() => {
        this.loading = true;
        return fenxiaoConsumeLog(row.id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>
