<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item width="100" prop="title" label="项目标题">
            <el-input
              v-model="queryForm.title"
              size="small"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="date" :label="$t('defalult.rq')">
            <el-date-picker
              v-model="queryForm.date"
              size="small"
              type="date"
              :placeholder="$t('defalult.xzrq')"
            ></el-date-picker>
          </el-form-item>
          <el-form-item width="100" prop="category" label="分类">
            <el-select
              v-model="queryForm.category"
              size="small"
              filterable
              placeholder="请选择分类"
            >
              <el-option
                v-for="item in dictData"
                :key="item.dataCode"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-button size="small" @click="resetForm()">
        {{ $t('defalult.chongz') }}
      </el-button>
      <el-button type="primary" size="small" @click="search">
        {{ $t('defalult.cx') }}
      </el-button>
      <el-button type="primary" size="small" @click="handleAdd()">
        添加知识库
      </el-button>
      <el-button type="primary" size="small" @click="deletes">
        {{ $t('defalult.plsc') }}
      </el-button>
    </div>
    <el-table
      :data="tableData"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        fixed
        type="selection"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="id"
        label="Id"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="addUser"
        label="添加人"
        width="250"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        width="250"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="introduction"
        label="简介"
        width="150"
        align="center"
      >
        <template #default="{ row }">
          <p v-html="row.introduction"></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        :label="$t('grouping.cjsj')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleshow(row)">查看</el-button>
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <knowledge-edit ref="edit"></knowledge-edit>
    <detail ref="detail"></detail>
  </div>
</template>

<script>
import knowledgeEdit from './components/knowledgeEdit';
import detail from './components/detail';
import { getList, doDelete, getCategoryDict } from '@/api/knowledge';
export default {
  components: {
    knowledgeEdit,
    detail,
  },
  data() {
    return {
      tableData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      ids: [],
      dictData: [],
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        title: '',
        date: '',
        category: '',
      },
    };
  },
  created() {
    this.fetchData();
    this.getCategoryDict();
  },
  methods: {
    //搜索
    search() {
      this.fetchData();
    },
    async getCategoryDict() {
      const { data } = await getCategoryDict();
      this.dictData = data;
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    handleAdd() {
      this.$refs['edit'].showEdit();
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row);
    },
    handleshow(row) {
      this.$refs['detail'].showData(row);
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    deletes() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    //删除
    deleteData(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      const that = this;
      that.deletes([row.id]);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  padding-left: 12px;
  margin-bottom: 30px;
}
</style>

<style lang="scss" scoped>
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
      .el-cascader {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
  .dialog-footer {
    text-align: right;
  }
}
</style>
