<template>
  <div class="dm-wrap" v-loading="loading">
    <p class="title mb30">手工导入群发</p>
    <el-form ref="form" :model="ruleForm" label-width="120px" :rules="rules">
      <!-- <el-form-item label="营销场景">
        <span v-if="isInfo">{{ ruleForm.sceneSettingName }}</span>
        <el-select v-else class="w250" v-model="ruleForm.sceneSettingId" placeholder="选择营销场景">
          <el-option v-for="item in sceneSettingIdOptions" :key="item.sceneSettingId" :label="item.sceneName" :value="item.sceneSettingId"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="发送时间">
        <el-select class="w250" v-model="ruleForm.sendType">
          <el-option v-for="(v, i) in sendTypeOptions" :key="i" :value="v.value" :label="v.label"></el-option>
        </el-select>
        <el-date-picker class="w250" v-if="ruleForm.sendType" v-model="ruleForm.sendTime" type="datetime" placeholder="选择日期时间" align="right" :picker-options="pickerOptions"></el-date-picker>
      </el-form-item>
      <el-form-item label="上传文件">
        <div>
          <div v-if="fileId" class="inline-block">已导入</div>
          <file v-if="!fileId" needExtraParams :paramsObj="{ smsImportPlanId: ruleForm.smsImportPlanId }" ref="fileUpload" class="dash-btn inline-block" label="点击上传" @backImg="backImg" acceptType=".xls, .xlsx" tips="" :url="uploadUrl" width="50" iconClass="el-icon-upload2" /><!-- 上传 -->
          <!-- <el-button icon="el-icon-upload2" class="dash-btn">点击上传</el-button> -->
          <el-button type="text" class="ml10" @click="download">点击下载文件模板</el-button>
        </div>
        <p style="margin-top:8px;color:#909399;font-size:12px;line-height:1;">手机号数量单次最多不超过5000条</p>
      </el-form-item>
    </el-form>
    <p class="title mb15 mt40">短信模板设置</p>
    <div class="dm-title__label--tips" style="margin-top:15px;margin-bottom:20px;"><i class="el-icon-warning pr8 warning-color"></i>短信运营商限制：为避免骚扰用户，营销短信只允许在8点到22点发送</div>
    <sms-temp pbSize="pb15" :activeId.sync="ruleForm.gicSmsTemplateId" @emitSmsItemInfo="onSmsItemInfo">
      <span slot="tips" class="fz12 gray">* 请注意若发送的短信模板中包含会员变量，但发送对象无该变量时则显示横杆“-”</span>
    </sms-temp>
    <div class="text-center">
      <el-button type="primary" @click="submitForm">提 交</el-button>
      <el-button @click="$router.go(-1)">返 回</el-button>
    </div>
  </div>
</template>

<script>
import { _debounce } from '@/utils/index';
import smsTemp from '@/components/libs/smsTemp';
import { formatDateTimeByType } from '@/utils/index.js';
import file from '@/components/upload/file';
import { getSmsImportPlanDetail, saveImportSmsPlan, importSmsSendPhone, downloadOImportSmsTemp } from '@/service/api/messageApi.js';

import { sceneSettingList } from '@/service/api/wechatApi.js';
export default {
  name: 'import-send-form',
  data() {
    return {
      loading: false,
      ruleForm: {
        smsImportPlanId: this.$route.query.id,
        sceneSettingName: '',
        sceneSettingId: '',
        sendTime: '',
        sendType: 0,
        gicSmsTemplateId: ''
      },
      fileId: '',
      uploadUrl: importSmsSendPhone,
      rules: {},
      pickerOptions: {
        disabledDate(val) {
          return Date.now() >= val.getTime() + 24 * 60 * 60 * 1000;
        }
      },
      sceneSettingIdOptions: [],
      sendTypeOptions: [{ value: 0, label: '立即发送' }, { value: 1, label: '选择时间发送' }], // eslint-disable-line
    };
  },
  components: {
    smsTemp,
    file
  },
  methods: {
    formatDateTimeByType,
    download() {
      window.location = downloadOImportSmsTemp;
    },
    onSmsItemInfo(val) {
      this.smsType = val.type;
    },
    submitForm: _debounce(function() {
      if (this.loading) {
        return;
      }
      if (this.ruleForm.sendType == 1 && !this.ruleForm.sendTime) {
        this.$tips({ type: 'warning', message: '请设置发送时间' });
        return;
      }
      if (this.ruleForm.sendType == 1 && new Date().getTime() + 1000 * 60 * 5 >= new Date(this.ruleForm.sendTime)) {
        this.$tips({ type: 'warning', message: '定时发送的时间点必须晚于当前时间5分钟后' });
        return;
      }
      if (!this.ruleForm.gicSmsTemplateId) {
        this.$tips({ type: 'warning', message: '请选择短信模板' });
        return;
      }
      this.loading = true;
      const params = { ...this.ruleForm };
      if (params.sendType) {
        params.sendTime = formatDateTimeByType(this.ruleForm.sendTime, 'yyyy-MM-dd-HH-mm-ss');
      } else {
        params.sendTime = '';
      }
      saveImportSmsPlan(params)
        .then(res => {
          this.loading = false;
          this.$message({ message: `新增成功`, type: 'success' });
          this.$router.push('/message/import-send');
        })
        .catch(() => {
          this.loading = false;
        });
    }),
    async sceneSettingList() {
      let res = await sceneSettingList();
      this.sceneSettingIdOptions = res.result || [];
    },
    async getSmsImportPlanDetail() {
      let res = await getSmsImportPlanDetail();
      this.ruleForm.smsImportPlanId = res.result;
    },
    // ****上传图片***
    upload() {
      this.$refs.fileUpload.$el.querySelector('input').click();
    },
    backImg(res) {
      this.fileId = new Date().getTime(); // 代表以及上传过文件
    }
  },
  mounted() {
    this.sceneSettingList();
    this.getSmsImportPlanDetail();
  },
  created() {
    this.$store.commit('mutations_breadcrumb', [{ name: '营销管理', path: '' }, { name: '短信营销', path: '' }, { name: this.info ? '手工导入群发详情' : '新建导入群发', path: '' }]); // eslint-disable-line
  }
};
</script>

<style lang="scss" scoped>
.mt40 {
  margin-top: 40px;
}
.title {
  font-size: 16px;
  font-weight: 500;
  color: rgba(48, 49, 51, 1);
  line-height: 22px;
}
.dash-btn {
  width: 174px;
  border-style: dashed;
  background: rgba(255, 255, 255, 1);
  border-radius: 4px;
}
.dm-title__label--tips {
  background: #fffbe6;
  border-color: #ffe58f;
  margin-right: 0px;
}
/deep/ .dash-btn .el-button {
  width: 174px;
  border-style: dashed;
  border-radius: 4px;
  &:not(.is-disabled) {
    background: white;
    color: #606266;
    border-color: #c0c4cc;
  }
  &.is-disabled {
    color: #c0c4cc;
  }
}
</style>
