<template>
  <div class="app-container pageTopForm">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" size="small" label-width="80px" class="demo-ruleForm">
      <el-col :span="8">
        <el-form-item label="标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入标题"
              clearable
              @keyup.enter.native="handleQuery"
            />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item class="lastBtnSty">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-col>
    </el-form>
    <TableList :columns="columns" :config="config" :modelIdent="this.$options.name" :queryParams="queryParams" :tableList="roleList" @fnClick="callback" @fnRenderList="getList" @value-change="handleValueChange">
      <template #tempTitle="{ row, column }">
        <div @click="fnDetails(row)">
          <a href="javascript:;" style="color: #51afff;">{{row.title}}</a>
        </div>
      </template>
      <template #tempIssueType="{ row, column }">
        <dict-tag :options="dict.type.knowledge_base_type" :value="row.issueType"/>
      </template>
    </TableList>
  </div>
</template>

<script setup>
  import TableList from "@/components/table/index.vue"
  import {listProblem, delProblem} from "@/api/sync/tacitly"
  export default {
    name: 'ProblemRecordIndex',
    components: {TableList},
    dicts: ['knowledge_base_type'],
    data() {
      return {
        loading: true,
        showSearch: true,
        roleList: [],
        queryParams: {
          total: 0,
          pageNum: 1,
          pageSize: 10
        },
        // 列显隐信息
        columns: {
          id: { label: `ID`, visible: false },
          title: { label: `标题`, visible: true, slotName: 'tempTitle'},
          issueType: { label: `标签`, slotName: 'tempIssueType',visible: true},
          solution: { label: `解决方案`, visible: true},
          createTime: { label: `创建时间`,visible: false},
          updateTime:{ label: `修改时间`,visible: false}
        },
        config: {
          searcherForm: [
            {label: '标题', prop: 'title', type: 'selset', options: []}
          ],
          tableButton: {
            top: [
              {content: '新增', fnCode: 'insert', type: 'primary', icon: 'el-icon-plus', hasPermi: 'problemRecord:add'},
              {content: '删除', fnCode: 'delete', type: 'danger', icon: 'el-icon-delete', hasPermi: 'problemRecord:detele'},
              {content: '导出', fnCode: 'export', type: 'primary', icon: 'el-icon-download', hasPermi: 'problemRecord:export'},
            ],
            line: [
              {content: '修改', fnCode: 'edit', type: 'text', icon: 'el-icon-edit', hasPermi: 'problemRecord:edit'},
              {content: '查看', fnCode: 'view', type: 'text', icon: 'el-icon-view', hasPermi: 'problemRecord:view'},
            ]
          }
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        listProblem(this.queryParams).then(response => {
          this.roleList = response.rows;
          this.queryParams.total = response.total;
          this.loading = false;
        })
      },
      // 处理子组件传递的新值
      handleValueChange(newValue) {
        // 父组件更新自身数据，实现同步
        this.showSearch = newValue;
        // console.log('父组件拿到新值:', newValue);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryRef");
        this.handleQuery();
      },

      // 详情
      fnDetails(row) {
        this.$router.push({
          path:'/sync/problemRecord/details/index',
          query:{
            id: row.id,
            readonly: true
          }
        });
      },

      callback(result, rowData, selectChange) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'insert':
              this.$router.push({
                path:'/sync/problemRecord/details/index'});
              break;
            case 'edit':
              this.$router.push({
                path:'/sync/problemRecord/details/index',
                query:{
                  id: rowData.id
                }
              });
              break;
              case 'view':
                this.fnDetails(rowData);
                break;
            case 'delete':
              this.$modal.confirm('是否确认删除数据项？').then(function() {
                return delProblem(selectChange)
              }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功")
              }).catch(() => {});
              break;
            case 'export':
              let dataList = [];
              Object.keys(this.columns).forEach(item => {
                if (item.visible) {
                  dataList.push(item.prop);
                }
              });
              this.download("/system/knowledgebase/export", {
                properties: dataList,
              }, `问题记录_${new Date().getTime()}.xlsx`);
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style scoped>
  ::v-deep .lastBtnSty .el-form-item__content{
    margin-left: 10px!important;
  }
</style>
