<template>
  <div class="supervision-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :model="searchForm" inline>
        <el-form-item label="提醒名称">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入项目名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="提醒类型">
          <el-select v-model="searchForm.type" placeholder="全部" clearable>
            <el-option label="全部" value="" />
            <el-option label="申报任务即将结束" value="1" />
            <el-option label="项目即将建设完成" value="2" />
            <el-option label="全局自定义通知" value="3" />
            <el-option label="项目自定义通知" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tablebox">
      <div class="operation-area">
        <el-button
          type="primary"
          @click="handleAdd"
          icon="el-icon-circle-plus-outline"
          >添加规则</el-button
        >
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" />
        <el-table-column prop="id" label="序号" width="80" align="center" />
        <el-table-column
          prop="name"
          label="提醒名称"
          min-width="200"
          align="center"
        />
        <el-table-column
          prop="type"
          label="提醒类型"
          min-width="150"
          align="center"
        />
        <el-table-column
          prop="target"
          label="提醒对象"
          min-width="120"
          align="center"
        />
        <el-table-column
          prop="mode"
          label="提醒方式"
          min-width="120"
          align="center"
        />
        <el-table-column
          prop="time"
          label="提醒时间"
          min-width="180"
          align="center"
        />
        <el-table-column
          prop="user"
          label="提醒用户"
          min-width="120"
          align="center"
        />
        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetail(scope.row)"
              >详情</el-button
            >
            <el-button
              type="text"
              @click="handleEdit(scope.row)"
              style="color: #67c23a"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="handleDelete(scope.row)"
              style="color: red"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="pagination.total > 0"
        :total="pagination.total"
        :page.sync="pagination.currentPage"
        :limit.sync="pagination.pageSize"
        @pagination="handleSearch"
      />
    </div>

    <!-- 添加规则弹框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        size="small"
      >
        <el-form-item label="提醒规则名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入提醒名称" />
        </el-form-item>
        <el-form-item label="提醒对象" prop="target">
          <el-radio-group v-model="ruleForm.target">
            <el-radio label="政务用户">政务用户</el-radio>
            <el-radio label="企业用户">企业用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提醒方式" prop="mode">
          <el-radio-group v-model="ruleForm.mode">
            <el-radio label="定期提醒">定期提醒</el-radio>
            <el-radio label="不定期提醒">不定期提醒</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提醒方法" prop="method">
          <el-radio-group v-model="ruleForm.method">
            <el-radio label="邮件提醒">邮件提醒</el-radio>
            <el-radio label="即时提醒">即时提醒</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提醒时间" prop="time">
          <el-date-picker
            v-model="ruleForm.time"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="提醒内容" prop="content">
          <el-input
            v-model="ruleForm.content"
            type="textarea"
            :rows="4"
            placeholder="请输入提醒内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Supervision",
  components: {
    Pagination: () => import("@/components/Pagination"),
  },
  data() {
    return {
      searchForm: {
        name: "",
        type: "",
      },
      tableData: [],
      pagination: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
      // 选中的行数据
      selectedRows: [],
      // 弹框显示控制
      dialogVisible: false,
      // 表单数据
      ruleForm: {
        name: "",
        target: "政务用户",
        mode: "定期提醒",
        method: "邮件提醒",
        time: "",
        content: "",
      },
      dialogTitle: "",
      rules: {
        name: [
          { required: true, message: "请输入提醒规则名称", trigger: "blur" },
        ],
        target: [
          { required: true, message: "请选择提醒对象", trigger: "change" },
        ],
        mode: [
          { required: true, message: "请选择提醒方式", trigger: "change" },
        ],
        method: [
          { required: true, message: "请选择提醒方法", trigger: "change" },
        ],
        time: [
          { required: true, message: "请选择提醒时间", trigger: "change" },
        ],
        content: [
          { required: true, message: "请输入提醒内容", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      getSmartReminderPage(this.searchForm).then((res) => {
        console.log(res);
      });
    },
    // 搜索方法
    handleSearch() {
      // TODO: 实现搜索逻辑
      console.log("搜索条件：", this.searchForm);
    },
    // 重置方法
    handleReset() {
      this.searchForm.name = "";
      this.searchForm.type = "";
      this.handleSearch();
    },
    // 打开添加规则弹框
    handleAdd() {
      this.dialogVisible = true;
      this.ruleForm = {
        name: "",
        target: "政务用户",
        mode: "定期提醒",
        method: "邮件提醒",
        time: "",
        content: "",
      };
      this.dialogTitle = "新增智能提醒规则";
    },
    // 表格选择改变
    handleSelectionChange(val) {
      this.selectedRows = val;
    },
    // 查看详情
    handleDetail(row) {
      console.log("查看详情：", row);
    },
    // 修改
    handleEdit(row) {
      this.dialogTitle = "修改智能提醒规则";
    },
    // 删除
    handleDelete(row) {
      this.$confirm("确认删除该提醒吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // TODO: 实现删除逻辑
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    // 取消添加
    cancelAdd() {
      this.dialogVisible = false;
      this.$refs.ruleForm.resetFields();
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // TODO: 调用添加接口
          console.log("提交的表单数据：", this.ruleForm);
          this.$message.success("添加成功");
          this.dialogVisible = false;
          this.$refs.ruleForm.resetFields();
          this.handleSearch(); // 刷新列表
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.supervision-container {
  padding: 8px 0 8px 8px;

  .search-area {
    background-color: #fff;
    padding: 16px 16px 0px 16px;
    margin-bottom: 20px;
    border-radius: 0.5rem;
    border: 1px solid #eee;
  }
  .tablebox {
    background-color: #fff;
    border-radius: 0.5rem;
    padding: 1rem;
    border: 1px solid #eee;
    .operation-area {
      margin-bottom: 20px;
    }
  }
  .el-table {
    .el-button {
      padding: 2px 0;
      margin: 0 5px;

      &.el-button--text {
        color: #409eff;

        &:hover {
          color: #66b1ff;
        }
      }
    }
  }
}

.el-dialog {
  .el-form {
    padding: 0 20px;

    .el-form-item {
      margin-bottom: 20px;

      .el-radio-group {
        .el-radio {
          margin-right: 20px;
          line-height: 32px;
        }
      }

      .el-date-picker {
        width: 100%;
      }

      .el-textarea {
        .el-textarea__inner {
          min-height: 80px;
        }
      }
    }
  }

  .dialog-footer {
    text-align: right;
    padding: 10px 20px 20px;

    .el-button {
      padding: 9px 20px;

      & + .el-button {
        margin-left: 10px;
      }
    }
  }
}
</style>
