import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '_@angular_forms@9.0.7@@angular/forms';
import {NzModalService} from '_ng-zorro-antd@9.3.0@ng-zorro-antd';
import {ActivatedRoute, Router} from '_@angular_router@9.0.7@@angular/router';
import {CommonsService} from '../../../services/commons.service';
import {OilPriceTaskService} from '../../../services/oil-price/oil-price-task.service';
import {LocalStorageService} from "../../../services/local-storage.service";
import {ADMIN_INFO_OBJECT} from "../../../services/local-storage.namespace";

@Component({
  selector: 'app-oil-price-task-edit',
  templateUrl: './oil-price-task-edit.component.html',
  styleUrls: ['./oil-price-task-edit.component.scss']
})
export class OilPriceTaskEditComponent implements OnInit {
  dataFrom: FormGroup;
  btnLoading = false;

  oilNoArray = [];
  priceTypeArray = [];
  regionArray = [];
  gasArray = [];

  secUserObjectType: number;

  constructor(private formBuilder: FormBuilder,
              private modal: NzModalService,
              private oilPriceTaskService: OilPriceTaskService,
              private activatedRoute: ActivatedRoute,
              private commonsService: CommonsService,
              private localStorageService: LocalStorageService , // 请求缓存
              private router: Router) {
      this.secUserObjectType = Number(this.localStorageService.get(ADMIN_INFO_OBJECT)['secUser']['objectType']);
  }

  ngOnInit(): void {

    this.dataFrom = this.formBuilder.group({
      oilNo: ['92', [Validators.required]],
      priceType: [null, [Validators.required]],
      price: [0, [Validators.required]],
      executionType: ['1', [Validators.required]],
      startTime: [null],
      regionId: [null],
      merStoreIdArray: [[]],
    });

    // 管理员
    if (this.secUserObjectType === 0) {
      // 价格类型  1. 国标价   2. 油站价  3. 优惠幅度
      this.priceTypeArray.push({ codeValue: 1, codeName: '国标价' });
      this.priceTypeArray.push({ codeValue: 2, codeName: '油站价' });
      this.priceTypeArray.push({ codeValue: 3, codeName: '优惠幅度'});

      // 获取全部省级地区
      this.commonsService.getProvinceList(data => {
        this.regionArray = data['return_data'];
      });

      this.commonsService.getGasSelectList( data => {
        this.gasArray = data['return_data'];
      });

      // 分公司
    } else if (this.secUserObjectType === 1) {
      // 价格类型  1. 国标价   2. 油站价  3. 优惠幅度
      this.priceTypeArray.push({ codeValue: 1, codeName: '国标价' });
      this.priceTypeArray.push({ codeValue: 2, codeName: '油站价' });
      this.priceTypeArray.push({ codeValue: 3, codeName: '优惠幅度'});

      this.regionArray.push({ regionId: this.localStorageService.get(ADMIN_INFO_OBJECT)['bsCompany']['regionId'],
                              regionName: this.localStorageService.get(ADMIN_INFO_OBJECT)['bsCompany']['regionName'] });

      this.commonsService.getGasSelectList( data => {
        this.gasArray = data['return_data'];
      });

      // 商户
    } else if (this.secUserObjectType === 2) {
      this.priceTypeArray.push({ codeValue: 2, codeName: '油站价' });
      this.priceTypeArray.push({ codeValue: 3, codeName: '优惠幅度' });

      this.commonsService.getGasSelectList( data => {
        this.gasArray = data['return_data'];
      });

      // 加油站
    } else if (this.secUserObjectType === 3) {
      this.priceTypeArray.push({ codeValue: 2, codeName: '油站价' });
      this.priceTypeArray.push({ codeValue: 3, codeName: '优惠幅度' });

      this.dataFrom.patchValue({ merStoreIdArray: [ this.localStorageService.get(ADMIN_INFO_OBJECT)['merchantStore']['id'] ] });
    }

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

  }

  /**
   * 提交
   */
  submitFrom() {
    for (const i in this.dataFrom.controls) {
      this.dataFrom.controls[i].markAsDirty();
      this.dataFrom.controls[i].updateValueAndValidity();
    }
    if (this.dataFrom.status == null || this.dataFrom.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请规范填写所有的必填项信息',
      });
      return;
    }

    const dataFromValue = this.dataFrom.value;
    const dataArray = [];

    if (dataFromValue.priceType === '1') {
        dataArray.push({
          regionId: dataFromValue.regionId,
          oilNo: dataFromValue.oilNo,
          priceType: dataFromValue.priceType,
          price: dataFromValue.price,
          executionType: dataFromValue.executionType,
          startTime: dataFromValue.startTime,
        });
    } else {
      for (const storeId of this.dataFrom.value.merStoreIdArray) {
        dataArray.push({
          merStoreId: storeId,
          oilNo: dataFromValue.oilNo,
          priceType: dataFromValue.priceType,
          price: dataFromValue.price,
          executionType: dataFromValue.executionType,
          startTime: dataFromValue.startTime,
        });
      }
    }

    this.btnLoading = true;
    this.oilPriceTaskService.batchAddTask(dataArray, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '添加成功',
          nzOnOk: () => this.router.navigateByUrl('admin/gas-oil-price/task-list')
        });
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
      this.btnLoading = false;
    });
  }

}
