<template>
  <div>
    <div class="search-wrapper">
      <el-form
        :inline="true"
        :model="searchForm"
        class="search_form"
        style="text-align: left"
      >
        <el-form-item label="推荐搜索词">
          <el-input
            clearable
            v-model="searchForm.keyword"
            placeholder="请输入推荐搜索词"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="addHandle">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-wrapper">
      <table-template
        :tableData="tableData"
        :tableColumns="tableColumns"
        :loading="loading"
        :pagination="pagination"
        @Jump="jumpPage"
      >
        <el-table-column slot="icon" label="服务图标" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 30px; height: 30px"
              :src="scope.row.icon"
              :preview-src-list="[scope.row.icon]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column slot="delFlag" label="启用状态" align="center">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.delFlag == 0 || scope.row.delFlag == 1"
              :type="
                scope.row.delFlag == 1
                  ? 'danger'
                  : scope.row.delFlag == 0
                  ? 'success'
                  : ''
              "
              >{{ scope.row.delFlag == 1 ? "禁用" : "启用" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="350" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              round
              :disabled="scope.row.delFlag == 0"
              @click="view(scope.row)"
              >编辑</el-button
            >
            <el-button
              @click="enableHandle(scope.row)"
              :type="scope.row.delFlag == 0 ? 'warning' : 'success'"
              size="mini"
              round
              slot="reference"
              >{{ scope.row.delFlag == 0 ? "禁用" : "启用" }}
            </el-button>
            <el-popconfirm
              title="确定删除此数据吗？"
              @confirm="delHandle(scope.row)"
            >
              <el-button
                type="danger"
                size="mini"
                round
                slot="reference"
                style="margin-left: 10px"
                >删除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </table-template>
    </div>

    <el-dialog
      :title="type == 'add' ? '新增' : '编辑'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :model="dataForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="推荐搜索词" prop="keyword">
          <el-input v-model="dataForm.keyword" clearable></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="dataForm.sort" :min="1"></el-input-number>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableTemplate from "@/components/Table";
import {
  getKeyword,
  enableKeyword,
  editKeyword,
  addKeyword,
  delKeyword,
} from "@/api/livingCircle";
export default {
  components: { TableTemplate },
  data() {
    return {
      type: "add", // 'add' or 'edit
      dialogVisible: false,
      searchForm: {
        keyword: "",
      },
      TypeList: [],
      loading: false,
      tableData: [],
      tableColumns: [
        {
          key: "keyword",
          label: "推荐搜索词",
        },
        {
          key: "createTime",
          label: "创建时间",
        },
        {
          key: "delFlag",
          label: "启用状态",
          type: "slot",
        },
        {
          key: "sort",
          label: "排序",
        },
      ],
      pagination: {
        total: 0,
        pageSize: 10,
        currentPage: 1,
      },
      rules: {
        keyword: [
          { required: true, message: "请输入推荐搜索词", trigger: "blur" },
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
      },
      dataForm: {
        keyword: "",
        sort: "",
      },
    };
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.dataForm = {
          keyword: "",
          sort: "",
        };
      }
    },
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      let params = {
        keyword: this.searchForm.keyword,
        pageSize: this.pagination.pageSize,
        pageNum: this.pagination.currentPage,
      };
      getKeyword(params).then((res) => {
        if (res.code == 200) {
          this.tableData =
            res.data.list && res.data.list.length > 0 ? res.data.list : [];
          this.pagination.total = res.data?.total ? res.data.total : 0;
        } else {
          this.$message.warning(res.msg || "查询列表失败");
        }
      });
    },
    onSubmit() {
      this.init();
    },
    jumpPage() {
      this.init();
    },
    view(row) {
      this.type = "edit";
      this.dataForm.sort = row.sort;
      this.dataForm.keyword = row.keyword;
      this.dataForm.id = row.id;
      this.dialogVisible = true;
    },
    enableHandle(row) {
      enableKeyword({
        id: row.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.init();
        } else {
          this.$message.warning(res.msg || "操作失败");
        }
      });
    },
    addHandle() {
      this.type = "add";
      this.dialogVisible = true;
    },
    delHandle(row) {
      delKeyword({
        id: row.id,
        pageNum: 1,
        pageSize: 10,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.init();
        } else {
          this.$message.warning(res.msg || "删除失败");
        }
      });
    },
    confirmHandle() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            editKeyword(this.dataForm).then((res) => {
              if (res.code == 200) {
                this.$message.success("编辑成功");
                this.dialogVisible = false;
                this.init();
              } else {
                this.$message.warning(res.msg || "编辑失败");
              }
            });
          } else {
            addKeyword(this.dataForm).then((res) => {
              if (res.code == 200) {
                this.$message.success("新增成功");
                this.dialogVisible = false;
                this.init();
              } else {
                this.$message.warning(res.msg || "新增失败");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="stylus" scoped>
@import '~@/stylus/common';
.btn{
    margin-right 10px
}
</style>
