<template>
  <div class="app-container">
    <el-form
      @submit.native.prevent
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder=""
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </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="openSelectRule"
          v-hasPermi="['system:detailes:add']"
        >添加打分规则
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          @click="submitForm()"
        >提交
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-close"
          size="mini"
          @click="close()"
          v-hasPermi="['system:detailes:close']"
        >关闭
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      stripe
      :cell-style="cellStyle"
      v-loading="loading"
      :data="detailessList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <!-- <el-table-column label="" align="center" prop="id" /> -->
      <el-table-column
        label="内容"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="分值"
        align="center"
        prop="score"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="是否打分" align="center" prop="isUsed">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.Whether_Delete"
            :value="scope.row.isUsed"
          />
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <select-rule ref="select" :ruleId="queryParams.ruleId" @ok="handleQuery"/>
  </div>
</template>

<script>
import { listEducation,getEducation,
addEducation,updateEducation,delEducation,updateScore } from "@/api/smartSchool/studentManage/ruleDetails";
import selectRule from "./selectRule.vue";
import { CustomCellStyle } from "@/enums/customStyle";

export default {
  name: "Detailess",
  dicts: ["Whether_Delete"],
  components: { selectRule },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课堂巡视详细得分表格数据
      detailessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        score: null,
        isUsed: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 总分
      sumScore: 0,
      cellStyle: CustomCellStyle,
    };
  },

  created() {
    this.ruleId = +this.$route.params.id;
    this.form.ruleId = this.ruleId;
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listEducation(this.queryParams, this.ruleId).then((response) => {
        console.log(response);
        this.detailessList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        this.$modal.closeLoading();
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        patrolId: null,
        detailsId: null,
        content: null,
        score: null,
        isUsed: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 打开选择打分规则弹窗 */
    openSelectRule() {
      this.$refs.select.show();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      // console.log(selection);
      this.sumScore = selection
        .map((item) => Number(item.score))
        .reduce((front, back) => front + back, 0);
      // console.log(this.sumScore);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 提交按钮 */
    submitForm() {
      const patrolId = this.form.patrolId;
      // const ids = this.ids.join(",");
      updateScore({
        score: this.sumScore,
        patrolId: patrolId,
        ids: [...this.ids],
      }).then((response) => {
        this.$modal.msgSuccess("提交成功");
        this.close();
      });
    },

    // 关闭按钮
    close() {
      window.history.back(-1);
    },
    confirmRule(id) {
      this.$modal.loading('正在添加打分规则，请稍等...');
      addEducation(this.ruleId, id).then(response => {
        this.$modal.closeLoading();
        this.getList();
      }).catch(error => {
        this.$modal.closeLoading();
      })
    },
  },
};
</script>
