<template>
  <div class="activity-management">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>场次设置</h1></div>
    </div>
    <div class="part-filter">
      <div class="part-filter-form">
        <el-form :model="form" inline>
          <el-form-item label="场次代码">
            <el-input v-model.trim="form.code"></el-input>
          </el-form-item>
          <el-button type="primary" @click="handleCurrentChange(0)"
            >查询</el-button
          >
        </el-form>
        <div class="part-filter-form-action">
          <el-button type="primary" icon="icon icon-add" @click="add"
            >新增</el-button
          >
          <!-- <el-button>导入</el-button> -->
        </div>
      </div>
    </div>

    <el-table :data="tableData">
      <el-table-column type="selection" width="42" />
      <el-table-column width="100" label="ID">
        <span slot-scope="scope">{{ scope.row.id }}</span>
      </el-table-column>
      <el-table-column width="200" label="场次代码">
        <span slot-scope="scope">{{ scope.row.code }}</span>
      </el-table-column>
      <el-table-column label="候考时间">
        <span slot-scope="scope">{{ scope.row.prepareSeconds / 60 }}</span>
      </el-table-column>
      <el-table-column width="120" label="状态">
        <span slot-scope="scope">{{
          scope.row.enable | zeroOneEnableDisableFilter
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="开始时间">
        <span slot-scope="scope">{{
          scope.row.startTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="结束时间">
        <span slot-scope="scope">{{
          scope.row.finishTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column width="120" label="更新人">
        <span slot-scope="scope">{{ scope.row.updateName }}</span>
      </el-table-column>
      <el-table-column sortable width="170" label="更新时间">
        <span slot-scope="scope">{{
          scope.row.updateTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="210" fixed="right">
        <div slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="edit(scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="toggleEnableActivity(scope.row)"
          >
            {{ !scope.row.enable | booleanEnableDisableFilter }}
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <ActivityManagementDialog
      ref="theDialog"
      :examId="examId"
      :activity="selectedActivity"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import {
  searchActivities,
  toggleEnableActivity,
} from "@/api/examwork-activity";
import ActivityManagementDialog from "./ActivityManagementDialog";

export default {
  name: "ActivityManagement",
  components: {
    ActivityManagementDialog,
  },
  computed: {
    examId() {
      return this.$route.params.examId;
    },
  },
  data() {
    return {
      form: {
        code: "",
        enableState: null,
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selectedActivity: {},
    };
  },
  async created() {
    this.searchForm();
  },
  methods: {
    async searchForm() {
      const res = await searchActivities({
        examId: this.examId,
        code: this.form.code,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    add() {
      // this.selectedActivity = {};
      // this.$refs.theDialog.openDialog();
      this.$router.push({
        name: "ActivityEdit",
        params: { examId: this.examId },
      });
    },
    edit(activity) {
      this.selectedActivity = activity;
      this.$refs.theDialog.openDialog();
      // this.$router.push({
      //   name: "ActivityEdit",
      //   params: { examId: this.examId, activityId: activity.id },
      // });
    },
    async toggleEnableActivity(activity) {
      await toggleEnableActivity({
        examId: this.examId,
        id: activity.id,
        enable: activity.enable === 0 ? 1 : 0,
      });
      this.searchForm();
    },
  },
};
</script>

<style></style>
