<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.title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</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="iconUrl" label="图标" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 30px; height: 30px"
              :src="scope.row.iconUrl"
              :preview-src-list="[scope.row.iconUrl]"
            >
            </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="300" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" round @click="view(scope.row)"
              >查看</el-button
            >

            <el-button
              type="primary"
              size="mini"
              class="btn"
              round
              @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button
              @click="delFlagHandle(scope.row)"
              :type="scope.row.delFlag == 0 ? 'warning' : 'success'"
              size="mini"
              round
              slot="reference"
              >{{ scope.row.delFlag == 0 ? "禁用" : "启用" }}
            </el-button>
          </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="title">
          <el-input v-model="dataForm.title" clearable></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input
            v-model="dataForm.description"
            clearable
            type="textarea"
          ></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-item label="图标" prop="iconUrl">
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            action="#"
            :http-request="upload"
          >
            <img
              v-if="dataForm.iconUrl"
              :src="dataForm.iconUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </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 {
  instructList,
  instructAdd,
  instructEdit,
  instructEnable,
} from "@/api/instructions";
import TableTemplate from "@/components/Table";
import { iconUpload } from "@/api/livingCircle";
export default {
  name: "instructions",
  components: { TableTemplate },
  data() {
    return {
      type: "add",
      searchForm: {
        title: "",
      },
      tableData: [],
      pagination: {
        total: 0,
        pageSize: 10,
        currentPage: 1,
      },
      tableColumns: [
        {
          key: "title",
          label: "标题",
        },
        {
          key: "description",
          label: "简介",
        },
        {
          key: "iconUrl",
          label: "图标",
          type: "slot",
        },
        {
          key: "sort",
          label: "排序",
        },
        {
          key: "delFlag",
          label: "启用状态",
          type: "slot",
        },
        {
          key: "createTime",
          label: "创建时间",
        },
      ],
      loading: false,
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        description: [
          { required: true, message: "请输入简介", trigger: "blur" },
        ],
        iconUrl: [
          { required: true, message: "请输入类型图标", trigger: "blur" },
        ],
      },
      dataForm: {
        title: "",
        sort: 1,
        iconUrl: "",
        description: "",
        id: "",
      },
      dialogVisible: false,
    };
  },
  mounted() {
    this.init();
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.dataForm = {
          title: "",
          sort: "",
          iconUrl: "",
          description: "",
          id: "",
        };
      }
    },
  },

  methods: {
    init() {
      this.loading = true;
      instructList({
        ...this.searchForm,
        pageNum: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
      })
        .then((res) => {
          if (res.code == 200) {
            this.tableData = res.data.list;
            this.pagination.total = res.data.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    jumpPage() {
      this.init();
    },
    onSearch() {
      this.pagination.currentPage = 1;
      this.init();
    },
    addHandle() {
      this.type = "add";
      this.dialogVisible = true;
    },
    upload(file) {
      iconUpload(file.file).then((res) => {
        if (res.code == 200) {
          this.dataForm.iconUrl = res.data;
          this.$message.success("上传成功");
        } else {
          this.$message.warning(res.msg || "上传失败");
        }
      });
    },
    confirmHandle() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.type == "add") {
            delete this.dataForm.id;
            instructAdd(this.dataForm).then((res) => {
              if (res.code == 200) {
                this.$message.success("新增成功");
                this.init();

                this.dialogVisible = false;
              } else {
                this.$message.warning(res.msg || "新增失败");
              }
            });
          } else {
            // 编辑
            instructEdit(this.dataForm).then((res) => {
              if (res.code == 200) {
                this.$message.success("编辑成功");
                this.init();
                this.dialogVisible = false;
              } else {
                this.$message.warning(res.msg || "编辑失败");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    delFlagHandle(row) {
      instructEnable(row.id).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.init();
        } else {
          this.$message.warning(res.msg || "操作失败");
        }
      });
    },
    edit(row) {
      this.type = "edit";
      this.dialogVisible = true;
      this.dataForm.sort = row.sort;
      this.dataForm.title = row.title;
      this.dataForm.iconUrl = row.iconUrl;
      this.dataForm.description = row.description;
      this.dataForm.id = row.id;
    },
    view(row) {
      this.$router.push("/instructions/detail?id=" + row.id);
    },
  },
};
</script>

<style scoped></style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>
