<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="systemName+'号'" prop="usercode">
        <el-input
          v-model="queryParams.usercode"
          :placeholder="'请输入'+systemName+'号'"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户类型" prop="accountType">
        <el-select v-model="queryParams.accountType" placeholder="账户类型" clearable size="small">
          <el-option
              v-for="dict in accountTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务操作" prop="cateId">
        <el-select v-model="queryParams.cateId" placeholder="业务操作" clearable size="small">
          <el-option
              v-for="dict in changeTypeList"
              :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="accountChangeLogList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column :label="systemName+'号'" align="center" prop="usercode" />
      <el-table-column label="账户类型" align="center" prop="accountType" >
        <template v-slot="scope">
          <cai-dict-tag :options="accountTypeList" :value="scope.row.accountType"/>
        </template>
      </el-table-column>
      <el-table-column label="变动" align="center" prop="changeValue" >
        <template v-slot="scope" >
          <span :class="scope.row.changeValue >= 0 ?'font-red':'font-green'">{{scope.row.changeValue}}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务操作" align="center" prop="cateAdminName" />
      <el-table-column label="说明" align="center" prop="cateAdminName" min-width="200">
        <template v-slot="scope">
          <div class="context-avatar-center">
            <span>{{scope.row.showMessage}}</span><image-preview v-if="scope.row.tarImg" :src="scope.row.tarImg" :width="32" :height="32"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="后台操作" align="center" prop="isAdmin" >
        <template v-slot="scope">
          <cai-dict-tag :options="yesOrNoList" :value="scope.row.isAdmin"/>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="createTime" width="160" />
      <el-table-column label="链路号" align="center" prop="traceId" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              v-if="scope.row.traceId"
              @click="handleInfo(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"
    />
    <recharge-order-info v-if="rechargeOrderInfoVisible" ref="rechargeOrderInfo" />
    <user-gift-info v-if="userGiftInfoVisible" ref="userGiftInfo" />
    <guard-log-info v-if="guardLogInfoVisible" ref="guardLogInfo"/>
    <user-call-info v-if="userCallInfoVisible" ref="userCallInfo" />
  </div>
</template>

<script>
import {listAccountChangeLog, listChangeType} from "@/api/cai/accountChangeLog";
import {accountTypeList, yesOrNoList} from "@/constant/statusMap";
import RechargeOrderInfo from "@/views/cai/rechargeOrder/recharge-order-info";
import UserGiftInfo from "@/views/cai/userGift/user-gift-info";
import GuardLogInfo from "@/views/cai/guardLog/guard-log-info";
import UserCallInfo from "@/views/cai/userCall/user-call-info";

export default {
  name: "AccountChangeLog",
  components: {
    RechargeOrderInfo,UserGiftInfo,GuardLogInfo,UserCallInfo
  },
  data() {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      accountTypeList,yesOrNoList,
      userCallInfoVisible: false,
      rechargeOrderInfoVisible: false,
      userGiftInfoVisible: false,
      guardLogInfoVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账户明细表格数据
      accountChangeLogList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usercode: undefined,
        accountType: undefined,
        cateId: undefined,
        traceId:undefined
      },
      changeTypeList:[],
    };
  },
  created() {
    this.getList();
    listChangeType().then(res => {
      this.changeTypeList = res.data
    })
  },
  methods: {
    /** 查询账户明细列表 */
    getList() {
      this.loading = true;
      listAccountChangeLog(this.queryParams).then(response => {
        this.accountChangeLogList = 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){
      const command = row.traceLinkType
      switch (command) {
        case "RECHARGE":
          this.rechargeOrderInfoVisible = true
          this.$nextTick(() => {
            this.$refs.rechargeOrderInfo.traceIdInit(row.traceId)
          })
          break;
        case "SYSTEM_TRANS":
          this.rechargeOrderInfoVisible = true
          this.$nextTick(() => {
            this.$refs.rechargeOrderInfo.traceIdInit(row.traceId)
          })
          break;
        case "GIFT":
          this.userGiftInfoVisible = true
          this.$nextTick(() => {
            this.$refs.userGiftInfo.traceIdInit(row.traceId)
          })
          break;
        case "GUARD":
          this.guardLogInfoVisible = true
          this.$nextTick(() => {
            this.$refs.guardLogInfo.traceIdInit(row.traceId)
          })
          break;
        case "VIDEO":
          this.userCallInfoVisible = true
          this.$nextTick(() => {
            this.$refs.userCallInfo.traceIdInit(row.traceId)
          })
          break;
        case "WITHDRAW":
          /*this.rechargeOrderInfoVisible = true
          this.$nextTick(() => {
            this.$refs.rechargeOrderInfo.traceIdInit(row.traceId)
          })*/
          this.$modal.msgWarning("无关联记录");
          break;
        case "SYSTEM":
          if(row.changeType === 501){
            this.rechargeOrderInfoVisible = true
            this.$nextTick(() => {
              this.$refs.rechargeOrderInfo.traceIdInit(row.traceId)
            })
          }else {
            this.$modal.msgWarning("无关联记录");
          }
          break;
        default:
          this.$modal.msgSuccess("无关联记录");
          break;
      }
    }
  }
};
</script>

<style lang="scss" scoped>
  .context-avatar-center {
    display: flex;
    align-items: center; /* 垂直居中 */
    justify-content: center; /* 水平居中 */
  }
  .font-red {
    color: red;
  }
  .font-green {
    color: green;
  }
  .font-blue {
    color: blue;
  }
</style>
