function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { BizDialog, BizGlobalDataContext, IconFont } from '@zgfe/business-lib';
import { Button, Space, Switch } from 'antd';
import moment from 'moment';
import React, { useContext, useState } from 'react';
import apis from '../../../constants/api';
import request from '../../../utils/ajax';
import { classPrefix } from '../types';
var Enable = function Enable(_ref) {
  var item = _ref.item,
    setIdList = _ref.setIdList;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    loading = _useState2[0],
    setLoading = _useState2[1];
  var _useState3 = useState(item.status === 'enable'),
    _useState4 = _slicedToArray(_useState3, 2),
    checked = _useState4[0],
    setChecked = _useState4[1];
  var _useContext = useContext(BizGlobalDataContext),
    isDemo = _useContext.isDemo,
    authority = _useContext.authority;
  function onChange(enable) {
    setLoading(true);
    var status = enable ? 'enable' : 'disable';
    request(apis.changeIdEnable, {
      method: 'get',
      params: {
        id: item.id,
        status: status
      }
    }).then(function (res) {
      if (!res) return;
      setChecked(enable);
      setIdList(function (list) {
        return list.map(function (data) {
          return _objectSpread(_objectSpread({}, data), {}, {
            status: data.id === item.id ? status : data.status
          });
        });
      });
    }).finally(function () {
      setLoading(false);
    });
  }
  return /*#__PURE__*/React.createElement(Switch, {
    checked: checked,
    disabled: item.type === 'fixed' || !authority[100016],
    loading: loading,
    checkedChildren: "\u542F\u7528",
    unCheckedChildren: "\u7981\u7528",
    onChange: onChange
  });
};
var BtnDel = function BtnDel(_ref2) {
  var data = _ref2.data,
    idList = _ref2.idList,
    setIdList = _ref2.setIdList;
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    loading = _useState6[0],
    setLoading = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    showConfirm = _useState8[0],
    setShowConfirm = _useState8[1];
  var _useContext2 = useContext(BizGlobalDataContext),
    authority = _useContext2.authority;
  function onDel() {
    setLoading(true);
    request(apis.delId, {
      method: 'get',
      params: {
        id: data.id
      }
    }).then(function (res) {
      if (!res) return;
      setIdList(function (list) {
        return list.filter(function (item) {
          return item.id !== data.id;
        });
      });
      setShowConfirm(false);
    }).finally(function () {
      setLoading(false);
    });
  }
  var isRelated = false; // 当前id是否被用作关联id
  idList.forEach(function (item) {
    if (item.correlationId === data.id) {
      isRelated = true;
    }
  });
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
    size: "small",
    type: "link",
    className: "".concat(classPrefix, "-list-btn-del"),
    onClick: setShowConfirm.bind(undefined, true),
    disabled: data.type === 'fixed' || !authority[100018]
  }, "\u5220\u9664"), /*#__PURE__*/React.createElement(BizDialog, {
    open: showConfirm,
    closable: true,
    okButtonProps: {
      loading: loading
    },
    onCancel: setShowConfirm.bind(undefined, false),
    className: "".concat(classPrefix, "-list-dialog"),
    onOk: onDel,
    title: /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(IconFont, {
      type: "tishi1",
      style: {
        color: '#FD9F41',
        fontSize: '23px'
      }
    }), "\u63D0\u793A")
  }, isRelated ? '此ID被关联，删除后不可恢复，是否确认删除？' : 'ID删除后不可恢复，是否确认删除？'));
};
var BtnSort = function BtnSort(_ref3) {
  var data = _ref3.data,
    idList = _ref3.idList,
    setIdList = _ref3.setIdList,
    direction = _ref3.direction,
    index = _ref3.index,
    length = _ref3.length;
  var disable = direction === 'up' && index === 3 || direction === 'down' && index === length - 1 || data.type === 'fixed';
  var _useState9 = useState(false),
    _useState10 = _slicedToArray(_useState9, 2),
    loading = _useState10[0],
    setLoading = _useState10[1];
  var _useContext3 = useContext(BizGlobalDataContext),
    authority = _useContext3.authority;
  function onSort() {
    var arr = Array.from(idList);
    var _arr$splice = arr.splice(index, 1),
      _arr$splice2 = _slicedToArray(_arr$splice, 1),
      remove = _arr$splice2[0];
    arr.splice(index + (direction === 'up' ? -1 : 1), 0, remove);
    setLoading(true);
    request(apis.sortId, {
      method: 'post',
      data: {
        ids: arr.map(function (item) {
          return item.id;
        })
      }
    }).then(function (res) {
      if (!res) return;
      setIdList(arr);
    }).finally(function () {
      setLoading(false);
    });
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
    size: "small",
    type: "link",
    disabled: disable || !authority[100019],
    onClick: onSort,
    loading: loading
  }, direction === 'up' ? '上移' : '下移'));
};
function getColumnList(_ref4) {
  var idList = _ref4.idList,
    setIdList = _ref4.setIdList,
    onEdit = _ref4.onEdit;
  var _useContext4 = useContext(BizGlobalDataContext),
    authority = _useContext4.authority;
  function strRender(v) {
    return v || '-';
  }
  function dateRender(v) {
    return v ? moment(new Date(v)).format('YYYY/MM/DD HH:mm:ss') : '-';
  }
  var fieldTypes = {
    string: '字符串',
    number: '数值',
    date: '日期'
  };
  var columnList = [{
    title: 'ID',
    dataIndex: 'idName',
    fixed: 'left',
    render: function render(_v, data) {
      return strRender(data.idAlias || data.idName);
    }
  }, {
    title: '类型',
    dataIndex: 'idFieldType',
    render: function render(value) {
      return value ? fieldTypes[value] : '-';
    }
  }, {
    title: '关联ID',
    dataIndex: 'correlationIdName',
    render: strRender
  }, {
    title: '映射表',
    dataIndex: 'mappingTables',
    render: strRender
  }, {
    title: '映射字段',
    dataIndex: 'mappingFields',
    render: strRender
  }, {
    title: '创建者',
    dataIndex: 'createUser',
    render: strRender
  }, {
    title: '创建时间',
    dataIndex: 'createTime',
    render: dateRender
  }, {
    title: '更新时间',
    dataIndex: 'updateTime',
    render: dateRender
  }, {
    title: '操作',
    dataIndex: '-',
    fixed: 'right',
    render: function render(_v, item, index) {
      return /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(Enable, {
        setIdList: setIdList,
        item: item
      }), /*#__PURE__*/React.createElement(Button, {
        size: "small",
        type: "link",
        onClick: onEdit === null || onEdit === void 0 ? void 0 : onEdit.bind(undefined, item),
        disabled: !authority[100017]
      }, "\u7F16\u8F91"), /*#__PURE__*/React.createElement(BtnDel, {
        idList: idList,
        data: item,
        setIdList: setIdList
      }), /*#__PURE__*/React.createElement(BtnSort, {
        idList: idList,
        data: item,
        setIdList: setIdList,
        direction: "up",
        index: index,
        length: idList.length
      }), /*#__PURE__*/React.createElement(BtnSort, {
        idList: idList,
        data: item,
        setIdList: setIdList,
        direction: "down",
        index: index,
        length: idList.length
      }));
    }
  }];
  return columnList;
}
export default getColumnList;