function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useEffect } from 'react';
import { Select, Table, Modal } from 'antd';
import { PlusCircleOutlined } from '@ant-design/icons';
import net from "../../../services/net";
// 选择云手机通用组件
export default function SelectCph(_ref) {
  var value = _ref.value,
    requestUrl = _ref.requestUrl,
    onChange = _ref.onChange,
    disabled = _ref.disabled,
    _ref$cphRowKey = _ref.cphRowKey,
    cphRowKey = _ref$cphRowKey === void 0 ? 'id' : _ref$cphRowKey,
    selectProps = _ref.selectProps,
    tableProps = _ref.tableProps,
    modalProps = _ref.modalProps;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    dataSource = _useState4[0],
    setDataSource = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    selectedRowKeys = _useState6[0],
    setSelectedRowKeys = _useState6[1];
  var _useState7 = useState({
      current: 1,
      pageSize: 10,
      total: 0
    }),
    _useState8 = _slicedToArray(_useState7, 2),
    tablePagination = _useState8[0],
    setTablePagination = _useState8[1];
  var _useState9 = useState(false),
    _useState10 = _slicedToArray(_useState9, 2),
    loading = _useState10[0],
    setLoading = _useState10[1];
  var _ref2 = selectProps || {},
    mode = _ref2.mode;
  useEffect(function () {
    initTableData(tablePagination);
  }, []);
  useEffect(function () {
    setSelectedRowKeys(value);
  }, [value]);
  var initTableData = function initTableData(pagination) {
    setLoading(true);
    net(requestUrl || 'https://console-siyu.excelliance.cn/api/device/list', {
      method: 'POST',
      data: {
        pageNo: (pagination === null || pagination === void 0 ? void 0 : pagination.current) || 1,
        pageSize: (pagination === null || pagination === void 0 ? void 0 : pagination.pageSize) || 10
      }
    }).then(function (res) {
      var _res$data, _res$data2;
      setDataSource((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.dataSource);
      setTablePagination((_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.pagination);
    }).finally(function () {
      setLoading(false);
    });
  };
  var handleSelectChange = function handleSelectChange(val) {
    setSelectedRowKeys(val);
  };
  var handleOk = function handleOk() {
    setVisible(false);
    onChange(selectedRowKeys);
  };
  var handleCancel = function handleCancel() {
    setVisible(false);
    setSelectedRowKeys(value || []);
  };
  var tableColumns = [{
    title: '云手机IP',
    dataIndex: 'host'
  }, {
    title: 'adb端口',
    dataIndex: 'portAdb'
  }, {
    title: 'scrcpy端口',
    dataIndex: 'portScrcpy'
  }, {
    title: '是否分配',
    dataIndex: 'assignLabel'
  }, {
    title: '组织名称',
    dataIndex: 'orgName'
  }, {
    title: '所属环境',
    dataIndex: 'env'
  }];
  var rowSelection = _objectSpread(_objectSpread({}, tableProps === null || tableProps === void 0 ? void 0 : tableProps.rowSelection), {}, {
    selectedRowKeys: selectedRowKeys,
    preserveSelectedRowKeys: true,
    // 保留选中项
    onChange: function onChange(rowKeys) {
      setSelectedRowKeys(rowKeys);
    }
  });
  var handleTableChange = function handleTableChange(pagination) {
    setTablePagination(pagination);
    initTableData(pagination);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Select, _extends({
    mode: "multiple",
    placeholder: "\u8BF7\u9009\u62E9\u4E91\u624B\u673A",
    style: {
      width: '100%'
    },
    open: false,
    onClick: function onClick() {
      setVisible(!visible);
    },
    value: selectedRowKeys,
    showArrow: true // 显示箭头，开启这个属性才能使suffixIcon生效
    ,
    suffixIcon: /*#__PURE__*/React.createElement(PlusCircleOutlined, {
      style: {
        fontSize: '16px',
        color: '#c5c9d0'
      }
    }),
    onChange: handleSelectChange,
    disabled: disabled
  }, selectProps)), /*#__PURE__*/React.createElement(Modal, _extends({
    title: "\u9009\u62E9\u4E91\u624B\u673A",
    open: visible,
    onOk: handleOk,
    onCancel: handleCancel,
    width: (modalProps === null || modalProps === void 0 ? void 0 : modalProps.width) || 800,
    okText: "\u786E\u8BA4",
    cancelText: "\u53D6\u6D88"
  }, modalProps), /*#__PURE__*/React.createElement(Table, _extends({
    columns: tableColumns,
    rowKey: cphRowKey,
    dataSource: dataSource,
    loading: loading,
    pagination: tablePagination,
    rowSelection: rowSelection,
    onChange: handleTableChange
  }, tableProps))));
}