<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.activity_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menu" slot-scope="{row}">
        <el-button type="text" size="mini" icon="el-icon-s-comment" @click="show(row,1)">报名列表</el-button>
      </template>
      <template slot="content" slot-scope="{row}">
        <el-button size="mini" @click="show(row,2)">查看活动内容</el-button>
      </template>
      <template slot="activityTime" slot-scope="{row}">
        <el-tag>{{ row.beginTime + ' - ' + row.endTime }}</el-tag>
      </template>
      <template slot="peopleCount" slot-scope="{row}">
        <el-tooltip class="item" effect="dark" :content="'剩余名额:' + row.remainingCount" placement="top">
          <el-tag>{{ row.peopleCount }}</el-tag>
        </el-tooltip>
      </template>
      <template slot="activityStatus" slot-scope="scope">
        <el-tag v-if="scope.row.activityStatus===0" type="info">待开始</el-tag>
        <el-tag v-if="scope.row.activityStatus===1" type="success">已开始</el-tag>
        <el-tag v-if="scope.row.activityStatus===2" type="danger">已结束</el-tag>
      </template>
      <!--      <template slot="activityStatus" slot-scope="{row}">-->
      <!--        <el-switch v-model="row.activityStatus"-->
      <!--                   :active-value=1 active-text="开启"-->
      <!--                   :inactive-value=0 inactive-text="关闭"-->
      <!--                   @change="statusChange(row)">-->
      <!--        </el-switch>-->
      <!--      </template>-->
    </avue-crud>

    <el-drawer :title="form.name" :visible.sync="showEnroll" size="23%" direction="ltr"
               :modal-append-to-body="false" destroy-on-close append-to-body center >
      <activityenroll :activity-id="activityId"></activityenroll>
    </el-drawer>

    <!--    <el-dialog :visible.sync="showContent" width="30%" :modal-append-to-body="false" append-to-body center>-->
    <!--      <avue-ueditor v-model="form.content" :upload="upload" disabled></avue-ueditor>-->
    <!--    </el-dialog>-->
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/party/activity";
  import {mapGetters} from "vuex";
  // import AvueUeditor from 'avue-plugin-ueditor';
  import form from "@/views/util/form";

  export default {
    // comments: {
    //   AvueUeditor,
    // },
    props: {
      branchId: {
        type: String
      }
    },
    data() {
      let _this = this;
      return {
        upload: {
          action: '/api/blade-resource/oss/endpoint/put-file',
          props: {
            res: "data",
            url: "link",
          }
        },
        showEnroll: false,
        showContent: false,
        activityId: '',
        content: '',
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: false,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          // labelWidth: 120,
          menuWidth:450,
          column: [
            {
              label: "活动名称",
              prop: "name",
              row: true,
              // width: 200,
              search: true,
              rules: [{
                required: true,
                message: "请输入活动名称",
                trigger: "blur"
              }]
            },
            {
              label: "活动图片",
              prop: "img",
              type: 'upload',
              hide: true,
              row: true,
              listType: 'picture-img',
              propsHttp: {
                res: "data",
                url: "link"
              },
              action: "/api/blade-resource/oss/endpoint/put-file",
              tip: '只能生产jpg/png图片，且不超过500kb',
              display: true
            },
            // {
            //   label: "活动内容",
            //   prop: "content",
            //   component: 'AvueUeditor',
            //   options: {
            //     action: '/api/blade-resource/oss/endpoint/put-file',
            //     props: {
            //       res: "data",
            //       url: "link",
            //     }
            //   },
            //   minRows: 6,
            //   width: 150,
            //   span: 18,
            //   row: true,
            //   slot: true,
            //   rules: [{
            //     required: true,
            //     message: "请输入活动内容",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "活动时间",
              prop: "activityTime",
              type: "datetimerange",
              format: "yyyy-MM-dd hh:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              // width: 290,
              row: true,
              slot: true,
              change: ({value, column}) => {
                _this.form.beginTime = value[0];
                _this.form.endTime = value[1];
              },
              rules: [{
                required: true,
                message: "请输入活动开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "报名截止时间",
              prop: "deadlineTime",
              type: "datetime",
              // width: 160,
              row: true,
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              rules: [{
                required: true,
                message: "请输入报名截止时间",
                trigger: "blur"
              }]
            },
            {
              label: "上限报名人数",
              prop: "peopleCount",
              type: "number",
              row: true,
              // width: 120,
              slot: true,
              rules: [{
                required: true,
                message: "请输入上限报名人数",
                trigger: "blur"
              }]
            },
            {
              label: "验证身份证",
              prop: "idVerification",
              type: "radio",
              // width: 100,
              row: true,
              search: true,
              searchLabelWidth: 100,
              // dicUrl: "/api/blade-system/dict-biz/dictionary?code=yes_no",
              dicData:[
                {
                  label:'是',
                  value:1
                },{
                  label: '否',
                  value:0
                }
              ],
              dataType: "number",
              // props: {
              //   label: "dictValue",
              //   value: "dictKey",
              // },
              rules: [{
                required: true,
                message: "请输入是否需要验证身份证",
                trigger: "blur"
              }]
            },
            {
              label: "活动状态",
              prop: "activityStatus",
              type: "select",
              // width: 150,
              row: true,
              search: true,
              slot: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=on_off",
              dataType: "number",
              value: 0,
              props: {
                label: "dictValue",
                value: "dictKey",
              },
              rules: [{
                required: true,
                message: "请选择活动状态",
                trigger: "blur"
              }]
            },
            {
              label: "排序",
              prop: "sort",
              row: true,
              hide:true,
              // width: 100,
              rules: [{
                required: false,
                message: "请输入排序",
                trigger: "blur"
              }]
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.activity_add, false),
          viewBtn: this.vaildData(this.permission.activity_view, false),
          delBtn: this.vaildData(this.permission.activity_delete, false),
          editBtn: this.vaildData(this.permission.activity_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      statusChange(row) {
        row.content = btoa(unescape(encodeURIComponent(row.content)));
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          console.log(error);
        });
      },
      show(row, type) {
        //type : 1、报名   2、活动内容
        console.log(type)
        this.form = row;
        this.activityId = row.id;
        switch (type) {
          case 1:
            this.showEnroll = true;
            break;
          case 2:
            this.showContent = true;
            break;
          default:
            break;
        }
      }
      ,
      rowSave(row, done, loading) {
        row.branchId = this.branchId;
        row.content = btoa(unescape(encodeURIComponent(row.content)));
        row.remainingCount = row.peopleCount;
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        row.content = btoa(unescape(encodeURIComponent(row.content)));
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
            this.form.activityTime = [res.data.data.beginTime, res.data.data.endTime];
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        this.query.branchId = this.branchId
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>

</style>
