import helper from 'src/Utils/helper';
import { request } from '../../boot/axios';
import { Pagination } from '../class';
import {
  DeivceSearchEntity,
  DeivceEntity,
  DeivcePointSearchEntity,
  DeivcePointEntity
} from './model/deivce';
const pathName = 'Device/';

class DeviceApi {
  // PLC控制器 分页
  getDevicePageList = (val: Pagination<DeivceSearchEntity>) => {
    let format = `?page=${val.page}&size=${val.rowsPerPage}`;
    if (val.data.deviceCode) {
      format += `&deviceCode=${val.data.deviceCode}`;
    }
    if (val.data.deviceName) {
      format += `&deviceName=${val.data.deviceName}`;
    }
    if (val.data.activated != undefined && val.data.activated != null) {
      format += `&activated=${val.data.activated}`;
    }
    return request.get(`${pathName}GetDevicePageList${format}`);
  };

  // 获取PLC控制器列表
  getDeviceInfoList = (val: DeivceSearchEntity) => {
    const format = helper.params(val);
    return request.get(`${pathName}GetDeviceInfoList${format}`);
  };

  // 根据 id 获取PLC控制器明细
  getDeviceDetail = (id: any) => {
    return request.get(`${pathName}GetDeviceDetail?id=${id}`);
  };

  // 添加PLC控制器
  addDevice = (val: DeivceEntity) => {
    return request.post(`${pathName}AddDevice`, val);
  };

  // 修改PLC控制器
  updateDevice = (val: DeivceEntity) => {
    return request.post(`${pathName}UpdateDevice`, val);
  };

  // 删除PLC控制器
  deleteDevice = (id: any) => {
    return request.post(`${pathName}DeleteDevice?id=${id}`);
  };

  // PLC控制器点位 分页
  getDevicePointPageList = (val: Pagination<DeivcePointSearchEntity>) => {
    let format = `?page=${val.page}&size=${val.rowsPerPage}`;
    if (val.data.deviceId) {
      format += `&deviceId=${val.data.deviceId}`;
    }
    if (val.data.deviceCode) {
      format += `&deviceCode=${val.data.deviceCode}`;
    }
    if (val.data.deviceName) {
      format += `&deviceName=${val.data.deviceName}`;
    }
    if (val.data.equipmentName) {
      format += `&equipmentName=${val.data.equipmentName}`;
    }
    if (val.data.equipmentCode) {
      format += `&equipmentCode=${val.data.equipmentCode}`;
    }
    if (val.data.equipmentType) {
      format += `&equipmentType=${val.data.equipmentType}`;
    }
    if (val.data.pointCode) {
      format += `&pointCode=${val.data.pointCode}`;
    }
    if (val.data.pointName) {
      format += `&pointName=${val.data.pointName}`;
    }
    if (val.data.activated != undefined && val.data.activated != null) {
      format += `&activated=${val.data.activated}`;
    }
    return request.get(`${pathName}GetDevicePointPageList${format}`);
  };

  // 获取PLC控制器点位列表
  getDevicePointList = (val: DeivcePointSearchEntity) => {
    const format = helper.params(val);
    return request.get(`${pathName}GetDevicePointList${format}`);
  };

  // 根据 id 获取PLC控制器点位明细
  getDevicePointDetail = (id: any) => {
    return request.get(`${pathName}GetDevicePointDetail?id=${id}`);
  };

  // 添加PLC控制器点位
  addDevicePoint = (val: DeivcePointEntity) => {
    return request.post(`${pathName}AddDevicePoint`, val);
  };

  // 修改PLC控制器点位
  updateDevicePoint = (val: DeivcePointEntity) => {
    return request.post(`${pathName}UpdateDevicePoint`, val);
  };

  // 删除PLC控制器点位
  deleteDevicePoint = (val: any) => {
    return request.post(`${pathName}DeleteDevicePoint?id=${val}`);
  };
}
const deviceApi = new DeviceApi();

export { deviceApi };
