<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="直播间ID" prop="liveId">
        <el-input
          v-model="queryParams.liveId"
          placeholder="请输入直播间ID"
          clearable
          :disabled="liveAbled"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable size="small">
          <el-option v-for="i in taskTypeOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="触发类型" prop="triggerType">-->
<!--        <el-select v-model="queryParams.triggerType" placeholder="请选择触发类型" clearable size="small">-->
<!--          <el-option v-for="i in triggerTypeOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="i in statusOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['live:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['live:task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['live:task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['live:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务类型" align="center" prop="taskType" :formatter="taskTypeFormatter" />
      <el-table-column label="触发类型" align="center" prop="triggerType" :formatter="triggerTypeFormatter" />
      <el-table-column label="触发时间" align="center" prop="triggerValue" :formatter="triggerValueFormatter" />
      <el-table-column label="产品名称" align="center" prop="productName" :formatter="productNameFormatter" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormatter" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['live:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['live:task:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改直播间自动化任务配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="直播间ID" prop="liveId">
          <el-input :disabled="liveAbled" v-model="form.liveId" placeholder="请输入直播间ID" />
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务类型" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择任务类型" @change="updateTaskType()">
            <el-option v-for="i in taskTypeOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品选择" prop="content" v-if="form.taskType == 1">
          <el-select v-model="form.content" placeholder="请选择商品" ref="selectRef" >
            <el-option v-for="i in productOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
            <!-- 加载载中状态 -->
            <div v-if="isLoading" class="loading-indicator">
              <i class="el-icon-loading"></i>
              <span>加载中...</span>
            </div>

            <!-- 没有更多数据 -->
            <div v-if="!hasMore && !isLoading" class="no-more">
              没有更多数据了
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="红包选择" prop="content" v-if="form.taskType == 2">
          <el-select v-model="form.content" placeholder="请选择红包" ref="selectRef" >
            <el-option v-for="i in redOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
            <!-- 加载载中状态 -->
            <div v-if="isLoading" class="loading-indicator">
              <i class="el-icon-loading"></i>
              <span>加载中...</span>
            </div>

            <!-- 没有更多数据 -->
            <div v-if="!hasMore && !isLoading" class="no-more">
              没有更多数据了
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="抽奖选择" prop="content" v-if="form.taskType == 4">
          <el-select v-model="form.content" placeholder="请选择抽奖" ref="selectRef" >
            <el-option v-for="i in lotteryOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
            <!-- 加载载中状态 -->
            <div v-if="isLoading" class="loading-indicator">
              <i class="el-icon-loading"></i>
              <span>加载中...</span>
            </div>

            <!-- 没有更多数据 -->
            <div v-if="!hasMore && !isLoading" class="no-more">
              没有更多数据了
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券" prop="content" v-if="form.taskType == 5">
          <el-select v-model="form.content" placeholder="请选择优惠券" ref="selectCoupon" >
            <el-option v-for="i in couponOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
            <!-- 加载载中状态 -->
            <div v-if="isLoading" class="loading-indicator">
              <i class="el-icon-loading"></i>
              <span>加载中...</span>
            </div>

            <!-- 没有更多数据 -->
            <div v-if="!hasMore && !isLoading" class="no-more">
              没有更多数据了
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="商品选择" prop="goodsId" v-if="form.taskType == 6">
          <el-select v-model="form.goodsId" placeholder="请选择商品" ref="selectGoods" >
            <el-option v-for="i in productOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
            <!-- 加载载中状态 -->
            <div v-if="isLoading" class="loading-indicator">
              <i class="el-icon-loading"></i>
              <span>加载中...</span>
            </div>

            <!-- 没有更多数据 -->
            <div v-if="!hasMore && !isLoading" class="no-more">
              没有更多数据了
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="上下架状态" prop="goodsStatus" v-if="form.taskType == 6">
          <el-select v-model="form.goodsStatus" placeholder="请选择上下架状态">
            <el-option v-for="i in goodsStatusOptions" :key="i.value" :label="i.label" :value="i.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="触发时间" prop="content">
          <el-time-picker
            default-value="2025-01-01 00:00:00"
            v-model="form.triggerValue"
            :picker-options="{
      selectableRange: '00:00:00 - 23:59:59'
    }"
            placeholder="任意时间点">
          </el-time-picker>
        </el-form-item>
<!--        <el-form-item label="触发类型" prop="triggerType">-->
<!--          <el-select v-model="form.triggerType" placeholder="请选择触发类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="触发值" prop="triggerValue">-->
<!--          <el-input v-model="form.triggerValue" placeholder="请输入触发值" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="任务内容">-->
<!--          <editor v-model="form.content" :min-height="192"/>-->
<!--        </el-form-item>-->
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask, exportTask } from "@/api/live/task";
import {listLiveGoods} from "@/api/live/liveGoods";
import {listLiveRedConfOn} from "@/api/live/liveRedConf";
import {listLiveLotteryConfOn} from "@/api/live/liveLotteryConf";
import {listLiveCouponOn} from "@/api/live/liveCoupon";


export default {
  name: "Task",
  data() {
    return {
      taskTypeOptions:[
        {
          value: 1,
          label: "定时卡片推荐商品"
        },
        {
          value: 2,
          label: "定时红包发送"
        },
        {
          value: 4,
          label: "定时抽奖"
        },
        {
          value: 5,
          label: "定时优惠券"
        },
        {
          value: 6,
          label: "定时商品上下架"
        }
      ],
      statusOptions:[{
        value: "0",
        label: "禁用"
      },
        {
          value: "1",
          label: "启用"
        }
      ],
      goodsStatusOptions:[{
        value: 0,
        label: "下架"
      },
        {
          value: 1,
          label: "上架"
        }
      ],
      haveData:{
        goods:false,
        red:false,
        lottery:false,
        coupon:false
      },
      redOptions:[],
      lotteryOptions:[],
      productOptions:[],
      couponOptions:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      liveAbled: false,
      // 直播间自动化任务配置表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        liveId: null,
        taskName: null,
        taskType: null,
        triggerType: null,
        triggerValue: null,
        content: null,
        status: null,
        createdTime: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        liveId: [
          { required: true, message: "直播间ID不能为空", trigger: "blur" }
        ],
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        taskType: [
          { required: true, message: "任务类型：1-定时推送卡片商品 2-定时发送红包 ", trigger: "change" }
        ],
        triggerType: [
          { required: true, message: "触发类型：相对直播开始时间不能为空", trigger: "change" }
        ],
        triggerValue: [
          { required: true, message: "触发值：绝对时间用yyyy-MM-dd HH:mm:ss，相对时间用分钟数不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态：0-禁用 1-启用不能为空", trigger: "blur" }
        ],
        createdTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatedTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      },
      liveId: null,
      listParams: {
        pageNum: 1,
        pageSize: 10,
        liveId: null
      },
      isLoading: false, // 是否正在加载
      hasMore: true, // 是否还有更多数据
      socket:null ,
    };
  },
  watch: {
    // 监听路由的 query 参数变化
    '$route.query': {
      handler(newQuery) {
        if (this.$route.params.liveId) {
          this.liveId = this.$route.params.liveId;
        }else {
          this.liveId = this.$route.query.liveId;
        }
        if(this.liveId == null) {
          return;
        }
        this.liveAbled = true
        if(this.liveId == null) {
          return;
        }
        this.queryParams.liveId = this.liveId;
        this.getList();
      },
      // 初始化时立即执行一次
      immediate: true
    }
  },
  created() {

    this.socket = this.$store.state.liveWs[this.liveId]
  },
  methods: {

    statusFormatter(row, column, value){
      if (!value) return '--'; // 空值处理
      switch ( value){
        case 0:
          return "禁用";
        case 1:
          return "启用";
        default:
          return "--";
      }
    },
    taskTypeFormatter(row, column, value){
      if (!value) return '--'; // 空值处理
      switch (value) {
        case 1:
          return "定时推送卡片商品";
        case 2:
          return "定时发送红包";
        case 4:
          return "定时抽奖";
        case 5:
          return "定时优惠券";
        case 6:
          return "定时商品上下架";
        default:
          return "--";
      }
    },
    triggerTypeFormatter(row, column, value){
      if (!value) return '--'; // 空值处理
      switch (value) {
        case 1:
          return "相对直播开始时间";
        case 2:
          return "相对时间";
        default:
          return "--";
      }
    },
    productNameFormatter(row, column, value){
      let content = JSON.parse(row.content)
      if(content.productName) {
        return content.productName
      }
      return "--";
    },
    triggerValueFormatter(row, column, value) {
      if (!value) return '--'; // 空值处理

      // 创建日期对象（兼容时间戳和字符串）
      let date;
      if (typeof value === 'number') {
        // 处理时间戳（注意：如果是10位时间戳需要乘以1000）
        date = new Date(value.toString().length === 10 ? value * 1000 : value);
      } else if (typeof value === 'string') {
        // 处理字符串格式（尝试直接转换）
        date = new Date(value);
      } else {
        return '格式错误';
      }

      // 检查日期是否有效
      if (isNaN(date.getTime())) {
        return '无效时间';
      }
      // 格式化日期为 "yyyy-MM-dd HH:mm:ss"
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');

      return `${hours}:${minutes}:${seconds}`;
    },
    async updateTaskType(){
      this.hasMore = true;
      this.listParams = {
        pageNum: 1,
        pageSize: 10,
        liveId: null
      }
      if (this.isLoading || !this.hasMore) return;
      this.isLoading = true;
      this.listParams.liveId = this.liveId;
      if(this.listParams.liveId == null) {
        this.$message.error("页面错误，请联系管理员");
        return;
      }

      try{
        if (this.form.taskType == 1) {
          await this.addGoodsList();
        }else if (this.form.taskType == 2) {
          await this.addRedList();
        }else if (this.form.taskType == 4) {
          await this.addLotteryList();
        }else if(this.form.taskType == 5){
          await this.addCouponList();
        }else if(this.form.taskType == 6){
          await this.addGoodsList();
        }
      }catch ( err){
        console.error('加载数据失败:', err);
      }finally {
        this.isLoading = false;
      }
    },
    addGoodsList() {
      if(this.haveData.goods) return
      listLiveGoods(this.listParams).then(res => {
        if(res.rows.length > 0) {
          res.rows.forEach(item => {
            // 根据productName和goodsId组装成为label和value
            this.productOptions.push({
              value: item.goodsId,
              label: item.productName
            })
          })
          this.listParams.pageNum++;
          // 判断是否还有更多数据
          this.hasMore = this.productOptions.length < res.total;
          if (this.hasMore) {
            this.addGoodsList();
          } else {
            this.haveData.goods = true;
          }
        } else {
          this.hasMore = false;
          this.haveData.goods = true;
        }
      });
    },
    addRedList() {
      if(this.haveData.red) return
      listLiveRedConfOn(this.listParams).then(res => {
        if(res.rows.length > 0) {
          res.rows.forEach(item => {
            // 根据productName和goodsId组装成为label和value
            this.redOptions.push({
              value: item.redId,
              label: item.desc
            })
          })
          this.listParams.pageNum++;
          // 判断是否还有更多数据
          this.hasMore = this.redOptions.length < res.total;
          if (this.hasMore) {
            this.addRedList();
          } else {
            this.haveData.red = true
          }
        } else {
          this.haveData.red = true
          this.hasMore = false;
        }
      });
    },
    addLotteryList() {
      if(this.haveData.lottery) return
      listLiveLotteryConfOn(this.listParams).then(res => {
        if(res.rows.length > 0) {
          res.rows.forEach(item => {
            // 根据productName和goodsId组装成为label和value
            this.lotteryOptions.push({
              value: item.lotteryId,
              label: item.desc
            })
          })
          this.listParams.pageNum++;
          // 判断是否还有更多数据
          this.hasMore = this.lotteryOptions.length < res.total;
          if (this.hasMore) {
            this.addLotteryList();
          } else {
            this.haveData.lottery = true
          }
        } else {
          this.haveData.lottery = true
          this.hasMore = false;
        }
      });
    },
    addCouponList() {
      if(this.haveData.coupon) return
      listLiveCouponOn(this.listParams).then(res => {
        if(res.rows.length > 0) {
          res.rows.forEach(item => {
            // 根据productName和goodsId组装成为label和value
            this.couponOptions.push({
              value: item.couponId,
              label: item.title
            })
          })
          this.listParams.pageNum++;
          // 判断是否还有更多数据
          this.hasMore = this.couponOptions.length < res.total;
          if (this.hasMore) {
            this.addCouponList();
          } else {
            this.haveData.coupon = true
          }
        } else {
          this.haveData.coupon = true
          this.hasMore = false;
        }
      });
    },
    /** 查询直播间自动化任务配置列表 */
    getList() {
      if(this.liveId == null) {
        this.$message.error("页面错误，请联系管理员");
        return;
      }
      this.loading = true;
      listTask(this.queryParams).then(res => {

        this.taskList = res.rows;

        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        liveId: this.liveId,
        taskName: null,
        taskType: null,
        triggerType: null,
        triggerValue: null,
        content: null,
        goodsId: null,
        goodsStatus: null,
        status: 1,
        createdTime: null,
        updatedTime: null
      };
      this.listParams={
          pageNum: 1,
          pageSize: 10,
          liveId: this.liveId
      }
      this.hasMore = true;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加直播间自动化任务配置";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      if(row.taskType == 1){
        await this.addGoodsList();
      }else if(row.taskType == 2){
        await this.addRedList();
      }else if(row.taskType == 4){
        await this.addLotteryList();
      }else if(row.taskType == 5) {
        await this.addCouponList();
      }else if(row.taskType == 6) {
        await this.addGoodsList();
      }
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        let content = JSON.parse( response.data.content)
        if (this.form.taskType == 1) {
          this.form.content = content.goodsId;
        }else if (this.form.taskType == 2) {
          this.form.content = content.redId;
        }else if (this.form.taskType == 4) {
          this.form.content = content.lotteryId;
        }else if(this.form.taskType == 5){
          this.form.content = content.couponId;
        }else if(this.form.taskType == 6){
          this.form.goodsId = content.goodsId;
          this.form.goodsStatus = content.status;
        }
        this.open = true;
        this.title = "修改直播间自动化任务配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.liveId = this.liveId;
      if(this.liveId == null) {
        this.msgError("请选择直播间");
        return;
      }
      if(this.form.taskType == 6){
        if(this.form.goodsId == null) {
          this.msgError("请选择商品");
          return;
        }
        this.form.content = JSON.stringify({
          goodsId: this.form.goodsId,
          status: this.form.goodsStatus
        })

      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(this.socket == null) {
        this.$message.error("请进入直播间后，在进行操作！")
        return;
      }
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除直播间自动化任务配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTask(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
          const msg={
            cmd:'delAutoTask',
            data:row.absValue,
            liveId:this.liveId,
            userType:1
          }
          this.socket.send(JSON.stringify( msg))
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有直播间自动化任务配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTask(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
  }
};
</script>

<style scoped>
.loading-indicator {
  padding: 10px;
  text-align: center;
  color: #606266;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-indicator .el-icon-loading {
  margin-right: 5px;
  animation: rotate 1s linear infinite;
}

.no-more {
  padding: 10px;
  text-align: center;
  color: #909399;
  font-size: 12px;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>
