<template>
  <div class="app-container">
    <!--<el-form-->
    <!--  :model="queryParams"-->
    <!--  ref="queryForm"-->
    <!--  size="small"-->
    <!--  :inline="true"-->
    <!--  v-show="showSearch"-->
    <!--  label-width="68px"-->
    <!--&gt;-->
    <!--  <el-form-item label="品质" prop="name">-->
    <!--    <el-input-->
    <!--      v-model="queryParams.name"-->
    <!--      placeholder="请输入品质"-->
    <!--      clearable-->
    <!--      @keyup.enter.native="handleQuery"-->
    <!--    />-->
    <!--  </el-form-item>-->
    <!--  <el-form-item label="概率值" prop="value">-->
    <!--    <el-input-->
    <!--      v-model="queryParams.value"-->
    <!--      placeholder="请输入概率值"-->
    <!--      clearable-->
    <!--      @keyup.enter.native="handleQuery"-->
    <!--    />-->
    <!--  </el-form-item>-->
    <!--  <el-form-item label="背景颜色" prop="bgColor">-->
    <!--    <el-select-->
    <!--      v-model="queryParams.bgColor"-->
    <!--      placeholder="请选择背景颜色"-->
    <!--      clearable-->
    <!--      style="width: 180px"-->
    <!--    >-->
    <!--      <el-option label="bg-rarity-epic" value="bg-rarity-epic" />-->
    <!--      <el-option label="bg-rarity-rare" value="bg-rarity-rare" />-->
    <!--      <el-option label="bg-rarity-fine" value="bg-rarity-fine" />-->
    <!--      <el-option label="bg-rarity-common" value="bg-rarity-common" />-->
    <!--    </el-select>-->
    <!--  </el-form-item>-->
    <!--  <el-form-item label="稀有度" prop="rarity">-->
    <!--    <el-select-->
    <!--      v-model="queryParams.rarity"-->
    <!--      placeholder="请选择稀有度"-->
    <!--      clearable-->
    <!--      style="width: 180px"-->
    <!--    >-->
    <!--      <el-option label="史诗" value="史诗" />-->
    <!--      <el-option label="稀有" value="稀有" />-->
    <!--      <el-option label="精良" value="精良" />-->
    <!--      <el-option label="普通" value="普通" />-->
    <!--    </el-select>-->
    <!--  </el-form-item>-->
    <!--  &lt;!&ndash;<el-form-item label="关联卡池id" prop="poolId">&ndash;&gt;-->
    <!--  &lt;!&ndash;  <el-input&ndash;&gt;-->
    <!--  &lt;!&ndash;    v-model="queryParams.poolId"&ndash;&gt;-->
    <!--  &lt;!&ndash;    placeholder="请输入关联卡池id"&ndash;&gt;-->
    <!--  &lt;!&ndash;    clearable&ndash;&gt;-->
    <!--  &lt;!&ndash;    @keyup.enter.native="handleQuery"&ndash;&gt;-->
    <!--  &lt;!&ndash;  />&ndash;&gt;-->
    <!--  &lt;!&ndash;</el-form-item>&ndash;&gt;-->
    <!--  <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="handleAdd"-->
      <!--    v-hasPermi="['system:probabilities: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="['system:probabilities: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="['system:probabilities: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="['system:probabilities:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="6" style="display: flex; align-items: center">
        <span
          style="margin-left: 10px"
          :style="{ color: totalProbability === 100 ? '#409EFF' : 'red' }"
        >
          总概率值：{{ totalProbability }}
        </span>
      </el-col>
      <!--<right-toolbar-->
      <!--  :showSearch.sync="showSearch"-->
      <!--  @queryTable="getList"-->
      <!--&gt;</right-toolbar>-->
    </el-row>

    <el-table
      v-loading="loading"
      :data="probabilitiesList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="id" align="center" prop="id" />-->
      <!--<el-table-column type="index" label="序号" width="60" align="center" />-->
      <el-table-column label="品质" align="center" prop="name" />
      <el-table-column
        label="概率值"
        align="center"
        prop="value"
      ></el-table-column>
      <!--<el-table-column label="颜色设置" align="center" prop="colorClass" />-->
      <!--<el-table-column label="关联卡池id" align="center" prop="poolId" />-->
      <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="['system:probabilities:edit']"
            >修改概率</el-button
          >
          <!--<el-button-->
          <!--  size="mini"-->
          <!--  type="text"-->
          <!--  icon="el-icon-delete"-->
          <!--  @click="handleDelete(scope.row)"-->
          <!--  v-hasPermi="['system:probabilities:remove']"-->
          <!--  >删除</el-button-->
          <!--&gt;-->
        </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
      style="margin-right: 0"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="品质" prop="name">-->
        <!--  <el-input v-model="form.name" placeholder="请输入品质" />-->
        <!--</el-form-item>-->
        <el-form-item label="概率值" prop="value">
          <el-input v-model="form.value" placeholder="请输入概率值" />
        </el-form-item>
        <!--<el-form-item label="背景颜色" prop="bgColor">-->
        <!--  <el-select-->
        <!--    v-model="form.bgColor"-->
        <!--    placeholder="请选择背景颜色"-->
        <!--    style="width: 100%"-->
        <!--  >-->
        <!--    <el-option label="bg-rarity-epic" value="bg-rarity-epic" />-->
        <!--    <el-option label="bg-rarity-rare" value="bg-rarity-rare" />-->
        <!--    <el-option label="bg-rarity-fine" value="bg-rarity-fine" />-->
        <!--    <el-option label="bg-rarity-common" value="bg-rarity-common" />-->
        <!--  </el-select>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="稀有度" prop="rarity">-->
        <!--  <el-select-->
        <!--    v-model="form.rarity"-->
        <!--    placeholder="请选择稀有度"-->
        <!--    style="width: 100%"-->
        <!--  >-->
        <!--    <el-option label="史诗" value="史诗" />-->
        <!--    <el-option label="稀有" value="稀有" />-->
        <!--    <el-option label="精良" value="精良" />-->
        <!--    <el-option label="普通" value="普通" />-->
        <!--  </el-select>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="关联卡池id" prop="poolId">-->
        <!--  <el-input v-model="form.poolId" placeholder="请输入关联卡池id" />-->
        <!--</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>
  </div>
</template>

<script>
import {
  listProbabilities,
  getProbabilities,
  delProbabilities,
  addProbabilities,
  updateProbabilities,
} from "@/api/system/probabilities";

export default {
  name: "Probabilities",
  props: {
    pool: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 概率设置表格数据
      probabilitiesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        value: null,
        colorClass: null,
        poolId: this.poolId,
        rarity: null, // 新增
      },
      // 表单参数
      form: {
        id: null,
        name: null,
        value: null,
        colorClass: null,
        poolId: null,
        rarity: null, // 新增
      },
      // 表单校验
      rules: {
        poolId: [
          { required: true, message: "关联卡池id不能为空", trigger: "blur" },
        ],
      },
      poolId: null,
    };
  },
  mounted() {
    console.log(this.pool);
    // 直接用 this.pool.id
    this.loadDataByPoolId(this.pool.id);
    this.getList();
  },
  methods: {
    loadDataByPoolId(poolId) {
      this.queryParams.poolId = poolId;
      this.getList();
    },
    /** 查询概率设置列表 */
    getList() {
      this.loading = true;
      listProbabilities(this.queryParams).then((response) => {
        this.probabilitiesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        value: null,
        colorClass: null,
        poolId: null,
        rarity: null, // 新增
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加概率设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProbabilities(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改概率设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.form.poolId = this.poolId;

        if (valid) {
          if (this.form.id != null) {
            updateProbabilities(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProbabilities(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除概率设置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProbabilities(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/probabilities/export",
        {
          ...this.queryParams,
        },
        `probabilities_${new Date().getTime()}.xlsx`
      );
    },
  },
  computed: {
    totalProbability() {
      return this.probabilitiesList.reduce(
        (sum, row) => sum + Number(row.value || 0),
        0
      );
    },
  },
  watch: {
    totalProbability(newVal) {
      if (newVal !== 100) {
        this.$message.error("当前总概率值不为100，请赶快修改！");
      }
    },
  },
};
</script>
