<template>
  <t-card :bordered="false">
    <div class="form-step-container">
      <t-button @click="add">新增</t-button>
      <t-table
        rowKey="index"
        :data="list"
        :columns="columns"
        :stripe="true"
        :bordered="false"
        :hover="true"
        size="large"
        table-layout="auto"
        cellEmptyContent="-"
        :pagination="pagination"
      >
        <template #activity_image="{ row }">
          <div style="text-align: center;">
            <t-image
              :src="$store.state.user.apiUrl+row.activity_image"
              fit="contain"
              :style="{ width: '120px',margin:'0 auto'}"
            />
          </div>
        </template>
        <template #select="{ row }">
          <t-space size="24px">
            <t-button @click="openInfo(row)">报名详情</t-button>
            <t-button theme="warning" @click="edit(row)">编辑</t-button>
            <t-popconfirm content="确认删除吗" @confirm="del(row)">
              <t-button theme="danger">删除</t-button>
            </t-popconfirm>
          </t-space>
        </template>
      </t-table>
      <div style="margin-top: 30px">
        <t-pagination
          :total="pagination.total"
          :page-size="pagination.size"
          @current-change="onCurrentChange"
          :showPageSize="false"
        ></t-pagination>
      </div>
    </div>
    <t-dialog header="报名详情" :confirmBtn="null" :visible="infoMode" :onClose="onCloseMy" width="45%" top="50px">
      <t-table
        rowKey="index"
        :data="info_list"
        :columns="info_columns"
        :stripe="true"
        :bordered="false"
        :hover="true"
        size="large"
        table-layout="auto"
        cellEmptyContent="-"
      >
        <template #photo_image="{ row }">
          <img :src="'http://192.168.3.130/'+row.photo_image" style="width: 50px;height: 50px">
        </template>
      </t-table>
      <div style="margin-top: 30px">
        <t-pagination
          :total="info_pagination.total"
          :page-size="info_pagination.size"
          @current-change="onCurrentChangeInfo"
          :showPageSize="false"
        ></t-pagination>
      </div>
    </t-dialog>
    <t-dialog :header="isEdit?'编辑活动':'新增活动'" :visible="addMode" :onClose="onCloseMy" @confirm="onSubmit"
              width="45%" top="20px">
      <t-form>
        <t-form-item label="活动标题" name="activity_name">
          <t-input placeholder="请输入活动标题" v-model="addForm.activity_name"/>
        </t-form-item>
        <t-form-item label="活动简介" name="activity_desc">
          <t-input placeholder="请输入活动简介" v-model="addForm.activity_desc"/>
        </t-form-item>
        <t-form-item label="活动地址" name="activity_location">
          <t-input placeholder="请输入活动地址" v-model="addForm.activity_location"/>
        </t-form-item>
        <t-form-item label="活动简介" name="activity_desc">
          <div style="border: 1px solid #ccc;">
            <!-- 工具栏 -->
            <Toolbar
              style="border-bottom: 1px solid #ccc"
              mode="default"
              :editor="editor"
              :defaultConfig="toolbarConfig"
            />

            <Editor
              style="height: 200px"
              v-model="addForm.activity_content"
              :defaultConfig="editorConfig"
              mode="default"
              @onCreated="(e) => onCreated(e)"
            />
          </div>
        </t-form-item>
        <t-form-item label="活动主图">
          <t-upload
            :action="$store.state.user.apiUrl+'/api/common/upload'"
            v-model="addForm.activity_image_show"
            theme="image"
            tips="请选择单张图片文件上传"
            accept="image/*"
            :format-response="formatResponse"
          ></t-upload>
        </t-form-item>
        <t-form-item label="活动开始时间" name="activity_start_time">
          <t-date-picker :clearable="true" :style="{ width: '480px' }" placeholder="活动开始时间"
                         :enableTimePicker="true" :allow-input="false"
                         v-model="addForm.activity_start_time"></t-date-picker>
        </t-form-item>
        <t-form-item label="活动结束时间" name="activity_end_time">
          <t-date-picker :clearable="true" :style="{ width: '480px' }" placeholder="活动结束时间"
                         :enableTimePicker="true" :allow-input="false"
                         v-model="addForm.activity_end_time"></t-date-picker>
        </t-form-item>
      </t-form>
    </t-dialog>
  </t-card>

</template>
<script lang="ts">
import store from "@/store";
import {Editor, Toolbar} from '@wangeditor/editor-for-vue';

export default {
  components: {Editor, Toolbar},
  data() {
    return {
      visible: false,
      infoMode: false,
      addForm: {
        activity_name: '',
        activity_desc: '',
        activity_image_show: [],
        activity_image: '',
        activity_start_time: '',
        activity_end_time: '',
        activity_content: '',
        activity_location: ''
      },
      addMode: false,
      isEdit: false,
      editID: 0,
      infoId: 0,
      info_list: [],
      info_columns: [
        {colKey: 'photo_image', title: '证件照', align: 'center'},
        {colKey: 'nikename', title: '姓名', align: 'center'},
        {colKey: 'phone', title: '手机号', align: 'center'},
        {colKey: 'position', title: '职位', align: 'center'},
        {colKey: 'application_time', title: '报名时间', align: 'center'},
      ],
      info_pagination: {
        page: 1,
        size: 10,
        total: 0,
      },
      list: [],
      columns: [
        {colKey: 'activity_name', title: '活动标题', align: 'center'},
        {colKey: 'activity_desc', title: '活动简介', align: 'center'},
        {colKey: 'activity_image', title: '活动图片', align: 'center'},
        {colKey: 'activity_start_time', title: '活动开始时间', align: 'center'},
        {colKey: 'activity_end_time', title: '活动结束时间', align: 'center'},
        {colKey: 'activity_location', title: '活动地点', align: 'center'},
        {colKey: 'select', title: '操作', width: 200, align: 'center'},
      ],
      pagination: {
        page: 1,
        size: 10,
        total: 0,
      },
      editor: null,
      toolbarConfig: {
        showLinkImg: false,
        uploadImgShowBase64: true,
        excludeKeys: [
          'insertVideo', // 删除视频
          'uploadVideo',
          'group-video',
          'insertImage',// 删除网络图片上传
          'insertLink',// 删除链接
          'insertTable',// 删除表格
          'codeBlock',// 删除代码块
        ]
      },
      editorConfig: {
        placeholder: '',
        readOnly: false, // 是否只允许阅读，不可编辑
        autoFocus: true,
        MENU_CONF: {
          uploadImage: {
            server: store.state.user.apiUrl + '/api/common/upload',
            fieldName: 'file',
            customInsert(res: any, insertFn: InsertFnType) {  // TS 语法
              // customInsert(res, insertFn) {                  // JS 语法
              // res 即服务端的返回结果
              console.log(res);
              // 从 res 中找到 url alt href ，然后插入图片
              insertFn(res.data.fullurl, '', '')
            },
          }
        }
      },
    }
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  mounted() {
    //this.member_id=this.$route.query.id;
    this.getList();
  },
  methods: {
    onCurrentChangeInfo(d) {
      this.info_pagination.page = d;
      this.openInfo();
    },
    onCurrentChange(d) {
      this.pagination.page = d;
      this.getList();
    },
    openInfo(d) {
      this.infoMode = true;
      this.infoId = d.id;
      this.$request
        .post("/application/find", {
          application_id: this.infoId,
          page: this.info_pagination.page,
          size: this.info_pagination.size
        })
        .then((res) => {
          console.log(res);
          if (res.code == 1) {
            this.info_list = res.data.ret;
            this.info_pagination.total = res.data.count;
          }

        })
        .catch((e) => {
          console.log(e);
        });
    },
    formatResponse(res) {
      console.log(res);
      this.addForm.activity_image = res.data.url;
      return {url: res.data.fullurl};
    },
    onCreated(editor) {
      this.editor = Object.seal(editor);
    },
    del(d) {
      console.log(d);
      this.$request
        .post('/activity/del', {id: d.id})
        .then((res) => {
          if (res.code == 1) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
          console.log(res);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    add() {
      this.addMode = true;
      this.isEdit = false;
    },
    edit(d) {
      console.log(d);
      this.addForm.id = d.id;
      this.addForm.activity_name = d.activity_name;
      this.addForm.activity_desc = d.activity_desc;
      this.addForm.activity_image_show = [{url: store.state.user.apiUrl + d.activity_image}];
      this.addForm.activity_image = d.activity_image;
      this.addForm.activity_start_time = d.activity_start_time;
      this.addForm.activity_end_time = d.activity_end_time;
      this.addForm.activity_location = d.activity_location;
      this.addForm.activity_content = d.activity_content;
      this.addMode = true;
      this.isEdit = true;
    },
    getList() {
      this.$request
        .post("/activity", {page: this.pagination.page, size: this.pagination.size})
        .then((res) => {
          console.log(res);
          if (res.code == 1) {
            this.list = res.data.ret;
            this.pagination.total = res.data.count;
          }

        })
        .catch((e) => {
          console.log(e);
        });
    },
    onSubmit() {
      console.log(this.addForm);
      if (this.addForm.activity_name == '') {
        this.$message.error('活动标题不能为空');
        return;
      }
      if (this.addForm.activity_desc == '') {
        this.$message.error('活动简介不能为空');
        return;
      }
      if (this.addForm.activity_image == '') {
        this.$message.error('活动图片不能为空');
        return;
      }
      if (this.addForm.activity_content == '') {
        this.$message.error('活动详情不能为空');
        return;
      }
      if (this.addForm.activity_start_time == '') {
        this.$message.error('活动开始时间不能为空');
        return;
      }
      if (this.addForm.activity_end_time == '') {
        this.$message.error('活动结束时间不能为空');
        return;
      }
      if (this.addForm.activity_location == '') {
        this.$message.error('活动地址不能为空');
        return;
      }
      console.log(this.addForm);
      var url = '/activity/add';
      if (this.isEdit) {
        url = '/activity/update';
      }
      this.$request
        .post(url, this.addForm)
        .then((res) => {
          console.log(res);
          if (res.code == 1) {
            this.$message.success(res.msg);
            this.addMode = false;
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    onCloseMy() {
      this.addMode = false;
      this.infoMode = false;
    }
  }
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
