<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="活码名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入活码名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="启用" :value="1" />
          <el-option label="停用" :value="0" />
        </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"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupLiveCodeList" border>
      <el-table-column label="活码名称" align="center" prop="groupName" />
      <el-table-column label="实际二维码数量" align="center" prop="qrcodeNum" />
      <el-table-column label="今日加群数" align="center" prop="toDayNum" />
      <el-table-column label="累计加群数" align="center" prop="countNum" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <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)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleStatusChange(scope.row)"
          >{{ scope.row.status === 1 ? '停用' : '启用' }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-arrow-right"
            @click="handleGotoActual(scope.row)"
          >群实际码</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 @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活码名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入活码名称" />
        </el-form-item>
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择项目" @change="handleProjectChange">
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id"
            />
          </el-select>
        </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 { listGroupLiveCode, getGroupLiveCode, addOrUpdateGroupLiveCode, delGroupLiveCode } from "@/api/qw/groupLiveCode";
import { pageProject } from "@/api/adv/project";

export default {
  name: "GroupLiveCode",
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      groupLiveCodeList: [],
      projectList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupName: null,
        status: null,
      },
      form: {},
      rules: {
        groupName: [
          { required: true, message: "活码名称不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "项目不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询活码列表 */
    getList() {
      this.loading = true;
      listGroupLiveCode(this.queryParams).then(response => {
        this.groupLiveCodeList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },

    /** 加载项目列表 */
    loadProjectList() {
      pageProject({ pageNum: 1, pageSize: 1000 }).then(response => {
        this.projectList = response.data.records;
      }).catch(() => {
        this.projectList = [];
      });
    },

    /** 处理项目变更 */
    handleProjectChange() {
      const project = this.projectList.find(p => p.id === this.form.projectId);
      if (project) {
        this.form.projectName = project.projectName;
      }
    },

    /** 新增按钮 */
    handleAdd() {
      this.reset();
      this.loadProjectList();
      this.open = true;
      this.title = "新增活码";
    },

    /** 修改按钮 */
    handleUpdate(row) {
      this.reset();
      this.loadProjectList();
      this.open = true;
      this.title = "修改活码";
      getGroupLiveCode(row.id).then(response => {
        this.form = response.data;
      });
    },

    /** 启用/停用 */
    handleStatusChange(row) {
      const status = row.status === 1 ? 0 : 1;
      const statusText = status === 1 ? "启用" : "停用";
      this.$confirm(`确认要${statusText}该活码吗？`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const data = {
          id: row.id,
          status: status
        };
        addOrUpdateGroupLiveCode(data).then(response => {
          this.msgSuccess(`${statusText}成功`);
          this.getList();
        });
      }).catch(() => {});
    },

    /** 进入群实际码页面 */
    handleGotoActual(row) {
      this.$router.push({
        path: '/qw/contactWayC/groupActual',
        query: { liveCodeId: row.id }
      });
    },

    /** 表单提交 */
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          addOrUpdateGroupLiveCode(this.form).then(response => {
            this.msgSuccess(this.form.id ? "修改成功" : "新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    /** 取消 */
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 表单重置 */
    reset() {
      this.form = {
        id: null,
        groupName: null,
        projectId: null,
        projectName: null,
        status: 1
      };
      this.resetForm("form");
    },

    /** 搜索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
