<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>欢迎页面-X-admin2.0</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" href="./static/css/font.css">
        <link rel="stylesheet" href="./static/css/xadmin.css">
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <script src="./static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./static/js/xadmin.js"></script>
    </head>
    <body onload="InitParams({{.ItemList}})">
    <input type="text" id="token" value={{.token}} hidden=true>
    <div class="x-body layui-anim layui-anim-up">
        <blockquote class="layui-elem-quote">
          <input class="layui-btn" type="button" onclick="OpenAddItem()" value=新增>

          <input class="layui-btn" type="button" onclick="Refresh()" value=刷新>
        </blockquote>
    </div>
    <dialog open id="modalAdd" class="x-body layui-anim layui-anim-up" hidden=true style = "center;">
        <form action="rollaward" method="post" class="layui-form layui-form-pane">
            物品列表
            <table class="radio-table" border="1" id="tb_item">
              <thead>
                <th style="width:100px;">物品</th>
                <th style="width:100px;">价值</th>
                <th style="width:256px;">操作</th>
              </thead>
            </table>
            <br>
            <input class="layui-btn" type="button" onclick="modalAddItem()" value=新增道具>
            <hr class="hr15">
            <input value="新增" class="layui-btn" style="width:100%;" type="button" onclick="addRollAward()">
            <br>
            <br>
            <input value="关闭" class="layui-btn" style="width:100%;" type="button" onclick="closeModalAdd()">
            <hr class="hr20" >
        </form>
    </dialog>
    <dialog open id="modalModify" class="x-body layui-anim layui-anim-up" hidden=true style = "center;">
        <form action="rollaward" method="put" class="layui-form layui-form-pane">
            <input id="id_modify" hidden=true type="text">
            随机物品列表
            <table class="radio-table" border="1" id="tb_item_modify">
              <thead>
                <th style="width:100px;">物品</th>
                <th style="width:100px;">价值</th>
                <th style="width:256px;">操作</th>
              </thead>
            </table>
            <br>
            <input class="layui-btn" type="button" onclick="modalAddItem()" value=新增道具>
            <hr class="hr15">
            <input value="修改" class="layui-btn" style="width:100%;" type="button" onclick="modifyRollAward()">
            <br>
            <br>
            <input value="关闭" class="layui-btn" style="width:100%;" type="button" onclick="closeModalModify()">
            <hr class="hr20" >
        </form>
    </dialog>
    <dialog open id="modalItem" class="x-body layui-anim layui-anim-up" hidden=true style="center;width:512px;">
        <form class="layui-form layui-form-pane">
            <select name="" id="item_id" onchange="gradeItemChange(this.options[this.options.selectedIndex].value)">
              {{range .ItemList}}
                <option value={{.Id}}>{{.Name}}</option>
              {{end}}
            </select>
            <hr class="hr15">
            <input id="btn" value="新增" class="layui-btn" style="width:64px;" type="button" onclick="addItem()">
            <br>
            <br>
            <input value="关闭" class="layui-btn" style="width:64px;" type="button" onclick="closeModalItem()">
            <hr class="hr20" >
        </form>
    </dialog>
    <table class="radio-table" border="1">
      <thead>
        <th style="width:256px;">操作</th>
        <th style="width:64px;">ID</th>
        <th style="width:1024px;">奖励列表</th>
      </thead>
      <tbody>
        {{range .infoList}}
          <tr>
            <td style = "text-align:center;">
              <input class="layui-btn" type="button" onclick="OpenModifyItem({{.Id}}, {{.ItemIdList}})" value=修改>
              {{"   | "}}
              <input class="layui-btn" type="button" onclick="clickDelete({{.Id}})" value=删除>
            </td>
            <td style = "text-align:center;">{{.Id}}</td>
            <td style = "text-align:center;">{{BeegoItemNameList .ItemIdList}}</td>
          </tr>
        {{end}}
      </tbody>
    </table>
    <script language="JavaScript">
      var isModelAdd = true
      var _ItemList = [];
      var addSelectIndex = 0;
      var awardItemIdList = [];
      
      function InitParams(itemList) {
        console.log("InitParams");
        _ItemList = itemList;
        // UpdateSelectItems()
      }
      // 刷新页面
      function Refresh() {
        var token = document.getElementById("token").value;
        var text = "token=" + encodeURI(token);

        $.ajax({
          type:"get",
          url:"/rollaward?" + text,
          success:function (data) {
            // alert(data.status);
            window.location.href="/rollaward";
          }
        });
      }
      // 更新select控件
      function UpdateSelectItems() {
        var itemDialog = document.getElementById("modalItem")
        var obj = itemDialog.querySelector("#item_id")
        console.info("UpdateSelectItems options:" + obj.length)
        var maxItemsIndex = _ItemList.length - 1
        var startIndex = addSelectIndex
        var endIndex = addSelectIndex + 10
        if (endIndex > maxItemsIndex) {
          endIndex = maxItemsIndex
        }
        
        for (var i = startIndex; i <= endIndex; i ++) {
          if (i > maxItemsIndex) {
            break
          }
          var id = _ItemList[i].id
          var name = _ItemList[i].name + "(" + getItemPrice(id) +")"
          console.log("modalAddItem index:"+i+" name:"+name+" id:"+id)
          
          obj.add(new Option(name, id))
          // obj.options[obj.length] = new Option(name, id)
          // obj.options[obj.length].style = 'display:list-item';

          addSelectIndex += 1
        }
        obj.options[0].selected = true;
        console.info("UpdateSelectItems options:" + obj.length)
      }
      function gradeItemChange(itemId) {
        console.info("gradeItemChange itemId:" + itemId)
      }
      // 道具名称
      function getItemName(itemId) {
        var id = parseInt(itemId)
        for (var i = 0; i < _ItemList.length; i ++){
          if (_ItemList[i].id == id){
            return _ItemList[i].name
          }
        }
        return ""
      }
      // 道具价格
      function getItemPrice(itemId) {
        var id = parseInt(itemId)
        for (var i = 0; i < _ItemList.length; i ++){
          if (_ItemList[i].id == id){
            return _ItemList[i].price
          }
        }
        return ""
      }
      // 刷新物品列表
      function refreshTableItem() {
        var key = "tb_item"
        if (isModelAdd == false){key = "tb_item_modify"}
        var obj = document.getElementById(key)
        var tbody = obj.getElementsByTagName("tbody")[0];
        if (tbody) {
          if (-[1,]) {
            obj.removeChild(tbody);
          }else{
            obj.removeNode(tbody);
          }
        }
        for (var i = 0; i < awardItemIdList.length; i ++){
          console.info("refreshTableItem awardItemId:"+awardItemIdList[i])
        }

        if (awardItemIdList.length > 0 && awardItemIdList[0] != "") {
          tbody = document.createElement("tbody")
          for (var i = 0; i < awardItemIdList.length; i++){
            var tr = document.createElement("tr")

            var td1 = document.createElement("td")
            td1.style = "text-align:center;"
            td1.innerHTML = getItemName(awardItemIdList[i])
            tr.appendChild(td1)

            var td2 = document.createElement("td")
            td2.style = "text-align:center;"
            td2.innerHTML = getItemPrice(awardItemIdList[i])
            tr.appendChild(td2)

            var td4 = document.createElement("td")
            td4.style = "text-align:center;"
            td4.innerHTML = '<input class="layui-btn" type="button" onclick="itemDelete(' + awardItemIdList[i] + ')" value=删除>'
            tr.appendChild(td4)

            tbody.appendChild(tr)
          }
          obj.appendChild(tbody)
        }
      }
      // 物品弹窗
      function modalAddItem() {
        refreshTableItem()
        // UpdateSelectItems()

        document.getElementById("modalItem").hidden = false;
      }
      function closeModalItem() {
        document.getElementById("modalItem").hidden = true;
      }
      function addItem() {
        var item_id = parseInt(document.getElementById("item_id").value);

        if (item_id == null || item_id == 0){
          alert("请选择道具");
          return;
        }
        var itemName = getItemName(item_id)
        if (itemName == ""){
          alert("该道具未配置");
          return;
        }

        awardItemIdList.push(item_id)
        refreshTableItem()
        document.getElementById("modalItem").hidden = true;
      }
      // 删除物品
      function itemDelete(itemId) {
        var id = parseInt(itemId)
        console.log("itemDelete itemId:" + itemId)
        for (var i = 0; i < awardItemIdList.length; i ++) {
          if (awardItemIdList[i] == id) {
            console.log("itemDelete 找到道具")
            awardItemIdList.splice(i, 1)
            break
          }
        }
        refreshTableItem()
        document.getElementById("modalItem").hidden = true;
      }

      function OpenAddItem() {
        isModelAdd = true

        document.getElementById("modalAdd").hidden = false;
      }
      function closeModalAdd() {
        document.getElementById("modalAdd").hidden = true;
      }
      function addRollAward () {
        var token = document.getElementById("token").value;
        if (awardItemIdList.length <= 0){
          alert("奖励物品空！");
          return;
        }

        var text = "token=" + encodeURI(token);
        text += "&items=" + encodeURI(JSON.stringify(awardItemIdList));

        $.ajax({
          type:"post",
          url:"/rollaward?" + text,
          success:function (data) {
            // alert(data.status);
            if (data.status){
              window.location.href="/rollaward";
              if (data.info){
                layer.msg(data.info,{icon:1,time:1000});
              }
            }else{
              if (data.info){
                alert(data.info);
              }else{
                window.location.href="/accountlogin";
              }
            }
          }
        });
      }

      function closeModalItem() {
        document.getElementById("modalItem").hidden = true;
      }

      function OpenModifyItem(id, itemIdList) {
        isModelAdd = false;

        awardItemIdList = []
        for (var i = 0; i < itemIdList.length; i ++) {
          awardItemIdList.push(itemIdList[i]);
        }

        document.getElementById("id_modify").value = id;
        refreshTableItem()

        document.getElementById("modalModify").hidden = false;
      }
      function closeModalModify() {
        document.getElementById("modalModify").hidden = true;
      }

      function modifyRollAward () {
        var token = document.getElementById("token").value;
        var id = document.getElementById("id_modify").value;
        if (awardItemIdList.length <= 0){
          alert("奖励物品空！");
          return;
        }

        var text = "token=" + encodeURI(token);
        text += "&id=" + encodeURI(id);
        text += "&items=" + encodeURI(JSON.stringify(awardItemIdList));

        $.ajax({
          type:"put",
          url:"/rollaward?" + text,
          success:function (data) {
            // alert(data.status);
            if (data.status){
              window.location.href="/rollaward";
              if (data.info){
                layer.msg(data.info,{icon:1,time:1000});
              }
            }else{
              if (data.info){
                alert(data.info);
              }else{
                window.location.href="/accountlogin";
              }
            }
          }
        });
      }

      function clickDelete (pcode) {
        var token = document.getElementById("token").value;
        var text = "token=" + encodeURI(token);
        text += "&pcode=" + encodeURI(pcode);

        $.ajax({
          type:"delete",
          url:"/rollaward?" + text,
          success:function (data) {
            // alert(data.status);
            if (data.status){
              window.location.href="/rollaward";
              if (data.info){
                layer.msg(data.info,{icon:1,time:1000});
              }
            }else{
              if (data.info){
                alert(data.info);
              }else{
                window.location.href="/accountlogin";
              }
            }
          }
        });
      }
    </script>
    </body>
</html>