import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '@angular/forms';
import {MerchantService} from '../../../services/merchant.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {CommonsService} from '../../../services/commons.service';

@Component({
  selector: 'app-price-list',
  templateUrl: './price-list.component.html',
  styleUrls: ['./price-list.component.scss']
})
export class PriceListComponent implements OnInit {

  WEB_SERVE_URL = environment.imageUrl;
  searchForm: FormGroup; // 搜索框
  requestData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;

  constructor(
      private form: FormBuilder,
      private merchant: MerchantService,
      private iconService: IconService,
      private message: NzMessageService,
      private router: Router,
      private common: CommonsService
  ) {
  }

  ngOnInit(): void {
    this.init();
  }

  public init(): void {
    this.searchForm = this.form.group({
      merchantKey: [null],
      merchantName: [null],
      telephone: [null],
      status: [null],
    });
    this.getRequest(true, this.searchForm.value);
  }

  // 查询列表
  public getRequest(reset: boolean = false, whereObject: object) {

    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.merchant.getMerchantList(whereObject, data => {
      if (data['return_code'] === '000000') {
        this.requestData = data['return_data'].list;
        this.total = data['return_data'].total;
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

  // 重置
  public resetForm(): void {
    this.searchForm.reset();
  }

  // 禁用商户
  public disableMerchant(id): void {
    const message =  '是否禁用商户';
    this.common.showConfirm(message, data => {
      if (data) {
        this.merchant.disableMerchant(id, dataReturn => {
          if (dataReturn['return_code'] === '000000') {
            this.message.success('操作成功');
            this.getRequest(false , this.searchForm.value);
          } else {
            this.message.warning(dataReturn['return_msg']);
          }
        });
      }
    });
  }

  // 启动商户
  public enableMerchant(id): void {
    const message =  '是否启用商户';
    this.common.showConfirm(message, data => {
      if (data) {
        this.merchant.enableMerchant(id, dataReturn => {
          if (dataReturn['return_code'] === '000000') {
            this.message.success('操作成功');
            this.getRequest(false , this.searchForm.value);
          } else {
            this.message.warning(dataReturn['return_msg']);
          }
        });
      }
    });
  }


  // 修改
  public getEdit(id: number): void {
    this.router.navigate(['/admin/merchant/merchant-edit'], {
      queryParams: {
        merchantId: id
      }
    }).then(r => console.log(r));
  }

  // 查看详情
  public getDetail(id: number): void {
    this.router.navigate(['/admin/merchant/merchant-detail'], {
      queryParams: {
        merchantId: id
      }
    }).then(r => console.log(r));
  }

  // 查看门店列表
  public getList(id: number , merchantName: string): void {
    this.router.navigate(['/admin/merchantStore/store-list'], {
      queryParams: {
        merchantId: id,
        name: merchantName,
      }
    }).then(r => console.log(r));
  }
}
