<template>
  <div class="companyList tradeactivity">
    <template v-if="isShowAdd">
      <div class="head-title">
        活动管理
        <el-button style="float:right;margin-top: -5px;" size="small" type="primary"
          @click="isShowAdd=false,clearMsg()">新增</el-button>
      </div>
      <div class="content">
        <div>
          <div class="searchInput" style="width:250px">
            <el-input @keyup.enter.native="msg.pageIndex=1,getList()" @clear="msg.pageIndex=1,getList()"
              style="display:inline-block;width:225px;height:30px" placeholder="活动名称" v-model="msg.ActivityName"
              size="small" clearable>
            </el-input>
            <span @click="msg.pageIndex=1,getList()" class="el-icon-search"
              style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
          </div>
        </div>
        <el-table :data="tableData" v-loading="loading" border style="width: 100%;margin:20px 0">
          <el-table-column prop="Id" label="编号" width="100">
          </el-table-column>
          <el-table-column prop="ActivityName" label="活动名称">
          </el-table-column>
          <el-table-column prop="ActivityTypeName" label="活动类型">
          </el-table-column>
          <el-table-column prop="CoverImg" label="封面图">
            <template slot-scope="scope">
              <div class="app-image"
                :style="{backgroundImage:'url(' + scope.row.CoverImg + ')',backgroundSize:'cover'}">
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="StartTimeStr" label="活动时间">
            <template slot-scope="scope">
              <span>{{scope.row.StartTimeStr}}</span><br/>
              <span>{{scope.row.EndTimeStr}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="SignStartTimeStr" label="报名时间">
            <template slot-scope="scope">
              <span>{{scope.row.SignStartTimeStr}}</span><br/>
              <span>{{scope.row.SignEndTimeStr}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ActivityStatusStr" label="活动状态">
          </el-table-column>
          <el-table-column prop="JoinNum" label="报名人数">
            <template slot-scope="scope">
              <el-button type="text" @click="dialogTableVisible = true,getItem(scope.row)">
                {{scope.row.JoinNum}} 人
              </el-button>
            </template>
          </el-table-column>
          <el-table-column prop="Id" width="200" label="操作">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                <img @click="EditType(scope.row)" style="width:32px;height:32px" src="../../assets/img/userman/edit.png"
                  alt="">
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
                <img @click="delType(scope.row)" style="width:32px;height:32px;margin:0 10px"
                  src="../../assets/img/userman/del.png" alt="">
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination style="text-align:right" background @current-change="handleCurrentChange"
          :page-size="msg.pageSize" :current-page.sync="msg.pageIndex" layout="total,prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </template>
    <template v-else>
      <div class="head-title">
        <span @click="isShowAdd=true" style="color:rgb(64, 158, 255);cursor:pointer;">活动管理</span><span
          style="margin:0 9px;color:#C0C4CC">/</span><span>修改活动</span>
      </div>
      <div class="content">
        <el-form label-width="120px" :model="addMsg" :rules="rules" ref="addMsg">
          <el-form-item label="活动名称" class="is-required" prop="ActivityName">
            <el-input type="text" class="w400" v-model="addMsg.ActivityName" size="small"></el-input>
          </el-form-item>
          <el-form-item label="开始时间" class="is-required" prop="StartTime">
            <el-date-picker class="w300" value-format="yyyy-MM-dd" v-model="addMsg.StartTime" type="date" size="small"
              placeholder="开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" class="is-required" prop="EndTime">
            <el-date-picker class="w300" v-model="addMsg.EndTime" type="date" size="small" placeholder="结束时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="报名开始时间" class="is-required" prop="SignStartTime">
            <el-date-picker class="w300" v-model="addMsg.SignStartTime" type="datetime" size="small" value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="报名开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="报名截止时间" class="is-required" prop="SignEndTime">
            <el-date-picker class="w300" v-model="addMsg.SignEndTime" type="datetime" size="small" value-format="yyyy-MM-dd HH:mm:ss" placeholder="报名截止时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="活动类型" class="is-required" prop="ActivityType">
            <el-select class="w300" v-model="addMsg.ActivityType" size="small" placeholder="请选择">
              <el-option :key="0" label="请选择" :value="0"></el-option>
              <el-option v-for="item in activityTypeList" :key="item.Id" :label="item.TypeName" :value="item.Id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="封面图" size="small">
            <el-button @click="isShowImage=true" size="small">选择文件</el-button>
            <div class="app-gallery-item" style="position: relative;width: 100px;margin-top: 10px;border:none;">
              <img v-if="!addMsg.CoverImg || addMsg.CoverImg==''" src="../../assets/img/default.png"
                style="width:80px;height:80px" alt="">
              <img v-else style="width:80px;height:80px" :src="addMsg.CoverImg" alt="">
            </div>
          </el-form-item>
          <el-form-item label="活动详情">
            <UE style="width:750px;" :defaultMsg="defaultMsg" :config="config" :IsMultiple="true" ref="ue"></UE>
          </el-form-item>
        </el-form>
      </div>
      <el-button size="small" style="margin-top:20px;padding:9px 25px;" type="primary" @click="submitform('addMsg')">保存
      </el-button>
    </template>
    <!-- 选择文件 -->
    <el-dialog title="选择文件" :visible.sync="isShowImage" width="1240px">
      <ChooseImg @SelectId="SelectId"></ChooseImg>
    </el-dialog>
    <el-dialog title="报名信息" :visible.sync="dialogTableVisible">
      <el-table :data="joinData">
        <el-table-column property="LinkMan" label="联系人" width="150"></el-table-column>
        <el-table-column property="LinkTel" label="联系电话" width="200"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
  import ChooseImg from "@/components/global/ChooseImg.vue";
  import UE from "@/components/global/UE.vue";
  export default {
    components: {
      ChooseImg,
      UE,
    },
    data() {
      let validataTrade = (rule, value, callback) => {
        if (this.addMsg.ActivityType == 0) {
          return callback(new Error('请选择活动类型'));
        } else {
          callback();
        }
      }
      return {
        defaultMsg: "",
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 350
        },
        msg: {
          pageIndex: 1,
          pageSize: 15,
          ActivityName: '',
        },
        total: 0,
        isShowAdd: true, //是否显示新增
        tableData: [], //数据列表
        isShowImage: false, //是否显示选择图片弹窗
        addMsg: {
          Id: 0, //活动编号(主键)
          ActivityName: "", //活动名称
          StartTime: "", //开始时间
          EndTime: "", //结束时间
          CoverImg: "", //封面图
          ActivityType: 0, //活动类型
          ActivityInfo: "", //活动详情
          SignStartTime: "", //报名开始时间
          SignEndTime: "", //报名截止时间
        },
        activityTypeList: [], //活动类型列表
        dialogTableVisible: false, //是否显示弹窗
        joinData: [], //报名信息
        rules: {
          ActivityName: [{
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }],
          StartTime: [{
            required: true,
            message: '请选择开始时间',
            trigger: 'blur'
          }],
          EndTime: [{
            required: true,
            message: '请选择结束时间',
            trigger: 'blur'
          }],
          ActivityType: [{
            validator: validataTrade,
            trigger: "change",
            required: true
          }],

        },
        pickerBeginDateBefore: {
          disabledDate: time => {
            if (this.addMsg.EndTime == null) {
              return false;
            } else {
              let endTime = new Date(this.addMsg.EndTime)
              return endTime.getTime() < time.getTime()
            }
          }
        },
        pickerSignBeginDateBefore: {
          disabledDate: time => {
            if (this.addMsg.SignEndTime == null) {
              return false;
            } else {
              let endTime = new Date(this.addMsg.SignEndTime)
              return endTime.getTime() < time.getTime()
            }
          }
        },
        pickerBeginDateAfter: {
          disabledDate: time => {
            let startTime = new Date(this.addMsg.StartTime)
            return startTime.getTime() >= time.getTime()
          }
        },
        pickerSignBeginDateAfter: {
          disabledDate: time => {
            let startTime = new Date(this.addMsg.SignStartTime)
            return startTime.getTime() >= time.getTime()
          }
        }
      };
    },
    created() {
      this.getActivityType();
      this.getList();
    },
    methods: {
      getItem(item) {
        this.joinData = [];
        this.joinData = item.CommerceConsultList;
      },
      //获取活动类型列表
      getActivityType() {
        this.apipost("/api/Trade/GetCommerceActivityTypeList", {}, res => {
          if (res.data.resultCode == 1) {
            this.activityTypeList = res.data.data;
          }
        })
      },
      clearMsg() {
        this.addMsg.Id = 0;
        this.addMsg.ActivityName = "";
        this.addMsg.StartTime = "";
        this.addMsg.EndTime = "";
        this.addMsg.CoverImg = "";
        this.addMsg.ActivityType = 0;
        this.addMsg.ActivityInfo = "";
        this.addMsg.SignStartTime = '';
        this.addMsg.SignEndTime = '';
      },
      SelectId(msg) {
        let url = this.getIconLink(msg.url)
        this.addMsg.CoverImg = url
        this.isShowImage = false;
      },
      getList() {
        this.loading = true;
        this.apipost("/api/Trade/GetCommerceActivityPage", this.msg, res => {
          this.loading = false;
          if (res.data.resultCode == 1) {
            this.total = res.data.data.count;
            let pageData = res.data.data.pageData;
            this.tableData = pageData;
          }
        })
      },
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      //修改类型
      EditType(item) {
        this.apipost("/api/Trade/GetCommerceActivity", {
          Id: item.Id
        }, res => {
          if (res.data.resultCode == 1) {
            this.isShowAdd = false;
            var tempData = res.data.data;
            this.addMsg.Id = tempData.Id;
            this.addMsg.ActivityName = tempData.ActivityName;
            this.addMsg.StartTime = tempData.StartTimeStr;
            this.addMsg.EndTime = tempData.EndTimeStr;
            this.addMsg.SignStartTime = tempData.SignStartTimeStr;
            this.addMsg.SignEndTime = tempData.SignEndTimeStr;
            this.addMsg.CoverImg = tempData.CoverImg;
            this.addMsg.ActivityType = tempData.ActivityType;
            this.addMsg.ActivityInfo = tempData.ActivityInfo;
            if (tempData.ActivityInfo) {
              this.defaultMsg = tempData.ActivityInfo;
            }
          } else {
            this.Error(res.data.message);
          }
        })
      },
      //删除类型
      delType(item) {
        let that = this;
        that.Confirm("是否删除?", function () {
          that.apipost(
            "/api/Trade/RemoveCommerceActivity", {
              Id: item.Id,
              Status: 1
            },
            res => {
              if (res.data.resultCode == 1) {
                that.Success(res.data.message);
                that.getList();
              } else {
                that.Error(res.data.message);
              }
            },
          );
        });
      },
      //新增修改
      submitform(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let content = this.$refs.ue.getUEContent();
            this.addMsg.ActivityInfo = content;
            this.apipost("/api/Trade/SetCommerceActivity", this.addMsg, res => {
              if (res.data.resultCode == 1) {
                this.isShowAdd = true;
                this.getList();
                this.clearMsg();
                this.Success(res.data.message);
              } else {
                this.Error(res.data.message);
              }
            })
          } else {
            return false;
          }
        })
      }
    },
    mounted() {}
  };

</script>
<style>
  .companyList .remark_name {
    color: #888888;
    font-size: 12px;
    margin-left: 10px;
    float: right;
  }

  .companyList .app-image {
    background-position: center center;
    width: 50px;
    height: 50px;
    border-radius: 0%;
    float: left;
    margin-right: 8px;
  }

  .companyList .blue {
    color: #409EFF;

  }

  .companyList .content .searchInput {
    border: 1px solid #DCDFE6;
    border-radius: 4px;
  }

  .companyList .content .searchInput .el-input__inner {
    border: none;
    outline: none;
    height: 30px;
    line-height: 30px;
  }

  .companyList .content .searchInput {
    line-height: normal;
    display: inline-table;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    width: 250px;
    margin-right: 20px;
  }

  .companyList .content {
    background: #fff;
    margin-top: 10px;
    padding: 20px;
    box-sizing: border-box;
  }

  .tradeactivity .el-form-item__content {
    line-height: 0;
  }

</style>
