<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="'消费方'+systemName+'号'" prop="sourceUsercode">
        <el-input
            v-model="queryParams.sourceUsercode"
            :placeholder="'请输入'+systemName+'号'"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="'主播'+systemName+'号'" prop="targetUsercode">
        <el-input
            v-model="queryParams.targetUsercode"
            :placeholder="'请输入主播'+systemName+'号'"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="'邀请方'+systemName+'号'" prop="oneUsercode">
        <el-input
            v-model="queryParams.oneUsercode"
            :placeholder="'请输入邀请方'+systemName+'号'"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="'会长'+systemName+'号'" prop="unionUsercode">
        <el-input
            v-model="queryParams.unionUsercode"
            :placeholder="'请输入会长'+systemName+'号'"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分销" prop="status">
        <el-select v-model="queryParams.status" clearable size="small">
          <el-option
              v-for="dict in consumeLogStatusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @keyup.enter.native="handleQuery"
          />
        </el-select>
      </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-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">{{systemName}}号:</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" min-width="200" >
        <template v-slot="scope">
          <div v-if="scope.row.targetUsercode">
            <span style="font-weight: bold">主播:</span><span>{{scope.row.targetUsercode}}({{scope.row.targetPhone}})</span><br>
            <span style="font-weight: bold">费率/金额:</span>
            <span>
              <span class="font-green">{{scope.row.targetRate}}</span>
              /
              <span class="font-red">{{scope.row.anchorAmount}}</span>
            </span>
            <br>
          </div>
          <span v-if="!scope.row.targetUsercode">-</span>
        </template>
      </el-table-column>
      <el-table-column label="邀请方" align="center" prop="invite" min-width="200" >
        <template v-slot="scope">
          <div v-if="scope.row.oneUsercode">
            <span style="font-weight: bold">邀请人:</span><span>{{scope.row.oneUsercode}}({{scope.row.onePhone}})</span> <br>
            <span style="font-weight: bold">分销:</span><span :class="joinColor(scope.row.oneJoin)">{{scope.row.oneJoin ? '参与' : '不参与'}}</span><br>
            <span style="font-weight: bold">费率/金额:</span>
              <span>
                <span class="font-green">{{scope.row.oneRate}}</span>
              /
              <span class="font-red">{{scope.row.oneAmount}}</span>
              </span> <br>
          </div>
          <span v-if="!scope.row.oneUsercode">-</span>
        </template>
      </el-table-column>
      <el-table-column label="工会" align="center" prop="union" min-width="200" >
        <template v-slot="scope">
          <div v-if="scope.row.unionUsercode">
            <span style="font-weight: bold">会长:</span><span>{{scope.row.unionUsercode}}({{scope.row.unionPhone}})</span> <br>
            <span style="font-weight: bold">分销:</span><span :class="joinColor(scope.row.oneJoin)">{{scope.row.unionJoin ? '参与' : '不参与'}}</span><br>
            <span style="font-weight: bold">费率/金额:</span>
            <span>
              <span class="font-green">{{scope.row.unionRate}}</span>
              /
              <span class="font-red">{{scope.row.unionAmount}}</span>
            </span> <br>
          </div>
          <span v-if="!scope.row.unionUsercode">-</span>
        </template>
      </el-table-column>
      <el-table-column label="是否计算" align="center" prop="calculateStatus">
        <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" >
        <template v-slot="scope">
          <span>{{scope.row.createTime}}</span><br/>
          <span>{{scope.row.opCreate}}</span>
        </template>
      </el-table-column>
      <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)"
              v-hasPermi="['cai:consumeLog: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"
    />
    <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 {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      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,
        status: undefined
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    joinColor(join){
      return join ? 'font-blue' : 'font-red'
    },
    /** 查询分销记录列表 */
    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>
<style lang="scss" scoped>
.font-red {
  color: red;
}
.font-green {
  color: green;
}
.font-blue {
  color: blue;
}
</style>
