<!-- 活动管理ß -->
<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="活动名称：">
        <el-input v-model="form.name" placeholder="请输入活动名称" clearable />
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select
          v-model="form.activityState"
          placeholder="请选择当前状态"
          clearable
        >
          <el-option
            v-for="item in currentOptions"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上下架状态：">
        <el-select
          v-model="form.status"
          placeholder="请选择上下架状态"
          clearable
        >
          <el-option
            v-for="item in disabledOptions"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          plain
          @click="showDialog"
          v-hasPermi="['operation:activity:add']"
          >新增赠送流量</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          plain
          @click="getDetail"
          v-hasPermi="['operation:activity:add']"
          >新增活动</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column
        label="活动id"
        align="center"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        label="活动名称"
        align="center"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column label="活动图片" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="有效时间" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.timeList.join(" - ") }}
        </template>
      </el-table-column>
      <el-table-column
        label="当前状态"
        align="center"
        :formatter="activityFormatter"
        width="150px"
      />
      <el-table-column
        label="上下架状态"
        align="center"
        :formatter="statusFormatter"
        width="150px"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row, true)"
            >查看</el-button
          >
          <el-button
            type="text"
            v-if="scope.row.status === 0"
            @click="getChange(scope.row.id, 1, '下架')"
            v-hasPermi="['operation:activity:down']"
            >下架</el-button
          >
          <span v-else style="margin: 0 10px">
            <el-button
              v-if="scope.row.activityState !== 2"
              type="text"
              @click="getDetail(scope.row)"
              v-hasPermi="['operation:activity:edit']"
              >编辑</el-button
            >
            <el-button
              v-if="scope.row.activityState !== 2"
              type="text"
              @click="getChange(scope.row.id, 0, '上架')"
              v-hasPermi="['operation:activity:up']"
              >上架</el-button
            >
            <el-button
              type="delete"
              @click="getDelete(scope.row)"
              v-hasPermi="['operation:activity:delete']"
              >删除</el-button
            >
          </span>
          <el-button
            type="text"
            v-clipboard:copy="scope.row.copyUrl"
            v-clipboard:success="getCopy"
            v-hasPermi="['operation:activity:copy']"
            >复制链接</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogTitle"
      width="1000px"
      :before-close="getDialogClose"
    >
      <!-- :rules="[
            {
              required: true,
              message: '请输入要赠送设备的ICCID号',
              trigger: 'blur',
            },
            { max: 30, message: '不可超过30个字符', trigger: 'blur' },
          ]" -->
      <el-form :model="dialogData" ref="dialogData" label-width="auto">
        <!-- ICCID号 -->
        <el-form-item label="ICCID号：" prop="iccid">
          <el-input
            v-model="dialogData.iccid"
            placeholder="请输入要赠送设备的ICCID号"
          />
        </el-form-item>

        <!-- 关联活动ID -->
        <el-form-item label="关联活动ID：">
          <el-select
            v-model="dialogData.activityId"
            placeholder="请选择关联活动"
          >
            <el-option
              v-for="item in dialogAcitivityIds"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>

        <!-- 设备型号 -->
        <el-form-item label="设备型号：">
          <el-select v-model="dialogData.deviceId" placeholder="请选择设备型号">
            <el-option
              v-for="item in devOptions"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="getDialogClose">取消</el-button>
        <el-button type="primary" @click="getDialogSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
allNoDeleteList,
change,
doExchange,
list,
} from "@/api/operation/activity";
import { currentMixin, devMixin, disabledMixin } from "@/mixin/index";
export default {
  mixins: [devMixin, currentMixin, disabledMixin],
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增赠送流量",
      dialogAcitivityIds: [],
      dialogData: {},

      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总数据
      total: 0,
      // 列表
      tableData: [],
    };
  },
  mounted() {
    this.getList();
    this.getAllNoDeleteList();
    this.getDevList();
  },

  methods: {
    // 弹窗
    async showDialog() {
      this.dialogVisible = true;
      this.dialogData = {};
      if (this.dialogAcitivityIds.length == 0) {
        this.getAllNoDeleteList();
      }
      if (this.devOptions.length == 0) {
        this.getList();
      }
    },

    // 提交分类表单
    getDialogSubmit() {
      var iccid = this.dialogData.iccid;
      var activityId = this.dialogData.activityId;
      var deviceId = this.dialogData.deviceId;
      var param = {
        iccid: iccid,
        activityId: activityId,
        deviceId: deviceId,
      };

      console.log("操作参数==" + JSON.stringify(param));
      doExchange(param).then((res) => {
        // 89860624650085884202
        console.log("操作结果=" + JSON.stringify(param));
        if (res.code === 0) {
          this.dialogVisible = false;
          this.dialogData = {};
          this.$refs.dialogData.resetFields();
          this.$message.success("操作成功！");
        }
      });
    },

    // 取消
    getDialogClose() {
      this.dialogVisible = false;
      this.dialogData = {};
      this.$refs.dialogData.resetFields();
    },

    getAllNoDeleteList() {
      // 平台ID：灵活配置多个平台的 0-vip会员 1-联通
      var param = { platformId: 1 };
      allNoDeleteList(param).then((res) => {
        if (res.code === 0) {
          this.dialogAcitivityIds = res.data;
        }
      });
    },

    // 列表
    getList() {
      this.loading = true;
      list(this.form).then((res) => {
        console.log("操作结果dasd发==00=" + JSON.stringify(res));
        if (res.code === 0) {
          this.tableData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        }
      });
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1;
      this.getList();
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10,
      };
      this.getList();
    },

    // 详情
    getDetail(row, boolean) {
      this.$router.push({
        path: `/operation/activity/detail`,
        query: {
          id: row.id,
          boolean: boolean,
          activityState: row.activityState,
        },
      });
    },

    // 上下架
    getChange(id, status, title) {
      change(id, status).then((res) => {
        if (res.code === 0) {
          this.$message.success(`${title}成功！`);
          this.getList();
        }
      });
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}?`, "提示", {
        type: "warning",
      }).then(() => {
        change(row.id, 2).then((res) => {
          if (res.code === 0) {
            this.$message.success("删除成功！");
            this.getList();
          }
        });
      });
    },

    // 复制链接
    getCopy() {
      this.$message.success("复制成功！");
    },

    // 字典翻译
    activityFormatter(row) {
      return this.selectDictLabel(this.currentOptions, row.activityState);
    },

    statusFormatter(row) {
      return this.selectDictLabel(this.disabledOptions, row.status);
    },
  },
};
</script>