<template>
  <section class="content">
    <div class="box box-primary">
      <div class="box-body">
        <section class="content-header">
          <h1 id="itemTitle"></h1>
        </section>
        <section class="content">
          <div id="table"></div>
          <div id="toolbar">
            <label class="source">
              选择时间: <input type="text"
                     name="startTime"
                     id="startTime"
                     class="input-sm">
              至 <input type="text"
                     id="endTime"
                     name="endTime"
                     class="input-sm">
            </label>
            <label class="source">
              宝箱类型: <select name="boxType"
                      id="boxType"
                      class="input-sm">
                <option value="0">全部</option>
                <option value="1">幸运许愿池</option>
                <option value="2">梦幻许愿池</option>
              </select>
            </label>
            <label class="source">
              查询凭据：<select name="source"
                      id="source"
                      class="input-sm">
                <option value="1">平台号</option>
                <option value="2">偏差范围</option>
              </select>
            </label>
            <label class="msNo">
              平台号：<input type="text"
                     placeholder="请输入草莓号"
                     id="msNo"
                     class="input-sm">
            </label>
            <label class="deviation">
              偏差值： <input type="text"
                     id="minDis"
                     class="input-m"> - <input type="text"
                     id="maxDis"
                     class="input-m">
            </label>
            <button id="btnSearch"
                    class="btn btn-sm btn-primary">查询</button>
          </div>
        </section>
      </div>
    </div>
  </section>
</template>

<script>
import TableHelper from '@/utils/bootstrap-table-helper';

export default {
  name: "PrizeStatisAdminNewView",
  setup () {
    return {};
  },
  created () {
    this.$nextTick(function () {
      this.initData();
    });
  },
  methods: {
    initData () {
      $(function () {
        //searchHistory();
        var whiteList = {};
        requestWhiteList(searchHistory());
        function searchHistory () {
          $('#table').bootstrapTable('destroy');
          $('#table').bootstrapTable({
            columns: [
              { field: 'uid', title: 'uid', align: 'center', valign: 'middle', width: '10%' },
              { field: 'erbanNo', title: '平台号', align: 'center', valign: 'middle', width: '10%' },
              { field: 'nick', title: '昵称', align: 'center', valign: 'middle', width: '10%' },
              { field: 'partition', title: '分区', align: 'center', valign: 'middle', width: '10%' },
              { field: 'totalPurchaseMoney', title: '历史抽奖充值总额', align: 'center', valign: 'middle', width: '10%' },
              { field: 'totalDrawMoney', title: '历史抽奖总额', align: 'center', valign: 'middle', width: '10%' },
              { field: 'totalPlatformValue', title: '历史获奖平台价值', align: 'center', valign: 'middle', width: '10%' },
              { field: 'totalActualValue', title: '历史获奖实际价值', align: 'center', valign: 'middle', width: '10%' },
              {
                field: 'deviation', title: '偏差值', align: 'center', valign: 'middle', width: '10%', formatter: function (val, row, index) {
                  if (val > 0) {
                    return '<span style="color:red">+' + val + '</span>';
                  } else {
                    return '<span style="color:green">' + val + '</span>';
                  }
                }
              },
              {
                field: 'useNum', title: '历史白名单次数', align: 'center', valign: 'middle', width: '5%', formatter: function (val, row, index) {
                  var uid = row.uid;
                  if (whiteList[uid]) {
                    return whiteList[uid].useNum;
                  } else {
                    return 0;
                  }
                }
              },
              {
                field: 'whiteStatus', title: '白名单状态', align: 'center', valign: 'middle', width: '5%', formatter: function (val, row, index) {
                  var uid = row.uid;
                  if (whiteList[uid] && whiteList[uid].availableNum > 0) {
                    return '<span style="color:red">待抽奖</span>';
                  } else {
                    return '<span style="color:green">未设置</span>';
                  }
                }
              },
              {
                field: 'diamondUseNum',
                title: '梦幻白名单次数',
                align: 'center',
                valign: 'middle',
                width: '5%',
                formatter: function (val, row, index) {
                  var uid = row.uid;
                  if (whiteList[uid]) {
                    return whiteList[uid].useDiamondNum;
                  } else {
                    return 0;
                  }
                }
              },
              {
                field: 'diamondWhiteStatus',
                title: '梦幻白名单状态',
                align: 'center',
                valign: 'middle',
                width: '5%',
                formatter: function (val, row, index) {
                  var uid = row.uid;
                  if (whiteList[uid] && whiteList[uid].availableDiamondNum > 0) {
                    return '<span style="color:red">待抽奖</span>';
                  } else {
                    return '<span style="color:green">未设置</span>';
                  }
                }
              },
              {
                field: 'tmp',
                title: '操作',
                align: 'center',
                width: '10%',
                valign: 'middle',
                formatter: function (val, row, index) {
                  var btnStr = "";
                  var uid = row.uid;
                  if (whiteList[uid] && whiteList[uid].availableNum > 0) {
                    btnStr += "<button class='btn btn-sm btn-warning opt-del normal-box-del' data-id=" + uid + ">移除普通白名单</button>";
                  } else {
                    btnStr += "<button class='btn btn-sm btn-success opt-set normal-box-set' data-id=" + uid + ">设置普通白名单</button>";
                  }
                  if (whiteList[uid] && whiteList[uid].availableDiamondNum > 0) {
                    btnStr += "<button class='btn btn-sm btn-warning opt-del diamond-box-del' data-id=" + uid + ">移除梦幻白名单</button>";
                  } else {
                    btnStr += "<button class='btn btn-sm btn-success opt-set diamond-box-set' data-id=" + uid + ">设置梦幻白名单</button>";
                  }
                  return btnStr;
                }
              }
            ],
            cache: false,
            striped: true,
            showRefresh: false,
            pageSize: 10,
            pagination: true,
            pageList: [10, 20, 30, 50],
            sidePagination: "server", //表示服务端请求
            //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
            //设置为limit可以获取limit, offset, search, sort, order
            queryParamsType: "undefined",
            queryParams: function queryParams (params) {   //设置查询参数
              var param = {
                pageSize: params.pageSize,
                pageNumber: params.pageNumber,
                msNo: $('#msNo').val(),
                boxType: $("#boxType").val(),
                minDis: $('#minDis').val(),
                maxDis: $('#maxDis').val(),
                source: $('#source').val(),
                startTime: $("#startTime").val(),
                endTime: $("#endTime").val()
              };
              console.log(param);
              return param;
            },
            uniqueId: 'uid',
            toolbar: '#toolbar',
            url: '/admin/box/record/statis/list',
            onLoadSuccess: function () {  //加载成功时执行
              console.log("load success");
            },
            onLoadError: function () {  //加载失败时执行
              console.log("load fail");
            }
          })
        }

        $('#source').on('change', function () {
          var source = parseInt($(this).val());
          if (source == 1) {
            $('.msNo').show();
            $('.deviation').hide();
          } else {
            $('.msNo').hide();
            $('.deviation').show();
          }
        });

        $('#table').on('click', '.normal-box-set', function () {
          var uid = $(this).data('id');
          editWhiteList(uid, 1, 1);
        });

        $('#table').on('click', '.diamond-box-set', function () {
          var uid = $(this).data('id');
          editWhiteList(uid, 1, 2);
        });

        $('#table').on('click', '.normal-box-del', function () {
          var uid = $(this).data('id');
          editWhiteList(uid, 0, 1);
        });

        $('#table').on('click', '.diamond-box-del', function () {
          var uid = $(this).data('id');
          editWhiteList(uid, 0, 2);
        });
        $('#btnSearch').on('click', function () {
          var source = $('#source').val();
          var msNo = $('#msNo').val();
          var minDis = $('#minDis').val();
          var maxDis = $('#maxDis').val();
          if (source == 1 && msNo == '') {
            $("#tipMsg").text("请输入平台号");
            $("#tipModal").modal('show');
            return;
          } else if (source == 2 && (minDis == '' || maxDis == '')) {
            $("#tipMsg").text("请输入偏差值");
            $("#tipModal").modal('show');
            return;
          }
          requestWhiteList(searchHistory());
        });

        function editWhiteList (uid, availableNum, boxType) {
          $.post("/admin/prizeWhiteUser/save", { uid: uid, availableNum: availableNum, boxType: boxType }, function (res) {
            if (res.code == 200) {
              if (availableNum > 0) {
                $("#tipMsg").text("设置成功");
              } else {
                $("#tipMsg").text("移除成功");
              }
              $("#tipModal").modal('show');

              requestWhiteList(function () {
                TableHelper.doRefresh('#table');
              })
            } else {
              $("#tipMsg").text("设置失败");
              $("#tipModal").modal('show');
            }
          })
        }

        function requestWhiteList (callback) {
          $.ajax({
            url: '/admin/prizeWhiteUser/list',
            success: function (res) {
              console.log(res);
              if (res.code == 200) {
                whiteList = res.data;
                typeof callback === 'function' && callback();
              }

            }
          })
        }

        // 初始化时间选择器
        $('#startTime').datetimepicker({
          format: 'yyyy-mm-dd hh:ii:00',
          autoclose: true
        })
        $("#endTime").datetimepicker({
          format: 'yyyy-mm-dd hh:ii:00',
          autoclose: true
        });
      })
    }
  },

};
</script>

<style scoped>
.deviation {
  display: none;
}

#toolbar label {
  margin-right: 6px;
}
</style>