<template>
  <div class="apaas_detail_container">
    <div class="apass_breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/message' }">
          {{ $t("lang.message") }}
        </el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/message/message_alert' }">
          {{ $t("lang.message_alert") }}
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          新建
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="main_contaner locked_height">
      <steps
        :active-step="step"
        :done="done"
        :show-done="true"
        done-title="推送成功"
        done-sub-title="用户可通过用户中心-消息通知查收推送内容"
        class="apaas_steps"
      >
        <step
          title="选择消息模板"
          :step="0"
          :active-icon="require('@/assets/imgs/progress_ic_xiaoximb.png')"
          class="apaas_step"
        >
          <select-template
            class="apaas_step_content"
            :edit="true"
            @change="selectTemplate"
          ></select-template>

          <div class="apaas_button apaas_step_action">
            <el-button type="defalut" @click="backToList">
              取消
            </el-button>
            <el-button type="primary" @click="step1Action">
              下一步
            </el-button>
          </div>
        </step>
        <step
          title="选择目标用户"
          :step="1"
          :active-icon="require('@/assets/imgs/progress_ic_renyuan.png')"
          class="apaas_step"
        >
          <select-users
            class="apaas_step_content"
            @change="selectTargetUser"
          ></select-users>

          <div class="apaas_button apaas_step_action">
            <el-button type="defalut" @click="backToList">
              取消
            </el-button>
            <span>
              <el-button type="defalut" @click="step = 0">
                上一步
              </el-button>
              <el-button type="primary" @click="step2Action">
                完成选择
              </el-button>
            </span>
          </div>
        </step>

        <template slot="action">
          <el-button type="primary" @click="backToList">
            返回列表
          </el-button>
        </template>
      </steps>
    </div>
  </div>
</template>

<script>
import Steps from "@/components/app-build-steps/app-build-steps";
import Step from "@/components/app-build-steps/app-build-step";
import SelectTemplate from "@/components/message-steps/select-template";
import SelectUsers from "@/components/message-steps/select-users";

export default {
  components: {
    Steps,
    Step,
    SelectTemplate,
    SelectUsers,
  },
  data() {
    return {
      step: 0,
      done: false,
      templateId: "",
      templateContent:"",
      targetUserIds: "",
    };
  },
  computed: {},
  created() {},
  methods: {
    backToList() {
      this.$router.push("/message/message_alert");
    },
    selectTemplate(value) {
      this.templateId = (value && value.id) || "";
      this.templateContent = (value && value.content) || "";
    },
    selectTargetUser(values) {
      this.targetUserIds = values.map((item) => item.id).join(",") || "";
    },
    step1Action() {
      if (this.templateId === "") {
        this.$message({
          message: "您尚未选择消息模板",
          type: "warning",
        });

        return;
      }

      this.step = 1;
    },
    step2Action() {
      if (this.targetUserIds === "") {
        this.$message({
          message: "您尚未选择目标用户",
          type: "warning",
        });

        return;
      }

      this.$http
        .post("/apaas/service/v3/push/alertmsg/create", {
          tpl_id: this.templateId,
          contents: this.templateContent,
          receive_user: this.targetUserIds,
        })
        .then(({ data }) => {
          if (data.success == 1) {
            this.$message({
              message: "创建成功",
              type: "success",
            });
            this.done = true;
          } else {
            this.$message({
              message: data.errMsg || "创建失败",
              type: "warning",
            });
          }
        })
        .catch((error) => {
          console.log(error);

          this.$message({
            message: "创建失败",
            type: "warning",
          });
        });
    },
  },
};
</script>

<style scoped>
.main_contaner {
  padding: 20px;
}
</style>
