<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-11-18 10:37:02
 * @LastEditors: 无尘
 * @LastEditTime: 2019-11-22 14:12:14
 -->
<!--
<base-config  @submitNext="submitNext"></base-config>

import baseConfig from '@/components/active-config/base-config.vue';
-->
<template>
  <div class="base-config-content">
    <div class="config-title font-14 color-303133">基本信息</div>
    <el-form
      label-width="114px"
      :model="activeInfoData"
      ref="baseForm"
      :rules="rules"
    >
      <el-form-item
        v-if="$route.query.acitivityId"
        label="活动目的："
        prop=""
      > </el-form-item>
      <el-form-item
        v-if="$route.query.acitivityId"
        label="转化路径："
        prop=""
      > </el-form-item>
      <el-form-item
        v-if="$route.query.acitivityId"
        label="活动营销页："
        prop=""
      > </el-form-item>
      <el-form-item
        v-if="$route.query.acitivityId"
        label="游戏计划："
        prop=""
      > </el-form-item>
      <el-form-item
        v-if="$route.query.acitivityId"
        label="卡券："
        prop=""
      > </el-form-item>
      <el-form-item
        label="活动名称："
        prop="activityName"
      >
        <limitInput
          :inputWidth="350"
          :disflag="$route.query.interfaceId ? true : false"
          :inputValue.sync="activeInfoData.activityName"
          :holder="'请输入活动名称'"
          :getByType="'word'"
          :maxLength="30"
        > </limitInput>
      </el-form-item>
      <el-form-item
        label="活动有效期："
        prop="dateRange"
      >
        <el-date-picker
          class="m-l-10"
          prefix-icon="el-icon-time"
          :picker-options="pickerOptions"
          v-model="activeInfoData.dateRange"
          @change="changeDate"
          :editable="false"
          :value-format="'yyyy-MM-dd HH:mm:ss'"
          :default-time="['00:00:00', '23:59:59']"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        > </el-date-picker>
      </el-form-item>
      <el-form-item class="m-t-50">
        <el-button
          type="primary"
          @click="returnBack"
        >返回</el-button>
        <el-button
          type="primary"
          @click="submitForm('baseForm')"
        >下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import limitInput from '@/components/limit-input.vue';
import { getRequest,postRequest } from '@/api/api';
// import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  name: 'base-config',
  props: {},
  components: {
    limitInput
  },
  data() {
    return {
      activeInfoData: {
        acitivityId: '',
        parentGoalId: '',
        goalId: '',
        activityMarketPageId: '',
        gamePlanId: '',
        cardCoupId: '',
        routerBitValue: '',
        routerBitName: '',
        activityName: '',
        dateRange: []
      },
      rules: {
        activityName: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        dateRange: [{ required: true, message: '请选择活动有效期', trigger: 'blur' }]
      },
      choiceDateCopy: [],
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.choiceDateCopy = [minDate.getTime()];
          if (maxDate) {
            this.choiceDateCopy = [];
          }
        },
        disabledDate: time => {
          if (this.choiceDateCopy.length) {
            const one = 365 * 24 * 3600 * 1000;
            const minTime = this.choiceDateCopy[0] - one;
            const maxTime = this.choiceDateCopy[0] + one;
            return time.getTime() < minTime || time.getTime() > maxTime || time.getTime() < Date.now() - 8.64e6;
          }
          return time.getTime() < Date.now() - 8.64e6;
        }
      },
    };
  },
  methods: {
    /**
     * 日期
     */
    changeDate(e) {
      const that = this;
      console.log(e)
      if (!e) {
        that.choiceDateCopy = [];
        that.activeInfoData.dateRange = [];
      }
    },
    /**
     * 返回
     */
    returnBack() {
      this.$router.go(-1);
    },
    /**
     * 下一步
     */
    submitForm(formName) {
      const that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.$emit('submitNext', that.activeInfoData);
          that.$store.dispatch('saveBaseInfo', that.activeInfoData);
          that.postSave();
        } else {
          return false;
        }
      });
    },
    postSave() {
      const that = this;
      if (!that.activeInfoData.dateRange) {
        that.activeInfoData.dateRange = [];
      }
      let para = {
        activityName: that.activeInfoData.activityName,
        startDate: that.activeInfoData.dateRange[0],
        endDate: that.activeInfoData.dateRange[1],
        parentGoalId: that.activeInfoData.parentGoalId,
        goalId: that.activeInfoData.goalId,
        activityMarketPageId: that.activeInfoData.activityMarketPageId,
        gamePlanId: that.activeInfoData.gamePlanId,
        cardCoupId: that.activeInfoData.cardCoupId,
        routerBitValue: that.activeInfoData.routerBitValue,
        routerBitName: that.activeInfoData.routerBitName
      };
      postRequest('/sharing-core-web/create-activity-base-info', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.$emit('submitNext', that.activeInfoData);
            that.$store.dispatch('saveBaseInfo', that.activeInfoData);
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取基础信息
     * /sharing-core-web/get-activity-base-info
     */
    getBaseData(acitivityId) {
      const that = this;
      let para = {
        acitivityId: acitivityId || that.$route.query.acitivityId
      };
      getRequest('/sharing-core-web/get-activity-base-info', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.activeInfoData = resData.result;
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
  },
  mounted() {
    const that = this;
    that.getBaseData();
  },
};
</script>
<style lang="less" scoped>
.base-config-content {
  padding: 48px 0 0 0;
  .config-title {
    font-weight: 600;
    margin-bottom: 30px;
  }
  .m-t-50 {
    margin-top: 50px;
  }
  .el-form-item {
    /deep/ .el-form-item__label {
      color: #606266;
    }
  }
}
</style>
