<template>
  <div>
    <el-form :inline="true" :model="secondForm">
      <el-form-item class="mr10" label="班级：">
        <el-input
          v-model="secondForm.bjmc"
          placeholder="输入班级名称"
          clearable
          @clear="getHdList"
        ></el-input>
      </el-form-item>
      <el-form-item class="mr10" label="活动类别：">
        <el-select
          v-model="secondForm.type"
          placeholder="请选择类别"
          @change="getHdList"
          clearable
        >
          <el-option
            v-for="item in typeList"
            :key="item.xdbh_id"
            :label="item.xdbh_name"
            :value="item.xdbh_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="mr10" label="活动时间：">
        <el-date-picker
          style="width: 100%"
          clearable
          v-model="secondForm.time"
          type="date"
          value-format="yyyy-MM-dd"
          @change="getHdList"
          placeholder="请选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item class="mr10" label="活动名称：">
        <el-input
          v-model="secondForm.title"
          placeholder="输入活动名称"
          clearable
          @clear="getHdList"
        ></el-input>
      </el-form-item>
      <el-button class="mb20" type="primary" @click="getHdList">搜索</el-button>
    </el-form>
    <div>
      <el-button type="primary" @click="createNew">发起活动</el-button>
      <el-button type="primary" @click="exportAc">导出</el-button>
      <el-button
        type="danger"
        :disabled="multipleSelection.length == 0"
        @click="delAc"
        >删除</el-button
      >
    </div>
    <el-table
      :data="secondTable"
      v-loading="loading"
      tooltip-effect="dark"
      class="mt20"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column
        label="班级"
        align="left"
        prop="xdb_bjmc"
        width="200"
      ></el-table-column>
      <el-table-column label="活动名称" width="auto">
        <template slot-scope="scope">
          <h3
            class="font-size-14 color-2 hand"
            @click="toDetail(scope.row.xdb_id)"
          >
            {{ scope.row.xdb_hdmc }}
          </h3>
        </template>
      </el-table-column>
      <el-table-column
        label="类别"
        width="100"
        prop="xdbh_name"
      ></el-table-column>
      <el-table-column label="活动时间" width="200">
        <template v-slot="scope">
          {{ scope.row.xdb_hdkssj }} ~ {{ scope.row.xdb_hdjssj }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="xdb_cjr" width="120">
      </el-table-column>
      <el-table-column label="审核人" prop="xdb_shr" width="120">
      </el-table-column>
      <el-table-column label="活动状态" width="120">
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.xdb_hdzt_option_k == 0 ||
              scope.row.xdb_hdzt_option_k == 1
            "
            class="text-grey"
            >未审核</span
          >
          <span v-if="scope.row.xdb_hdzt_option_k == 2" class="text-green"
            >进行中</span
          >
          <span v-if="scope.row.xdb_hdzt_option_k == 3" class="text-green"
            >已结束</span
          >
        </template>
      </el-table-column>
      <el-table-column label="活动地点" prop="xdb_hddz" width="auto">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="signDialogShow(scope.row)"
            >报名
          </el-button>
          <!--          <el-button-->
          <!--              :disabled="scope.row.xdb_hdzt_option_k > 1"-->
          <!--              type="text"-->
          <!--              @click="auditAc(scope.row)"-->
          <!--          >审核-->
          <!--          </el-button>-->
          <el-button type="text" @click="editAc(scope.row)">编辑 </el-button>
          <el-button type="text" @click="toDetail(scope.row.xdb_id)"
            >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="newDialogShow"
      width="750px"
      v-loading="loading"
    >
      <el-form
        :model="newForm"
        ref="newForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="班级：" prop="xdb_bjmc">
          <el-input
            v-model="newForm.xdb_bjmc"
            placeholder="请输入班级名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动名称：" prop="xdb_hdmc">
          <el-input
            v-model="newForm.xdb_hdmc"
            placeholder="请输入活动名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动类别：" prop="xdbh">
          <el-select
            v-model="newForm.xdbh"
            filterable
            placeholder="请选择活动类别"
            style="width: 410px"
          >
            <el-option
              v-for="item in typeList.slice(1)"
              :key="item.xdbh_id"
              :label="item.xdbh_name"
              :value="{ value: item.xdbh_id, label: item.xdbh_name }"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动时间：" prop="xdb_hdsj">
          <el-date-picker
            style="width: 410px"
            v-model="newForm.xdb_hdsj"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动地址：" prop="xdb_hddz">
          <el-input
            v-model="newForm.xdb_hddz"
            placeholder="请输入活动地址"
          ></el-input>
        </el-form-item>
        <!--        <el-form-item label="审核人：" prop="xdb_shr">-->
        <!--          <el-input-->
        <!--              v-model="newForm.xdb_shr"-->
        <!--              placeholder="请输入审核人"-->
        <!--          ></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="活动描述：" prop="xdb_hdms">
          <Tinymce
            v-if="newDialogShow"
            v-model="newForm.xdb_hdms"
            :height="300"
            :key="timestamp"
          ></Tinymce>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="newDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="newFormSubmit('newForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      title="报名"
      :visible.sync="signDialogVisible"
      width="550px"
      v-loading="loading"
    >
      <el-form
        :model="signForm"
        ref="signForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="学生姓名：" prop="xdh_xsxm">
          <el-input
            v-model="signForm.xdh_xsxm"
            placeholder="请输入学生姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="学籍号：" prop="xdh_xsxh">
          <el-input
            v-model="signForm.xdh_xsxh"
            placeholder="请输入学籍号"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码：" prop="xdh_sjhm">
          <el-input
            v-model="signForm.xdh_sjhm"
            placeholder="请输入手机号码"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="signDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="signFormSubmit('signForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { hd_list, hdlb_list, hd_add, hd_edit, hd_del, sign_add } from "./api";
import Tinymce from "@/components/Tinymce/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import { useUserStore } from "@/stores/user";
const { token, real_name } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 0,
      loading: false,
      cur_page: 1,
      secondForm: {
        bjmc: "",
        type: "",
        title: "",
        time: "",
      },
      gradeList: [
        {
          label: "六年级一班",
          value: 1,
        },
        {
          label: "五年级一班",
          value: 2,
        },
        {
          label: "四年级一班",
          value: 3,
        },
        {
          label: "三年级一班",
          value: 4,
        },
      ],
      secondTable: [],
      typeList: [],
      multipleSelection: [],
      dialogTitle: "",
      newDialogShow: false,
      newForm: {
        xdb_id: "",
        xdb_bjmc: "",
        xdb_hdmc: "",
        xdbh: "",
        xdb_hdsj: [],
        xdb_cjr: "",
        // xdb_shr: '',
        xdb_hddz: "",
        xdb_hdms: "",
      },
      rules: {
        xdb_bjmc: [
          { required: true, message: "请输入班级名称", trigger: "blur" },
        ],
        xdb_hdmc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        xdbh: [
          {
            type: "object",
            required: true,
            message: "请选择审核人",
            trigger: "change",
          },
        ],
        xdbh_sfqy: [
          { required: true, message: "请选择启用状态", trigger: "change" },
        ],
        xdb_hdsj: [
          { required: true, message: "请选择报名时间", trigger: "blur" },
        ],
        xdb_hddz: [
          { required: true, message: "请输入活动地址", trigger: "blur" },
        ],
        xdb_hdms: [
          { required: true, message: "请输入活动描述", trigger: "blur" },
        ],
        // xdb_shr:[
        //   {required: true, message: '请输入审核人', trigger: 'blur'}
        // ],
        xdh_xsxm: [
          { required: true, message: "请输入学生姓名", trigger: "blur" },
        ],
        xdh_xsxh: [
          { required: true, message: "请输入学籍号", trigger: "blur" },
        ],
        xdh_sjhm: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
        ],
      },
      newEdit: false,
      signDialogVisible: false,
      signForm: {
        xdh_xsxm: "",
        xdh_xsxh: "",
        xdh_sjhm: "",
        xdb_id: "",
        xdb_hdmc: "",
      },
      timestamp: Date.now(),
    };
  },
  components: {
    Tinymce,
    FileUpload,
  },
  methods: {
    getAllActivityType() {
      let data = {
        page: 1,
        limit: 9999,
      };
      hdlb_list(data).then((res) => {
        if (res.code == 1) {
          this.typeList = res.data.page_data;
          let obj = {
            xdbh_id: "",
            xdbh_name: "全部",
          };
          this.typeList.unshift(obj);
        }
      });
    },
    getHdList() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.secondForm.title,
        xdb_bjmc: this.secondForm.bjmc,
        xdbh_id: this.secondForm.type,
      };
      hd_list(data).then((res) => {
        this.loading = false;
        if (res.code == 1) {
          this.secondTable = res.data.page_data;
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getHdList();
    },
    createNew() {
      this.dialogTitle = "发起活动";
      this.newEdit = false;
      this.newForm = {
        xdb_id: "",
        xdb_bjmc: "",
        xdb_hdmc: "",
        xdbh: "",
        xdb_hdsj: [],
        xdb_cjr: "",
        // xdb_shr: '',
        xdb_hddz: "",
        xdb_hdms: "",
      };
      this.newDialogShow = true;
    },
    editAc(item) {
      this.dialogTitle = "活动编辑";
      this.newEdit = true;
      this.newForm = {
        xdb_id: item.xdb_id,
        xdb_bjmc: item.xdb_bjmc,
        xdb_hdmc: item.xdb_hdmc,
        xdbh: {
          label: item.xdbh_name,
          value: item.xdbh_id,
        },
        xdb_hdsj: [item.xdb_hdkssj, item.xdb_hdjssj],
        xdb_cjr: item.xdb_cjr,
        // xdb_shr: item.xdb_shr,
        xdb_hddz: item.xdb_hddz,
        xdb_hdms: item.xdb_hdms,
      };
      this.newDialogShow = true;
    },
    newFormSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          let data = {
            xdb_bjmc: this.newForm.xdb_bjmc,
            xdb_hdmc: this.newForm.xdb_hdmc,
            xdbh_id: this.newForm.xdbh.value,
            xdbh_name: this.newForm.xdbh.label,
            xdb_hdkssj: this.newForm.xdb_hdsj[0],
            xdb_hdjssj: this.newForm.xdb_hdsj[1],
            xdb_cjr: real_name,
            // xdb_shr: this.newForm.xdb_shr,
            xdb_hddz: this.newForm.xdb_hddz,
            xdb_hdms: this.newForm.xdb_hdms,
          };
          if (!this.newEdit) {
            hd_add(data).then((res) => {
              this.loading = false;
              if (res.code == 1) {
                this.$message({
                  message: "活动发起成功！",
                  type: "success",
                });
                this.newDialogShow = false;
                this.getHdList();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            data.xdb_id = this.newForm.xdb_id;
            hd_edit(data).then((res) => {
              this.loading = false;
              if (res.code == 1) {
                this.$message({
                  message: "活动编辑成功！",
                  type: "success",
                });
                this.newDialogShow = false;
                this.getHdList();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          }
        }
      });
    },
    auditAc(item) {
      this.loading = true;
      let data = {
        xdb_id: item.xdb_id,
        xdb_hdzt: "2",
      };
      hd_edit(data).then((res) => {
        this.loading = false;
        if (res.code == 1) {
          this.$message({
            message: "审核成功！",
            type: "success",
          });
          this.loading = false;
          this.getHdList();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    signDialogShow(item) {
      this.signForm.xdb_id = item.xdb_id;
      this.signForm.xdb_hdmc = item.xdb_hdmc;
      this.signForm.xdh_xsxm = "";
      this.signForm.xdh_xsxh = "";
      this.signForm.xdh_sjhm = "";
      this.signDialogVisible = true;
    },
    signFormSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          let data = {
            xdh_xsxm: this.signForm.xdh_xsxm,
            xdh_xsxh: this.signForm.xdh_xsxh,
            xdh_sjhm: this.signForm.xdh_sjhm,
            xdb_id: this.signForm.xdb_id,
            xdb_hdmc: this.signForm.xdb_hdmc,
          };
          sign_add(data).then((res) => {
            this.loading = false;
            if (res.code == 1) {
              this.$message({
                message: "活动报名成功！",
                type: "success",
              });
              this.signDialogVisible = false;
              this.getHdList();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },
    delAc() {
      let del_arr = [];
      for (let i in this.multipleSelection) {
        del_arr.push(this.multipleSelection[i].xdb_id);
      }
      this.$confirm("确定要删除这些活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            xdb_id: del_arr,
          };
          hd_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "活动删除成功！",
                type: "success",
              });
              this.getHdList();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    exportAc() {
      let exp_arr = [];
      for (let i in this.multipleSelection) {
        exp_arr.push(this.multipleSelection[i].xdb_id);
      }
      let link =
        window.globalVariables.api +
        "/xddy/dygl_bjhd/index?api=xls&page=1&limit=10000&token=" +
        token + '&xdb_id=' + exp_arr.join(',');
      window.open(link, "_self");
    },
    toDetail(id) {
      this.$router.push({ name: "bjhd_hdlb_hdxq", params: { id: id } });
    },
  },
  mounted() {
    this.getAllActivityType();
    this.getHdList();
  },
};
</script>

<style scoped>
</style>