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 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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
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),
    _useState0 = _slicedToArray(_useState9, 2),
    loading = _useState0[0],
    setLoading = _useState0[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;