import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {FormBuilder, FormGroup} from '@angular/forms';
import {HttpClient} from '@angular/common/http';
import {NzMessageService} from 'ng-zorro-antd';
import {environment} from '../../../../environments/environment';

@Component({
  selector: 'app-cms-content-view',
  templateUrl: './cms-content-view.component.html',
  styleUrls: ['./cms-content-view.component.less']
})
export class CmsContentViewComponent implements OnInit {
  config: any = {
    initialFrameHeight: 400,
  };
  // 详情id
  id;
  cmsContentView: any = {};
  // 定义回调路径
  FILE_URL;
  WEB_SERVE_URL;
  // 根据内容查询附件列表
  patchList = [];
  // 定义表单
  validateForm: FormGroup;
  constructor(
    private activatedRoute: ActivatedRoute,
    private fb: FormBuilder, // 表单
    private http: HttpClient, // http请求
    private message: NzMessageService, // 信息提示
  ) {
    this.FILE_URL = environment.imageUrl;
    this.WEB_SERVE_URL = environment.baseUrl;
    activatedRoute.queryParams.subscribe(queryParams => {
      this.id = queryParams.id;
      this.findContentView(queryParams.id);
      this.findPatchByContent(queryParams.id);
    });
  }

  ngOnInit() {
    // 判断表单
    this.validateForm = this.fb.group({
      recommend: [null],
      content: [null],
    });
  }

  // 查询内容详情
  findContentView(id) {
    this.http.get(this.WEB_SERVE_URL + '/cmsContent/getContentById?id=' + id).subscribe(data => {
      if (data['return_code'] === '000000') {
        this.cmsContentView = data['return_data'];
        console.log(this.cmsContentView);
      } else {
        this.message.create('error', data['return_msg']);
      }
    });
  }

  // 查询附件列表
  findPatchByContent(id) {
    this.http.get(this.WEB_SERVE_URL + '/cmsContent/getListPatches?pageNum=1&pageSize=100&contentId=' + id).subscribe( data => {
      if (data['return_code'] === '000000') {
        this.patchList = data['return_data'].list;
        console.log(this.patchList);
      } else {
        this.message.create('warning', data['return_msg']);
      }
    });
  }
  // 返回
  getBack() {
    history.back();
  }
}
