<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="游戏" prop="gameId">
        <el-select v-model="queryParams.gameId" placeholder="请选择" @change="initGameItemList">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开奖选项" prop="gameLotterySucc">
        <el-select v-model="queryParams.gameLotterySuccItem" placeholder="请选择" clearable>
          <el-option
            v-for="dict in gameItemShowList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="游戏期号" prop="gameDate">
        <el-input
          v-model="queryParams.gameDate"
          placeholder="请输入游戏期号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否开奖" prop="isLottery">
        <el-select v-model="queryParams.isLottery" placeholder="请选择是否开奖" clearable>
          <el-option
            v-for="dict in dict.type.app_game_is_open"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="记录时间" prop="gameRecordDate">
        <el-date-picker clearable
                        v-model="queryParams.gameRecordDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择记录时间">
        </el-date-picker>
      </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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleKj"
        >手动开奖</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:lottery:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:lottery:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:lottery:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:lottery:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lotteryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="游戏" align="center" prop="gameId">
        <template slot-scope="scope">
          <span>{{ getTypeName(scope.row.gameId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="游戏期号" align="center" prop="gameDate" />
      <el-table-column label="是否开奖" align="center" prop="isLottery">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_game_is_open" :value="scope.row.isLottery"/>
        </template>
      </el-table-column>
      <el-table-column label="开奖选项" align="center" prop="gameLotterySucc">
        <template slot-scope="scope">
          <span>{{ getItemName(scope.row.gameLotterySucc,scope.row.gameId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录时间" align="center" prop="gameRecordDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gameRecordDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:lottery:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:lottery:remove']"
          >删除</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"
    />

    <!-- 添加或修改游戏开奖记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="游戏类型" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择">
            <el-option
              v-for="item in gameClassifyList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏ID" prop="gameId">
          <el-select v-model="form.gameId" placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏期号" prop="gameDate">
          <el-input v-model="form.gameDate" placeholder="请输入游戏期号" />
        </el-form-item>
        <el-form-item label="是否开奖 0 否 1 是" prop="isLottery">
          <el-select v-model="form.isLottery" placeholder="请选择是否开奖 0 否 1 是">
            <el-option
              v-for="dict in dict.type.app_game_is_open"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开奖选项" prop="gameLotterySucc">
          <el-input v-model="form.gameLotterySucc" placeholder="请输入开奖选项" />
        </el-form-item>
        <el-form-item label="记录时间" prop="gameRecordDate">
          <el-date-picker clearable
                          v-model="form.gameRecordDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择记录时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 手动开奖 -->
    <el-dialog :title="title" :visible.sync="kjOpen" width="500px" append-to-body>
      <el-form ref="kjForm" :model="kjForm" :rules="kjRules" label-width="80px">
        <el-form-item label="游戏类型" prop="classId">
          <el-select v-model="kjForm.classId" placeholder="请选择" @change="changeClass">
            <el-option
              v-for="item in gameClassifyList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏" prop="gameId" >
          <el-select v-model="kjForm.gameId" placeholder="请选择" @change="changeGame">
            <el-option
              v-for="item in kjGamelist"
              :key="item.value"
              :label="item.label"
              :value="item.value"

            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开奖选项" prop="gameLotterySucc">
          <el-select v-model="kjForm.gameLotterySucc" placeholder="请选择">
            <el-option
              v-for="dict in kjGameItemList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏期号" prop="gameDate">
          <el-input v-model="kjForm.gameDate" placeholder="请输入游戏期号" />
        </el-form-item>
        <el-form-item label="是否开奖" prop="isLottery">
          <el-select v-model="kjForm.isLottery" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.app_game_is_open"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitKjForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLottery, getLottery, delLottery, addLottery, updateLottery,allList,kjLottery } from "@/api/business/lottery";

import {allGameList } from "@/api/business/game_item";

import {listClassifyGame } from "@/api/business/game";

export default {
  name: "Lottery",
  dicts: ['app_game_is_open'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 游戏开奖记录表格数据
      lotteryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      kjOpen:false,
      typeMap:{},
      typeList:[],
      gameItemMap:{},
      gameItemList:[],
      gameItemShowList:[],
      gameClassifyList:[],
      kjGamelist:[],
      kjGameItemList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classId: null,
        gameId: null,
        gameDate: null,
        isLottery: null,
        gameLotterySucc: null,
        gameLotterySuccItem: null,
        gameRecordDate: null
      },
      // 表单参数
      form: {},
      kjForm:{},
      // 表单校验
      rules: {
        classId: [
          { required: true, message: "游戏类型ID不能为空", trigger: "blur" }
        ],
        gameId: [
          { required: true, message: "游戏ID不能为空", trigger: "blur" }
        ],
      },
      // 表单校验
      kjRules: {
        classId: [
          { required: true, message: "游戏类型不能为空", trigger: "blur" }
        ],
        gameId: [
          { required: true, message: "游戏不能为空", trigger: "blur" }
        ],
        gameLotterySucc: [
          { required: true, message: "游戏选项不能为空", trigger: "blur" }
        ],
        gameDate: [
          { required: true, message: "游戏期号不能为空", trigger: "blur" }
        ],
        isLottery: [
          { required: true, message: "是否开奖不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    var that = this;
    allList().then(response => {
        if(response.data){
          for(var i in response.data){
            var item = response.data[i];
            that.gameItemMap[item.itemLocation.toString() + "_" + item.gameId] = item.itemName;
            that.gameItemList.push({
              value:item.itemLocation+"_"+item.gameId,
              label:item.itemName,
              gameId:item.gameId
            })
          }
        }
    });
    listClassifyGame().then(response => {
        if(response.data){
          for(var i in response.data){
            var item = response.data[i];
            that.gameClassifyList.push({
              value:item.id,
              label:item.name
            })
          }
        }
    });
    allGameList().then(response => {
      if(response.data){
        for(var i in response.data){
          var item = response.data[i];
          that.typeMap[item.id.toString()] = item.name;
          that.typeList.push({
            value:item.id,
            label:item.name,
            classId:item.classifyId
          })
        }
      }
      this.getList();
    })
  },
  methods: {
    initGameItemList(val){
      this.gameItemShowList = [];
      for(var i in this.gameItemList){
        var item = this.gameItemList[i];
        if(item.gameId == val){
          this.gameItemShowList.push(item);
        }
      }
    },
    changeClass(val){

      var that = this;
      that.kjGamelist = [];
      that.kjForm.gameId = null;
      that.kjForm.gameLotterySucc = null;
      for(var i in that.typeList){
        var item = that.typeList[i];
        if(item["classId"] == val){
          that.kjGamelist.push(item);
        }
      }
    },
    changeGame(val){
      var that = this;
      that.kjGameItemList = [];
      that.kjForm.gameLotterySucc = null;
      for(var i in that.gameItemList){
        var item = that.gameItemList[i];
        console.log(item["gameId"],val)
        if(item["gameId"] == val){
          that.kjGameItemList.push({
            value:item["value"].split("_")[0],
            label:item["label"],
            gameId:item["gameId"]
          });
        }
      }
    },
    getTypeName(id) {
      return this.typeMap[id.toString()];
    },
    getItemName(id,gameId) {
      let that = this;
      let val = "";
      let arr = id.split(",")
      for(var i in arr){
        var item = arr[i];
        if(val != ""){
          val += ",";
        }
        val += that.gameItemMap[item+"_"+gameId]
      }
      return val;
    },
    /** 查询游戏开奖记录列表 */
    getList() {
      this.loading = true;
      listLottery(this.queryParams).then(response => {
        this.lotteryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.kjOpen = false;
      this.reset();
      this.resetKj();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classId: null,
        gameId: null,
        gameDate: null,
        isLottery: null,
        gameLotterySucc: null,
        gameRecordDate: null
      };
      this.resetForm("form");
    },
    resetKj() {
      this.kjForm = {
        id: null,
        classId: null,
        gameId: null,
        gameDate: null,
        isLottery: null,
        gameLotterySucc: null,
        gameRecordDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.gameLotterySuccItem){
        this.queryParams.gameLotterySucc = this.queryParams.gameLotterySuccItem.split("_")[0];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.gameLotterySuccItem = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加游戏开奖记录";
    },
    /** 新增按钮操作 */
    handleKj() {
      this.resetKj();
      this.kjOpen = true;
      this.title = "手动开奖";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLottery(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改游戏开奖记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLottery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLottery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitKjForm() {
      this.$refs["kjForm"].validate(valid => {
        if (valid) {
          kjLottery(this.kjForm).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.open = false;
                this.getList();

            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除游戏开奖记录编号为"' + ids + '"的数据项？').then(function() {
        return delLottery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/lottery/export', {
        ...this.queryParams
      }, `lottery_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
