<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="奖项类型" align="center" prop="locationCodeText">
        <template slot-scope="{row}">
          <div v-for="(v,i) in prizeTypeList" :key="i" v-if="v.value===row.prizeType">{{v.label}}</div>
        </template>
      </el-table-column>
      <el-table-column label="奖项倍数" align="center" prop="prizeMultiple">
        <template slot-scope="{row}">
          <div v-if="row.prizeType==0">{{row.prizeMultiple}}</div>
          <div v-else>--</div>
        </template>
      </el-table-column>
      <el-table-column label="奖励区间" align="center" prop="minReward">
        <template slot-scope="{row}">
          <div v-if="row.prizeType==1">{{row.minReward+'-'+row.maxReward}}</div>
          <div v-else>--</div>
        </template>
      </el-table-column>
      <el-table-column label="兑换豆" prop="point" align="center">
        <template slot-scope="{row}">
          <div v-if="row.prizeType==2">{{row.point}}</div>
          <div v-else>--</div>
        </template>
      </el-table-column>
      <el-table-column label="中奖概率" prop="winProbability" align="center">
        <template slot-scope="{row}">
          <div>{{row.winProbability}}%</div>
        </template>
      </el-table-column>
      <el-table-column label="每日最大中奖次数" align="center" prop="dailyMaxWin" />
      <el-table-column label="是否上架" align="center" prop="isShelf">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="row.isShelf">上架</el-tag>
          <el-tag type="danger" v-if="!row.isShelf">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="游戏结果" align="center" prop="gameResult">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="row.gameResult">赢盘</el-tag>
          <el-tag type="danger" v-if="!row.gameResult">输盘</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</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)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(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" />

    <!-- 添加或修改测试单表对话框 -->
    <el-drawer :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="奖项类型" prop="prizeType">
          <el-select v-model="form.prizeType" placeholder="请选择奖项类型" clearable style="width: 100%" @change="prizeTypeChange">
            <el-option v-for="(v,i) in prizeTypeList" :key="i" :label="v.label" :value="v.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="奖项倍数" prop="prizeMultiple" v-if="form.prizeType===0">
          <el-select v-model="form.prizeMultiple" placeholder="请选择奖项倍数" clearable style="width: 100%">
            <el-option v-for="(v,i) in prizeMultipleList" :key="i" :label="v" :value="v" />
          </el-select>
        </el-form-item>
        <el-form-item label="奖励区间" prop="maxReward" v-if="form.prizeType===1">
          <el-input-number style="width: 40%" v-model="form.minReward" controls-position="right" :min="0.1" :precision="2"></el-input-number> -
          <el-input-number style="width: 40%" v-model="form.maxReward" controls-position="right" :min="0.1" :precision="2"></el-input-number>
        </el-form-item>
        <el-form-item label="奖励数量" prop="point" v-if="form.prizeType===2">
          <el-input-number v-model="form.point" controls-position="right" :min="0" :precision="0"></el-input-number>
          <span class="hint">*奖励兑换豆数量</span>
        </el-form-item>
        <el-form-item label="中奖概率" prop="winProbability">
          <el-input-number v-model="form.winProbability" controls-position="right" :min="0" :max="100" :precision="6"></el-input-number> %
          <!-- <el-input v-model="form.winProbability" placeholder="请输入中奖概率">
            <span slot="suffix"> %</span>
          </el-input> -->
          <span class="hint">支持输入小数</span>
        </el-form-item>
        <el-form-item label="每日最大中奖次数" prop="dailyMaxWin">
          <el-input-number v-model="form.dailyMaxWin" controls-position="right" :min="0" :precision="0"></el-input-number>
        </el-form-item>
        <el-form-item label="是否上架" prop="isShelf">
          <el-switch v-model="form.isShelf"></el-switch>
        </el-form-item>
        <!-- v-if="form.prizeType==1||form.prizeType==2" -->
        <el-form-item label="游戏结果" prop="gameResult">
          <el-select v-model="form.gameResult" disabled placeholder="请选择游戏结果" clearable style="width: 100%">
            <el-option v-for="(v,i) in gameResultList" :key="i" :label="v.label" :value="v.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { prizeAdd, prizeEdit, prizePage, prizeInfo, prizeDel } from "@/api/game.js";
import { getToken } from "@/utils/auth";

export default {
  dicts: ['ad_location'],
  components: {
  },
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "添加广告",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/demo/demo/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adName: undefined,
        status: undefined,
        locationCode: undefined
      },
      // 表单参数
      form: {
        prizeType: undefined,
        prizeMultiple: undefined,
        minReward: undefined,
        maxReward: undefined,
        point: undefined,
        winProbability: undefined,
        dailyMaxWin: undefined,
        isShelf: true,
        gameResult: undefined,
      },
      // 表单校验
      rules: {
        prizeType: [
          { required: true, message: "请选择奖项类型", trigger: "blur" }
        ],
        prizeMultiple: [
          { required: true, message: "请选择奖项倍数", trigger: "blur" }
        ],
        maxReward: [
          { required: true, message: "请输入游戏区间", trigger: "blur" }
        ],
        point: [
          { required: true, message: "请输入奖励数量", trigger: "blur" }
        ],
        winProbability: [{ required: true, message: "请输入中奖概率", trigger: "change" },
        {
          pattern: /^\d+(\.\d{1,8})?$/,
          message: "请输入小数位不得超过8位小数",
          trigger: "blur"
        }

        ],
        dailyMaxWin: [{ required: true, message: "请输入每日最大中奖次数", trigger: "change" }],
        isShelf: [{ required: true, message: "请选择是否上架", trigger: "change" }],
        gameResult: [{ required: true, message: "请选择游戏结果", trigger: "change" }],
      },
      prizeTypeList: [
        { label: "订单翻倍", value: 0 },
        { label: "红包奖励", value: 1 },
        { label: "兜底奖励", value: 2 },
      ],
      prizeMultipleList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      gameResultList: [
        { label: "赢", value: 1 },
        { label: "输", value: 0 }
      ]
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询测试单表列表 */
    getList () {
      this.loading = true;
      prizePage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    prizeTypeChange (e) {
      this.form.prizeMultiple = undefined;
      this.form.minReward = undefined;
      this.form.maxReward = undefined;
      this.form.point = undefined;
      this.form.gameResult = e == 0 ? 1 : 0;
      this.$refs.form.clearValidate()
    },
    // 取消按钮
    cancel () {
      this.form.prizeMultiple = undefined;
      this.form.minReward = undefined;
      this.form.maxReward = undefined;
      this.form.point = undefined;
      this.form.gameResult = undefined;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        prizeType: undefined,
        prizeMultiple: undefined,
        minReward: undefined,
        maxReward: undefined,
        point: undefined,
        winProbability: undefined,
        dailyMaxWin: undefined,
        isShelf: true,
        gameResult: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handlePage () {
      this.queryParams.pageNum = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      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 = "添加游戏配置";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      prizeInfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "编辑游戏配置";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.minReward === undefined && this.form.prizeType === 1) {
            this.$modal.msgError("请输入游戏区间最小值");
            return
          }
          if (this.form.prizeType === 1 && this.form.minReward >= this.form.maxReward) {
            this.$modal.msgError("请输入正确的游戏区间");
            return
          }
          this.buttonLoading = true;
          if (this.form.prizeType != 0) {
            this.form.prizeMultiple = 1
          }
          let formCopy = JSON.parse(JSON.stringify(this.form));
          formCopy.winProbability = this.form.winProbability + '';
          console.log(formCopy)
          if (this.form.id != null) {
            prizeEdit(formCopy).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            prizeAdd(formCopy).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除此数据项？').then(() => {
        this.loading = true;
        return prizeDel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
  }
};
</script>
<style lang="scss">
.el-drawer__body {
  padding: 0 20px !important;
}
</style>
<style lang="scss" scoped>
.flex {
  display: flex;
}
.demo-drawer__footer {
  margin-left: 120px;
}
.hint {
  display: inline-block;
  margin-left: 10px;
  color: #808080;
}
</style>
