<template>
  <div class="doc_manage_container">
    <apass-list
      ref="list"
      search-placeholder="请输入关键字"
      :hide-search="true"
      :list-total="listTotal"
      :list-header="listHeader"
      :list-data="listData"
      :list-padding-left="paddingLeft"
      @list-action="init"
    >
      <el-breadcrumb separator="/" slot="breadcrumb">
        <el-breadcrumb-item to="/technical_support">
          技术支持
        </el-breadcrumb-item>
        <el-breadcrumb-item to="/technical_support/doc_manage">
          开发文档管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          管理列表
        </el-breadcrumb-item>
      </el-breadcrumb>

      <template slot="header-left">
        <el-button type="primary" @click="addNew">
          新建
        </el-button>
      </template>

      <template slot="top">
        <div class="top_fliter">
          <show-more-filter class="filter_list">
            <div class="filter_item">
              <span class="filter_title">文档名称：</span>
              <el-input
                v-model="topFilter.name"
                placeholder="请输入文档名称"
                @keyup.enter.native="topFilterAction"
              ></el-input>
            </div>
            <div class="filter_item">
              <span class="filter_title">文档类型：</span>
              <el-select v-model="topFilter.type" placeholder="请选择">
                <el-option label="全部" value=""></el-option>
                <el-option
                  v-for="(item, index) in types"
                  :key="'top_type_' + index"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">最后更新时间：</span>
              <el-date-picker
                v-model="topFilter.time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </div>
          </show-more-filter>
          <div class="filter_action apaas_button">
            <el-button type="primary" @click="topFilterAction">
              查询
            </el-button>
            <el-button type="defalut" @click="topFilterClear">
              重置
            </el-button>
          </div>
        </div>
      </template>
    </apass-list>

    <apass-dialog ref="addDialog" title="新增">
      <div class="add_content" slot="content">
        <div class="add_notice">
          <p>注：同一文档类型下可维护多个文档名称</p>
        </div>
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="文档类型" prop="style">
            <el-select
              v-model="addForm.style"
              placeholder="请选择文档类型"
              style="width: 100%;"
            >
              <el-option
                v-for="(item, index) in types"
                :key="'add_type_' + index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文档名称" prop="title">
            <el-input
              v-model="addForm.title"
              placeholder="请输入文档名称"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

      <template slot="action">
        <el-button type="defalut" size="mini" @click="AddCancelAction">
          取消
        </el-button>
        <el-button type="primary" size="mini" @click="AddSubmitAction">
          确定
        </el-button>
      </template>
    </apass-dialog>

    <apass-dialog
      ref="detailDialog"
      :title="detail.title"
      class="doc_manage_detail_dialog"
      width="1340px"
    >
      <div
        class="detail_content w-e-text w-e-text-container apaas_scroll"
        slot="content"
        v-html="detail.content"
      ></div>
    </apass-dialog>

    <apass-dialog
      ref="deleteDialog"
      :msg="deleteDialogInfo.msg"
      :submit="deleteDialogInfo.submit"
    ></apass-dialog>
  </div>
</template>

<script>
import helper from "@/services/helper.js";
import apassList from "@/components/apass-list";
import apassDialog from "@/components/apass-dialog";
import showMoreFilter from "@/components/show-more-filter";

export default {
  components: {
    apassList,
    apassDialog,
    showMoreFilter,
  },
  data() {
    return {
      paddingLeft: 25,
      listTotal: 0,
      listHeader: [
        {
          label: "文档名称",
          prop: "title",
          type: "button",
          callback: this.viewItem,
          width: 320,
        },
        {
          label: "文档类型",
          prop: "style",
          getText: (item) => {
            let type = this.types[item.style] || null;

            return (type && type.label) || "";
          },
          align: "center",
        },
        {
          label: "操作人",
          prop: "created_by",
          align: "center",
        },
        {
          label: "最后更新时间",
          prop: "updated",
          getText: ({ updated }) => this.getTimeText(updated || ""),
          align: "center",
        },
        {
          label: "操作",
          type: "buttons",
          align: "center",
          width: 240,
          actionList: [
            {
              label: "编辑",
              callback: this.editItem,
            },
            {
              label: "删除",
              callback: this.deleteItem,
              class: "warn",
            },
          ],
        },
      ],
      listData: [],
      topFilter: {
        name: "",
        type: "",
        time: "",
      },
      types: [
        {
          label: "平台简介",
          value: 0, // "introductory",
        },
        {
          label: "使用须知",
          value: 1, // "notice",
        },
        {
          label: "术语说明",
          value: 2, // "trem",
        },
        {
          label: "标准规范",
          value: 3, // "standard",
        },
        {
          label: "操作指南",
          value: 4, // "guide",
        },
        {
          label: "接口文档",
          value: 5, // "api",
        },
        {
          label: "常见问题",
          value: 6, // "questions",
        },
      ],
      addForm: {
        style: "",
        title: "",
      },
      addFormRules: {
        style: [
          { required: true, message: "请选择文档类型", trigger: "change" },
        ],
        title: [
          { required: true, message: "请输入文档名称", trigger: "blur" },
          { max: 20, message: "长度应小于20个字符", trigger: "change" },
        ],
      },
      detail: {
        title: "",
        content: "",
      },
      deleteDialogInfo: {
        msg: "",
        submit: null,
      },
      tempFilter: {},
    };
  },
  methods: {
    init(filter) {
      let fullFilter = {
        ...filter,
        ...this.topFilter,
      };
      this.tempFilter = filter;

      this.$http
        .get("/apaas/support/document/list", {
          params: {
            search: fullFilter.name,
            style: fullFilter.type,
            start: fullFilter.time && fullFilter.time[0],
            end: fullFilter.time && fullFilter.time[1],
            limit: fullFilter.size,
            page: fullFilter.page,
          },
        })
        .then(({ data }) => {
          if (data.success === 1) {
            this.listTotal = data.total;
            this.listData = data.data || [];
          }
        });
    },
    getTimeText(time) {
      return helper.dateStringTransform(time);
    },
    topFilterAction() {
      this.refreshPage();
    },
    topFilterClear() {
      this.topFilter = {
        name: "",
        type: "",
        time: "",
      };
      this.refreshPage();
    },
    refreshPage() {
      this.$refs.list.resetCurrentPage();
    },
    viewItem(item) {
      this.$http
        .get("/apaas/support/document/get", {
          params: {
            id: item.id,
          },
        })
        .then(({ data }) => {
          if (data.success === 1) {
            this.detail = data.data;
          }
        });

      this.$refs.detailDialog.show();
    },
    editItem(item) {
      this.$router.push(`/technical_support/doc_manage/edit/${item.id}`);
    },
    deleteItem(item) {
      this.deleteDialogInfo.msg = `是否删除《${item.title}》？`;
      this.deleteDialogInfo.submit = () => {
        this.$http
          .delete("/apaas/support/document/delete", {
            params: {
              id: item.id,
            },
          })
          .then(({ data }) => {
            if (data.success === 1) {
              this.$message.success("删除成功");
              this.init(this.tempFilter);
            } else {
              this.$message.error("删除失败");
            }
          })
          .catch((error) => {
            console.log(error);
            this.$message.error("删除失败");
          });
      };
      this.$refs.deleteDialog.show();
    },
    addNew() {
      this.addForm = {
        style: 0,
        title: "",
      };
      this.$refs.addForm && this.$refs.addForm.clearValidate();
      this.$refs.addDialog.show();
    },
    AddCancelAction() {
      this.$refs.addDialog.hide();
    },
    AddSubmitAction() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$http
            .get("/apaas/support/document/checkTitle", {
              params: this.addForm,
            })
            .then(({ data }) => {
              if (data.success === 1) {
                this.$http
                  .post("/apaas/support/document/create", {
                    ...this.addForm,
                    content: "",
                  })
                  .then(({ data }) => {
                    if (data.success === 1) {
                      this.$message.success("新增成功");
                      this.editItem(data.data);
                    } else {
                      this.$message.error(data.errMsg || "新增失败");
                    }
                  })
                  .catch((error) => {
                    console.log(error);
                    this.$message.error("新增失败");
                  });
              } else {
                this.$message.error("验证失败");
              }
            })
            .catch((error) => {
              console.log(error);
              this.$message.error("验证失败");
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.doc_manage_container {
  height: 100%;
}
.add_notice {
  display: inline-block;
  margin: 10px auto;
  background-color: #f4f7fc;
  border-radius: 8px;
  padding: 10px;
}
.add_notice > p {
  font-size: 12px;
  line-height: 16px;
  color: #58617a;
  padding-left: 20px;
  background-image: url("../../../assets/imgs/ic_tishi.png");
  background-repeat: no-repeat;
  background-position: left center;
}
.detail_content {
  height: 650px;
  overflow: auto;
  text-align: left;
}
</style>

<style>
.doc_manage_detail_dialog .el-dialog__body {
  padding: 15px;
}
.doc_manage_detail_dialog .el-dialog__footer {
  display: none;
}
</style>
