<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      小小艺术家训练营
    </h2>
    <div class="m-core">
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
        ref="searchForm"
      >
        <el-form-item prop="name">
          <el-input
            v-model.trim="searchForm.name"
            clearable
             @keyup.enter.native="
              (e) => {
                e.target.blur();
                $refs.searchForm.save();
                search();
              }
            "
            placeholder="训练营标题"
          ></el-input>
        </el-form-item>

        <el-form-item prop="state">
          <el-select
            v-model.trim="searchForm.state"
            clearable
            filterable
            placeholder="训练营状态"
          >
            <el-option
              v-for="(item, index) in campStateList"
              :key="index"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="applyTime">
          <el-date-picker
            v-model.trim="searchForm.applyTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="报名开始日期"
            end-placeholder="报名结束日期"
            :picker-options="{
              firstDayOfWeek: 1,
            }"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="trainTime">
          <el-date-picker
            v-model.trim="searchForm.trainTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="训练开始日期"
            end-placeholder="训练结束日期"
            :picker-options="{
              firstDayOfWeek: 1,
            }"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>

      <div class="buttunWtap">
        <auth auths="tempLittleArtistTrainingCamp/add">
          <el-button
            type="primary"
            style="margin-bottom: 30px; margian-right: 10px"
            @click="addCamp"
            >新建训练营</el-button
          >
        </auth>
        <auth auths="tempLittleArtistTrainingCamp/queryPageTrainingCampUser">
          <el-button type="primary" style="margin-bottom: 30px" @click="getRosterList"
            >参与名单</el-button
          >
        </auth>
      </div>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="id"
            label="训练营编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="训练营标题"
          ></el-table-column>
          <el-table-column align="center" prop="studentId" label="状态">
            <template slot-scope="scope">
              <div>
                {{ scope.row.state | campStateListFilter }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="报名时间段">
            <template slot-scope="scope">
              <div>
                <p>{{ scope.row.applyStartDate | dayjsFormat }}</p>
                <p>{{ scope.row.applyEndDate | dayjsFormat }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="训练时间段">
            <template slot-scope="scope">
              <div>
                <p>{{ scope.row.trainStartDate | dayjsFormat }}</p>
                <p>{{ scope.row.trainEndDate | dayjsFormat }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="操作">
            <template slot-scope="scope">
              <div>
                <auth auths="tempLittleArtistTrainingCamp/queryUserTrainingDetail">
                  <el-button type="text" @click="lookCamp(scope.row)"
                    >详情</el-button
                  >
                </auth>
                <auth
                  :auths="'tempLittleArtistTrainingCamp/update'"

                >
                  <el-button
                    type="text"
                    @click="resetCamp(scope.row)"
                    v-if="scope.row.state != 'ING' && scope.row.state != 'END'"
                    >修改</el-button
                  >
                </auth>
                <auth
                  auths="tempLittleArtistTrainingCamp/delete"
                  v-if="scope.row.state == 'READY'"
                >
                  <el-button type="text" @click="deleteCamp(scope.row)"
                    >删除</el-button
                  >
                </auth>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
    <eidtCamp @getList="getList" ref="eidtCamp" />
    <campRosterList ref="campRosterList"/>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { getTimes } from "@/utils";
import { campStateList } from "@/utils/searchArray";
import { getTrainingCampList,delTrainingCamp } from "./api";

 import campRosterList from "./models/campRosterList";
import eidtCamp from "./models/eidtCamp";
//
export default {
  components: { pagination, eidtCamp, campRosterList },
  data() {
    return {
      searchForm: {
        search: null,
        applyTime: [],
        trainTime: [],
      },
      campStateList,
      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部

    this.init();
  },
  methods: {
    init() {
      this.getList();
    },
    async getList() {
      let { applyTime, trainTime, ...rest } = this.searchForm;
      let params = {
        ...rest,
        page: this.rules.page,
        rows: this.rules.limit,
        ...getTimes(applyTime, ["applyStartDate", "applyEndDate"]),
        ...getTimes(trainTime, ["trainStartDate", "trainEndDate"]),
      };
      try {
        const res = await getTrainingCampList({ ...params });
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {},
    lookCamp(row) {
      console.log(row)
      this.$router.push({path:'/operateManager/aristCampDetail',query:{id:row.id,imGroupIds:row.imGroupIds,name:row.name}})
    },
    resetCamp(row) {
      this.$refs.eidtCamp.openDioag(row);
    },
    deleteCamp(row) {
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delTrainingCamp(row.id).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功");
              this.getList();
              // this.routeOrderStatus = false;
            }
          });
        })
        .catch();
    },
    addCamp() {
      this.$refs.eidtCamp.openDioag();
    },
    getRosterList(){
      this.$refs.campRosterList.openDioag();
    }
  },
};
</script>
<style lang='scss' scoped>
</style>
