<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="案例标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入关键字"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属站点" prop="tenant_id" v-if="is_super">
        <SiteOptions v-model="queryParams" />
      </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="dataList">
      <el-table-column label="数据编码" prop="id" align="center" />
      <el-table-column label="案例标题" prop="title" align="center" />
      <el-table-column label="图片" prop="image" align="center" width="120">
        <template slot-scope="scope">
          <div>
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.image"
              fit="cover"
            ></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="所属站点" prop="area" align="center" />
      <el-table-column
        label="描述"
        prop="description"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column label="创建时间" prop="created_at" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.created_at) }}</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)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page_num"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="案例标题:" prop="title">
          <el-input v-model="form.title" placeholder="请输入案例标题" />
        </el-form-item>
        <el-form-item label="描述:" prop="description">
          <el-input
            v-model="form.description"
            placeholder="请输入描述"
            maxlength="30"
          />
        </el-form-item>
        <el-form-item label="图片:" prop="image">
          <ImageUpload v-model="form.image" :isShowTip="false" :limit="1" />
        </el-form-item>
        <el-form-item label="所属站点:" prop="tenant_id" v-if="is_super">
          <SiteOptions v-model="form" />
        </el-form-item>
        <el-form-item label="所属服务:" prop="kind_id">
          <el-cascader
            v-model="form.kind_id"
            clearable
            separator="-"
            placeholder="请选择，支持搜索"
            :options="caseOptions"
            filterable
            :props="{ emitPath: false, value: 'id', label: 'title' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="案例描述:" prop="content">
          <Editor v-model="form.content" :minHeight="150" />
        </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 {
  serviceCaseList,
  serviceCaseAdd,
  serviceCaseDetail,
  serviceCaseEdit,
  serviceCaseDelete,
  serviceCaseKindSelect,
} from "@/api/website/solution";
export default {
  data() {
    return {
      is_super: this.$store.getters.is_super,
      loading: true,
      showSearch: true,
      caseOptions: [],
      queryParams: {
        title: undefined,
        tenant_id: undefined,
        page_num: 1,
        page_size: 10,
      },
      total: 0,
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      rules: {
        title: [
          { required: true, message: "案例标题不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" },
        ],
        image: [
          {
            required: true,
            message: "图片不能为空",
            trigger: ["blur", "change"],
          },
        ],
        tenant_id: [
          {
            required: true,
            message: "所属站点不能为空",
            trigger: ["blur", "change"],
          },
        ],
        kind_id: [
          {
            required: true,
            message: "所属服务不能为空",
            trigger: ["blur", "change"],
          },
        ],
        content: [
          {
            required: true,
            message: "案例描述不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.page_num = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      serviceCaseList(this.queryParams).then((res) => {
        this.dataList = res.data.data;
        this.total = res.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.memberOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tenant_id: undefined,
        kind_id: undefined,
        title: undefined,
        image: undefined,
        description: undefined,
        content: "",
      };
      this.resetForm("form");
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    handleUpdate(row) {
      this.reset();
      serviceCaseDetail({ id: row.id }).then((res) => {
        this.form = res.data;
        this.open = true;
        this.title = "修改";
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            serviceCaseEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            serviceCaseAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名为"' + row.title + '"的数据项？')
        .then(function () {
          return serviceCaseDelete({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
  created() {
    this.getList();
    serviceCaseKindSelect().then((res) => {
      for (const key in res.data) {
        if (Object.hasOwnProperty.call(res.data, key)) {
          const item = res.data[key];
          this.caseOptions.push(item);
        }
      }
    });
  },
};
</script>