import { Injectable, Injector } from '@angular/core';
import { BaseService } from '../core/base.service';
import { map } from 'rxjs/operators';
import { cacheConf } from '@conf/cache.conf';
import { EACacheService } from '..';
import { of } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ShipperBaseService extends BaseService {
  $api_get_carless_carrier = ``; // 获取承运人
  $api_get_enterprise_project = `/api/mdc/cuc/enterpriseProject/getEnterpriseProjectList`; // 所属项目列表
  $api_get_staff_list = `/api/mdc/cuc/userApp/getStaffList`; //查询企业项目员工列表(录单员)
  $api_get_network_freight_forwarder_list = `/api/mdc/cuc/networkTransporter/findAll`; // 获取网络货运人
  $api_get_settlement_customer_list = `/api/mdc/cuc/settlementCustomer/findAll`; // 查找所有结算客户信息(下拉)
  $api_get_crm_customer_list = `/api/mdc/cuc/enterprise/getCrmSelectList`; // 查找所有Crm客户信息(下拉)
  $api_get_network_freight_forwarder_one = `/api/mdc/cuc/networkTransporter/get`; // 获取网络货运人
  $api_get_roles = `/api/mdc/cuc/roleInfo/getRoleList`; // 获取角色列表
  // 根据FullKey获取系统子配置(树)
  $api_getSysConfigTreeByParentFullKey = `/api/mdc/pbc/sysConfig/getSysConfigTreeByParentFullKey`;
  // 获取全部返佣配置
  $api_get_rebate_config = `/api/mdc/rebateConfig/list/listRebateConfig`;
  // 获取渠道销售管理集合
  $api_get_channel = `/api/mdc/channelSalesManagement/list/listChannelSalesManagement`;

  // 获取货主企业列表
  public $api_enterpriceList = '/api/mdc/cuc/enterpriseInfo/operate/enterpriceList';
  constructor(public injector: Injector) {
    super(injector);
  }

  /**
   * 获取无车承运人
   * @returns
   */
  getCarlessCarrier() {
    const params = {};
    return this.request(this.$api_get_carless_carrier, params, 'POST').pipe(
      map(res => {
        if (res) {
          res.map((m: any) => {
            return { label: m.platformName, value: m.operationId };
          });
        }
      })
    );
  }

  /**
   * 获取所属项目
   * @returns
   */
  getEnterpriseProject(params?: any) {
    // if (this.list.length > 0) {
    //   return of(this.list);
    // }
    return this.request(this.$api_get_enterprise_project, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.projectName,
            value: item.id
          };
        });
        const obj = [{ value: '', label: '全部' }];
        // this.list = [...obj, ...list];
        return [...obj, ...list];
      })
    );
  }

  /**
   * 获取录单员
   * @returns
   */
  getStaffList(params = {}, containerAll = true) {
    return this.request(this.$api_get_staff_list, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.name,
            value: item.appUserId
          };
        });
        const obj = [];
        if (containerAll) {
          obj.push({ label: '全部', value: '' });
        }
        return [...obj, ...list];
      })
    );
  }

  /**
   * 获取调度员
   * @returns
   */
  getStaffList2(params = {}) {
    return this.request(this.$api_get_staff_list, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: `${item.name}/${item.telephone}`,
            value: item.appUserId
          };
        });
        return [...list];
      })
    );
  }

  /**
   * 获取网络货运人
   * @returns
   */
  getNetworkFreightForwarder(params = {}, containerAll = false) {
    return this.request(this.$api_get_network_freight_forwarder_list, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.enterpriseName,
            value: item.id
          };
        });
        const obj = [];
        if (containerAll) {
          obj.push({ label: '全部', value: '' });
        }
        return [...obj, ...list];
      })
    );
  }
  /**
   * 获取网络货运人
   * @returns
   */
  getNetworkEnterpriseName(params = {}, containerAll = false) {
    return this.request(this.$api_get_network_freight_forwarder_list, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.enterpriseName,
            value: item.enterpriseName
          };
        });
        const obj = [];
        if (containerAll) {
          obj.push({ label: '全部', value: '' });
        }
        return [...obj, ...list];
      })
    );
  }

  /**
   * 获取CRM客户
   * @returns
   */
  getCRM(params = {}, containerAll = false) {
    return this.request(this.$api_get_crm_customer_list, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.enterpriseName,
            value: item.enterpriseId
          };
        });
        const obj = [];
        if (containerAll) {
          obj.push({ label: '全部', value: '' });
        }
        return [...obj, ...list];
      })
    );
  }

  /**
   * 获取CRM客户 crmCustomerId
   * @returns
   */
  getCRMCustomerId(params = {}, containerAll = false) {
    return this.request(this.$api_get_crm_customer_list, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.enterpriseName,
            value: item.crmCustomerId
          };
        });
        const obj = [];
        if (containerAll) {
          obj.push({ label: '全部', value: '' });
        }
        return [...obj, ...list];
      })
    );
  }

  /**
   * 获取货主企业列表
   * @returns
   */
  getEnterpriceList(params = { enterpriseName: '' }, containerAll = false) {
    let str = params.enterpriseName.replace(/^\s+|\s+$/g, '');
    if (str) {
      return this.request(this.$api_enterpriceList, params)
        .pipe(
          map((res: any) => {
            if (!res) {
              return [];
            }
            const list = res.map((item: any) => {
              return { label: item.enterpriseName, value: item.id };
            });
            const obj = [];
            if (containerAll) {
              obj.push({ label: '全部', value: '' });
            }
            return [...obj, ...list];
          })
        )
        .toPromise();
    } else {
      return of([]);
    }
  }

  /**
   * 获取结算客户
   * @returns
   */
  getCloseAccount(params = {}, containerAll = false) {
    return this.request(this.$api_get_settlement_customer_list, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.customerName,
            value: item.id
          };
        });
        const obj = [];
        if (containerAll) {
          obj.push({ label: '全部', value: '' });
        }
        return [...obj, ...list];
      })
    );
  }

  /**
   * 获取角色列表
   * @returns
   */
  getRoles(params = {}, containerAll = false) {
    return this.request(this.$api_get_roles, params).pipe(
      map((res: any) => {
        if (!res) {
          return [];
        }
        const list = res.map((item: any) => {
          return {
            label: item.roleName,
            value: item.id
          };
        });
        const obj = [];
        if (containerAll) {
          obj.push({ label: '全部', value: '' });
        }
        return [...obj, ...list];
      })
    );
  }

  /**
   * 根据ID获取网络货运人
   * @returns
   */
  getNetworkTransporterById(id: string) {
    return this.request(this.$api_get_network_freight_forwarder_one, { id });
  }

  // 根据FullKey获取系统子配置(树)
  loadConfigByKey(configFullKey: string = '') {
    return this.http.post(this.$api_getSysConfigTreeByParentFullKey, { configFullKey }).pipe(
      map((m: any) => {
        if (m.success === true) {
          return (m.data as any[]) || [];
        } else {
          this.msgSrv.warning(m.msg);
          return [];
        }
      })
    );
  }

  /**
   * 获取全部返佣配置
   * @returns
   */
  getRebateConfig() {
    const params = {};
    return this.request(this.$api_get_rebate_config, params, 'POST').pipe(
      map(res => {
        if (res) {
          return res.map((m: any) => {
            return { label: m.configName, value: m.id };
          });
        } else {
          return [];
        }
      })
    );
  }

  /**
   * 获取渠道销售管理集合
   * @returns
   */
  getChannel(params = {}, containerAll = false) {
    return this.request(this.$api_get_channel, params).pipe(
      map(res => {
        if (res) {
          const obj = [];
          if (containerAll) {
            obj.push({ label: '全部', value: '' });
          }
          return [...obj, ...res.map((m: any) => ({ label: `${m.name}/${m.telephone}`, value: m.id }))];
        } else {
          return [];
        }
      })
    );
  }
}
