import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn } from '@delon/abc/st';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
// import { PublishGoodsChooseFamifiarComponent } from 'src/app/routes/publish-goods/components/choose-famifiar-bulk/choose-famifiar.component';
import { SupplyManagementService } from '../../services/supply-management.service';
// import { SupplyManagementCurrentAssignmentComponent } from '../current-assignment/current-assignment.component';
import { SupplyManagementUpdatePriceComponent } from '../update-price/update-price.component';

@Component({
  selector: 'app-supply-management-bulk-detail',
  templateUrl: './bulk-detail.component.html',
  styleUrls: ['./bulk-detail.component.less']
})
export class SupplyManagementBulkDetailComponent implements OnInit {

  id = this.route.snapshot.params.id;
  i: any;
  logColumns: STColumn[] = [
    { title: '内容', index: 'operationContent' },
    { title: '操作人', index: 'operator' },
    { title: '操作时间', index: 'operatorTimestamp' },
  ];
  driverColums: STColumn[] = [
    { title: '司机姓名', index: 'theme' },
    { title: '手机号', index: 'operationUserPhone' },
    { title: '车牌号', index: ' createTime' },
    { title: '承运次数', index: 'operationUserPhone' },
    { title: '承运数量', index: ' createTime' },
    {
      title: '操作',
      fixed: 'right',
      width: '200px',
      className: 'text-center',
      buttons: [
        {
          text: '移除',
          click: (_record) => this.delDiver(_record),
        },
      ],
    },
  ];
  status: any = { 1: '待接单', 2: '已接单', 3: '已取消' };
  serviceType: any = {
    1: '抢单',
    2: '指派'
  }
  freightType: any = {
    1: '元/吨',
    2: '元/方',
    3: '元/车'
  } // 运单类型

  settlementBasis: any = {
    1: '以收货为准',
    2: '以发货为准'
  } // 结算依据

  rule: any = {
    1: '保留小数',
    2: '抹除小数',
    3: '抹除个数'
  } // 取整规则
  get reqParams() {
    return { 
      operateObject: this.i?.resourceCode,
      operateTypeList:  [4,7],
     };
  }
  currentStatus = 0;

  constructor(
    private route: ActivatedRoute,
    private msgSrv: NzMessageService,
    public service: SupplyManagementService,
    private modal: NzModalService,
    private router: Router
  ) {

  }

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

  /**
   * 移除司机
   */
  delDiver(item: any) {
    const { id } = item;
    // this.modal.confirm({
    //   nzTitle: '<i>删除确认</i>',
    //   nzContent: `请仔细核对,避免误操作！<br>是否删除？</br>`,
    //   nzOnOk: () => {
    //     this.service.http.post(this.service.$api_del_driver, { id }).subscribe((res) => {
    //       if (res) {
    //         this.service.msgSrv.success('数据删除成功！');
    //         // this.st1.reload();
    //       } else {
    //         this.service.msgSrv.error('删除失败！');
    //       }
    //     });
    //   },
    // });
  }

  /**
   * 查看当前指派
   */
  viewCurrentAssign(item: any) {
    // const modalRef = this.modal.create({
    //   nzTitle: '当前指派',
    //   nzWidth: '600px',
    //   nzContent: SupplyManagementCurrentAssignmentComponent,
    //   nzComponentParams: {
    //     i: item,
    //   },
    //   nzFooter: null,
    // });
  }
  /**
     *再下一单
     * @param record
     */
  placeOrder(record: any) {
    console.log(record)
    this.router.navigate(['/supply-management/bulk-amend', record.id], {
      queryParams: {
        sta: 4
      },
    })
  }
  /**
   * 修改货源
   */
  updateGoodsSource(record: any) {
    this.router.navigate(['./pbg/onecar-publish'], {
      queryParams: {
        id: record?.id
      }
    })
  }

  /**
 * 更新单价
 */
  updatePrice(item: any) {
    const modalRef = this.modal.create({
      nzTitle: '修改单价',
      nzWidth: '600px',
      nzContent: SupplyManagementUpdatePriceComponent,
      nzComponentParams: {
        record: item,
      },
      nzFooter: null,
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        this.getGoodsSourceDetail();
      }
    })
  }

  /**
 * 取消货源
 */
  cancleGoodsSource() {
    this.modal.confirm({
      nzTitle: '<b>确定取消货源吗?</b>',
      nzContent: `<b>取消后不可恢复，谨慎操作</b>`,
      nzOnOk: () =>
        this.service.request(this.service.$api_cancle_goods_source, { id: this.i.id }).subscribe((res) => {
          if (res === true) {
            this.service.msgSrv.success('操作成功！');
            this.getGoodsSourceDetail();
          }
        }),
    })
  }

  getGoodsSourceDetail() {
    this.service.request(this.service.$api_get_getBulkDetail, { id: this.id }).subscribe(res => {
      this.i = res;
      this.currentStatus = +this.i?.resourceStatus - 1;
    })
  }

  goBack() {
    window.history.go(-1);
  }
}
