import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {LocalStorageService} from '../../../services/local-storage.service';
import {CommonsService} from '../../../services/commons.service';
import {Router} from '@angular/router';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {OilPriceTaskService} from '../../../services/oil-price/oil-price-task.service';

@Component({
  selector: 'app-oil-price-task-list',
  templateUrl: './oil-price-task-list.component.html',
  styleUrls: ['./oil-price-task-list.component.scss']
})
export class OilPriceTaskListComponent implements OnInit {
  FILE_URL = environment.imageUrl;
  roleType;
  adminFlag;
  loadingObject = {
    spinning: false,
    msg: '加载中'
  };

  dataObject: any = {};
  tableLoading = true;
  searchForm: FormGroup;
  pageNum: number;
  whereObject: any = {};

  oilNoArray = [];
  priceTypeArray = [];
  secUserObjectType: number;

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private oilPriceTaskService: OilPriceTaskService,
              private localStorageService: LocalStorageService , // 请求缓存
              private commonsService: CommonsService,
              private router: Router,
              private form: FormBuilder) {
    this.roleType = Number(this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType);
    this.adminFlag = Number(this.store.get(ADMIN_INFO_OBJECT)['secUser'].adminFlag);
    this.secUserObjectType = Number(this.localStorageService.get(ADMIN_INFO_OBJECT)['secUser']['objectType']);
  }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      regionName: [null],
      merStoreKey: [null],
      merStoreName: [null],
      oilNo: [null],
      oilType: [null],
      priceType: [null],
      executionType: [null],
      status: [null],
    });

    this.commonsService.getDictionary('GAS_OIL_TYPE', data => {
      this.oilNoArray = data['return_data'];
    });

    // 管理员 、分公司
    if (this.secUserObjectType === 0 || this.secUserObjectType === 1) {
      // 价格类型  1. 国标价   2. 油站价  3. 优惠幅度
      this.priceTypeArray.push({codeValue: 1, codeName: '国标价'});
      this.priceTypeArray.push({codeValue: 2, codeName: '油站价'});
      this.priceTypeArray.push({codeValue: 3, codeName: '平台优惠'});
    } else {
      this.priceTypeArray.push({codeValue: 2, codeName: '油站价'});
      this.priceTypeArray.push({codeValue: 4, codeName: '油站直降'});
    }
    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.oilPriceTaskService.getTaskList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.modal.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }

  /**
   * 搜索
   * @param whereObject 条件
   */
  search(whereObject: object) {
    this.whereObject = whereObject;
    this.requestData(1);
  }

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


  /**
   * 弹出删除对话框
   */
  showDelConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除任务？任务一旦被删除，不会执行。',
      nzOkText: '是',
      nzCancelText: '否',
      nzOkType: 'danger',
      nzOnOk: () => this.delData(id)
    });
  }

  /**
   * 禁用数据
   */
  delData(id: number) {
    this.oilPriceTaskService.delTask(id, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }
}
