<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入关键字搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-row class="help-container">
      <el-col :span="8" v-for="dict in dict.type.buyer_help_type" :key="dict.value">
        <div class="help-item" :class="{'first-child':parseInt(dict.value) % 3 == 0,'first-row':parseInt(dict.value) < 3}">
          <div class="type-title">
            {{ dict.label }}
          </div>
          <template v-for="item in helpList">
            <div v-if="item.type === dict.value" @click="handleUpdate(item)">
              <div class="data-title link pointer">
                {{ item.title }}
              </div>
            </div>
          </template>
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="form.title" :visible.sync="open" width="500px" append-to-body fullscreen>
      <div v-html="form.content"></div>
    </el-dialog>
  </div>
</template>

<script>
import { listHelp, getHelp, delHelp, addHelp, updateHelp } from '@/api/buyer/help'
import {KEY_CODES} from "@riophae/vue-treeselect/src/constants";

export default {
  name: 'Help',
  dicts: ['buyer_help_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帮助中心表格数据
      helpList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 99999,
        type: null,
        title: null,
        content: null,
        order: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    document.onkeydown = (e) => {
      if(e.keyCode == KEY_CODES.ENTER){
        e.preventDefault()
      }
    }
   this.getList()
  },
  methods: {
    /** 查询帮助中心列表 */
    getList() {
      this.loading = true
      listHelp(this.queryParams).then(response => {
        this.helpList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        title: null,
        content: null,
        order: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remarks: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加帮助中心'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getHelp(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改帮助中心'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHelp(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addHelp(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除帮助中心选中的数据项？').then(function() {
        return delHelp(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/help/export', {
        ...this.queryParams
      }, `help_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style scoped lang="scss">
.help-container {

  .help-item {
    height: 230px;
    border: 1px solid #e5e5e5;
    border-width: 0px 1px 1px 0;
    padding: 8px 15px;

    .type-title {
      color: #333;
      font-size: 14px;
      margin-bottom: 30px;
    }

    .data-title {
      font-size: 13px;
      margin-bottom: 15px;
    }
  }

  .first-child {
    border-left: 1px solid #e5e5e5;
  }

  .first-row {
    border-top: 1px solid #e5e5e5;
  }
}
</style>
