import _extends from "@babel/runtime/helpers/esm/extends";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import ksButton from "../button";
import { http } from "../ks-utils";
import Toast from '../toast';
import Dialog from '../dialog';
import "./index.css";

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "ks-employee-list-box"
  }, [_c('div', {
    staticClass: "ks-employee-list-content",
    style: {
      height: _vm.multiple ? 'calc(100% - 50px)' : '100%'
    }
  }, [_vm.historyList.length ? _c('div', {
    staticClass: "history"
  }, [_c('div', {
    staticClass: "title"
  }, [_c('span', [_vm._v("搜索历史")]), _vm._v(" "), _c('img', {
    attrs: {
      "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-employee/delete.png"
    },
    on: {
      "click": _vm.clearAll
    }
  })]), _vm._v(" "), _c('div', {
    staticClass: "list-row"
  }, _vm._l(_vm.historyList, function (item, index) {
    return _c('div', {
      key: index,
      staticClass: "row"
    }, [_c('div', {
      class: {
        'row-context': true,
        'history-item-disabled': item._disabledItem || _vm.historySelected(item)
      },
      on: {
        "click": function click($event) {
          return _vm.selectHistory(item);
        }
      }
    }, [_vm._t("historyListInfo", [_c('div', {
      staticClass: "context-left"
    }, [_c('img', {
      staticClass: "avatar",
      attrs: {
        "src": item.headUrl + _vm.imgSize
      }
    }), _vm._v(" "), _c('span', {
      staticClass: "name"
    }, [_vm._v(_vm._s(item.employeeName) + "-" + _vm._s(item.position))]), _vm._v(" "), item.workingContent ? _c('div', {
      staticClass: "work-context"
    }, [_c('img', {
      attrs: {
        "src": item.workingIconUrl
      }
    }), _vm._v("\n                  " + _vm._s(item.workingContent) + "\n                ")]) : _vm._e(), _vm._v(" "), item.workType ? _c('div', {
      staticClass: "work-status",
      style: _vm.workStatusStyle(item)
    }, [_vm._v("\n                  " + _vm._s(_vm.getWorkText(item.workType)) + "\n                ")]) : _vm._e()])], {
      "item": item
    }), _vm._v(" "), _c('div', {
      staticClass: "close",
      on: {
        "click": function click($event) {
          $event.stopPropagation();
          return _vm.deleteRecord(item);
        }
      }
    }, [_c('img', {
      attrs: {
        "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-employee/close.png"
      }
    })])], 2)]);
  }), 0)]) : _c('div', {
    staticClass: "list"
  }, [_vm.searchList.length > 0 ? _c('div', {
    staticClass: "cell-box"
  }, _vm._l(_vm.searchList, function (item, index) {
    return _c('div', {
      key: index,
      staticClass: "cell",
      on: {
        "click": function click($event) {
          return _vm.handleSelect(item);
        }
      }
    }, [_vm.multiple ? _c('div', {
      staticClass: "cell-checked"
    }, [item._disabledItem ? _c('img', {
      staticClass: "radio-icon",
      attrs: {
        "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-employee/disabled-user.png"
      }
    }) : item._isChecked ? _c('img', {
      staticClass: "radio-icon",
      attrs: {
        "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-employee/radio-sel.png"
      }
    }) : _c('img', {
      staticClass: "radio-icon",
      attrs: {
        "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-employee/radio.png"
      }
    })]) : _vm._e(), _vm._v(" "), _c('img', {
      class: {
        'employee-img': true,
        'employee-img-disabled': item._disabledItem
      },
      attrs: {
        "src": item.headUrl + _vm.imgSize,
        "alt": ""
      }
    }), _vm._v(" "), _vm._t("employeeListInfo", [_c('div', {
      staticClass: "info"
    }, [_c('div', {
      staticClass: "info-top"
    }, [item.$name ? _c('div', {
      class: {
        'info-name': true,
        'info-name-disabled': item._disabledItem
      },
      domProps: {
        "innerHTML": _vm._s(item.$name)
      }
    }) : _c('div', {
      staticClass: "info-name"
    }, [_vm._v(_vm._s(item.employeeName))]), _vm._v(" "), _c('div', {
      staticClass: "info-tag"
    }, [item.workingContent ? _c('div', {
      staticClass: "work-context"
    }, [_c('img', {
      attrs: {
        "src": item.workingIconUrl
      }
    }), _vm._v("\n                    " + _vm._s(item.workingContent) + "\n                  ")]) : _vm._e(), _vm._v(" "), item.workType && _vm.workStatus[item.workType] ? _c('div', {
      staticClass: "work-status",
      style: _vm.workStatusStyle(item)
    }, [_vm._v("\n                    " + _vm._s(_vm.getWorkText(item.workType)) + "\n                  ")]) : _vm._e()])]), _vm._v(" "), _c('div', {
      class: {
        'info-deparetment': true,
        'info-deparetment-disabled': item._disabledItem
      }
    }, [_vm._v(_vm._s(item.departmentName))])])], {
      "item": item
    })], 2);
  }), 0) : _c('div', {
    staticClass: "no-data-container"
  }, [_c('img', {
    staticClass: "no-data",
    attrs: {
      "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/empty-image/NoResults.png"
    }
  }), _vm._v(" "), _c('div', [_vm._v("没有找到相关结果")])])])]), _vm._v(" "), _vm.multiple ? _c('div', {
    staticClass: "ks-employee-list-footer"
  }, [_c('div', {
    staticClass: "select-num"
  }, [_vm._v("已选" + _vm._s(_vm.selectValue.length) + "人")]), _vm._v(" "), _c('ks-button', {
    attrs: {
      "type": "primary"
    },
    on: {
      "click": _vm.confirm
    }
  }, [_vm._v("完成")])], 1) : _vm._e()]);
};

var __vue_staticRenderFns__ = [];
export default {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: "ks-employee-list",
  components: {
    ksButton: ksButton
  },
  props: {
    showHistory: {
      type: Boolean,
      default: true
    },
    value: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    // 是否多选
    multiple: {
      type: Boolean,
      default: false
    },
    // 多选重复添加的提示文案
    repeatText: {
      type: String,
      default: "请勿重复添加"
    },
    selected: {
      type: Function
    },
    historyListHandler: {
      type: Function
    },
    searchListHandler: {
      type: Function
    },
    disabledItemClick: {
      type: Function
    }
  },
  data: function data() {
    return {
      imgSize: "?thumbnail=132y132&imageView",
      selectValue: this.value,
      historyList: [],
      searchList: [],
      listLoading: false,
      workStatusList: []
    };
  },
  watch: {
    value: function value(val) {
      this.selectValue = val;
    }
  },
  mounted: function mounted() {
    var _this = this;

    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _this.getWorkConfigData();

            case 2:
              _this.getHistory();

            case 3:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  },
  methods: {
    getWorkText: function getWorkText(type) {
      var workItem = this.workStatusList && this.workStatusList.find(function (status) {
        return status.workType === type;
      });
      return workItem && workItem.workTypeWord || '';
    },
    getWorkConfigData: function getWorkConfigData() {
      var _this2 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        var res;
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return _this2.$http('kuasheng.ksWorkType.getWorkTypeList', 10184, {});

              case 2:
                res = _context2.sent;
                _this2.workStatusList = res;

              case 4:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    },
    // ARGB => RGBA
    ArgbtoRgba: function ArgbtoRgba(color) {
      var parseColorString = function parseColorString(color) {
        var hex = color.slice(1);
        var a = 1;

        if (hex.length === 8) {
          a = parseInt(hex.slice(6), 16) / 255;
          hex = hex.slice(0, 6);
        }

        var bigint = parseInt(hex, 16);
        return {
          r: bigint >> 16 & 255,
          g: bigint >> 8 & 255,
          b: bigint & 255,
          a: a
        };
      };

      var toRgbaString = function toRgbaString(colorObj, n) {
        if (n === void 0) {
          n = 10;
        }

        var r = colorObj.r,
            g = colorObj.g,
            b = colorObj.b,
            _colorObj$a = colorObj.a,
            a = _colorObj$a === void 0 ? 1 : _colorObj$a;
        return "rgba(" + r + "," + g + "," + b + "," + Math.floor(a * n) / n + ")";
      };

      var rgbaColor = "#" + color.slice(3, 9) + color.slice(1, 3);
      var colorObj = parseColorString(rgbaColor);
      return toRgbaString(colorObj);
    },
    workStatusStyle: function workStatusStyle(item) {
      var workStatus = this.workStatusList && this.workStatusList.find(function (status) {
        return status.workType === item.workType;
      });
      return {
        color: this.ArgbtoRgba(workStatus.foreColor),
        'background-color': this.ArgbtoRgba(workStatus.bgColor),
        'border-color': this.ArgbtoRgba(workStatus.borderColor)
      };
    },
    getHistory: function getHistory() {
      var _this3 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
        var list, ids, workList, newList;
        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                if (_this3.showHistory) {
                  _context3.next = 2;
                  break;
                }

                return _context3.abrupt("return");

              case 2:
                _context3.prev = 2;
                _context3.next = 5;
                return http("kuasheng.ksSearchRecord.listV2", 10184, {});

              case 5:
                list = _context3.sent;
                _this3.historyList = list;

                if (!(list && list.length)) {
                  _context3.next = 14;
                  break;
                }

                ids = list.map(function (item) {
                  return item.employeeNumber;
                });
                _context3.next = 11;
                return _this3.getWorkData(ids);

              case 11:
                workList = _context3.sent;
                newList = list.map(function (item) {
                  var workItem = workList.find(function (work) {
                    return work.employeeNumber === item.employeeNumber;
                  });

                  if (workItem) {
                    item = _extends({}, item, workItem);
                  }

                  return item;
                });

                if (_this3.historyListHandler && typeof _this3.historyListHandler === 'function') {
                  _this3.historyList = _this3.historyListHandler(newList);
                } else {
                  _this3.historyList = newList;
                }

              case 14:
                _this3.searchList = [];
                _context3.next = 20;
                break;

              case 17:
                _context3.prev = 17;
                _context3.t0 = _context3["catch"](2);
                console.log(_context3.t0);

              case 20:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, null, [[2, 17]]);
      }))();
    },
    getWorkData: function getWorkData(ids) {
      var _this4 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
        var workDataList;
        return _regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                if (!(ids && ids.length)) {
                  _context4.next = 5;
                  break;
                }

                _context4.next = 3;
                return _this4.$http('kuasheng.nimTeam.workingContent', 10184, {
                  employeeNumbers: ids
                });

              case 3:
                workDataList = _context4.sent;
                return _context4.abrupt("return", workDataList);

              case 5:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      }))();
    },
    deleteSelect: function deleteSelect(item) {
      var hasItem = this.searchList.find(function (cell) {
        return cell.employeeNumber === item.employeeNumber;
      });

      if (hasItem) {
        hasItem._isChecked = false;
      }
    },
    getSearch: function getSearch(val) {
      var _this5 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
        var res, selectNumberArry, reg, ids, workList;
        return _regeneratorRuntime.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                if (!(val === "")) {
                  _context5.next = 3;
                  break;
                }

                _this5.getHistory();

                return _context5.abrupt("return");

              case 3:
                _context5.prev = 3;
                _this5.historyList = [];
                _this5.listLoading = true;
                _this5.finished = false;
                _context5.next = 9;
                return http("kuasheng.ksBookVersion.contactSearch", 10184, {
                  keywords: val
                });

              case 9:
                res = _context5.sent;
                _this5.listLoading = false;
                _this5.finished = true;
                selectNumberArry = _this5.selectValue.map(function (v) {
                  return v.employeeNumber;
                });
                reg = new RegExp(val, "gi");
                _this5.searchList = res && res.map(function (item) {
                  if (selectNumberArry.includes(item.employeeNumber)) {
                    item._isChecked = true;
                  } else {
                    item._isChecked = false;
                  }

                  var $name = item.employeeName && item.employeeName.replace(reg, function (value) {
                    return "<span style=\"color: #7972FE;\">" + value + "</span>";
                  });
                  item.$name = $name;
                  return item;
                });

                if (_this5.searchListHandler && typeof _this5.searchListHandler === 'function') {
                  _this5.searchList = _this5.searchListHandler(listItem);
                } else {
                  _this5.searchList = listItem;
                }

                ids = res.map(function (item) {
                  return item.employeeNumber;
                });
                _context5.next = 19;
                return _this5.getWorkData(ids);

              case 19:
                workList = _context5.sent;

                if (workList && workList.length) {
                  _this5.searchList = _this5.searchList && _this5.searchList.map(function (item) {
                    var workItem = workList.find(function (work) {
                      return work.employeeNumber === item.employeeNumber;
                    });

                    if (workItem) {
                      item = _extends({}, item, workItem);
                    }

                    return item;
                  });
                }

                _context5.next = 26;
                break;

              case 23:
                _context5.prev = 23;
                _context5.t0 = _context5["catch"](3);
                console.log(_context5.t0);

              case 26:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5, null, [[3, 23]]);
      }))();
    },
    selectHistory: function selectHistory(item) {
      var selectItem = _extends({}, item, {
        employeeName: item.position ? item.employeeName + "-" + item.position : item.employeeName
      });

      this.handleSelect(selectItem, 'history');
    },
    historySelected: function historySelected(item) {
      return this.selectValue.find(function (i) {
        return i.id === item.id;
      });
    },
    handleSelect: function handleSelect(item, type) {
      var _this6 = this;

      try {
        if (item._disabledItem) {
          this.disabledItemClick && this.disabledItemClick(item);
          return;
        }

        var selectCb = this.selected;

        if (selectCb && typeof selectCb === 'function' && selectCb(item) === true) {
          return Promise.resolve(false);
        }

        this.$emit('selectItem', item, type);

        if (this.multiple) {
          // 多选
          if (item._isChecked) {
            var itemIndex = this.selectValue.findIndex(function (cell) {
              return cell.employeeNumber === item.employeeNumber;
            });
            item._isChecked = !item._isChecked;
            this.selectValue.splice(itemIndex, 1);
          } else {
            var hasItem = this.selectValue.find(function (cell) {
              return cell.employeeNumber === item.employeeNumber;
            });

            if (hasItem) {
              Toast(this.repeatText);
              return;
            }

            item._isChecked = !item._isChecked;
            this.selectValue.push(item);
            this.$emit('input', this.selectValue);
            this.$nextTick(function () {
              // 数组插入后，头像平移到最右边
              _this6.$emit('_moveHeadRight');
            });
          }
        } else {
          // 单选
          this.selectValue = [item];
          this.confirm();
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 删除记录
    deleteRecord: function deleteRecord(item) {
      var _this7 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
        return _regeneratorRuntime.wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                _context6.next = 2;
                return _this7.addHistoryRecord([item], 0);

              case 2:
                _context6.next = 4;
                return _this7.getHistory();

              case 4:
                Toast("删除成功!");

              case 5:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6);
      }))();
    },
    addHistoryRecord: function addHistoryRecord(list, type) {
      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
        var recordList;
        return _regeneratorRuntime.wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                _context7.prev = 0;

                if (list.length) {
                  _context7.next = 3;
                  break;
                }

                return _context7.abrupt("return");

              case 3:
                recordList = list.map(function (item) {
                  return {
                    employeeName: item.employeeName,
                    headUrl: item.headUrl,
                    openId: item.openId,
                    state: type
                  };
                });
                _context7.next = 6;
                return http("kuasheng.ksSearchRecord.changeV2", 10184, {
                  params: recordList
                });

              case 6:
                _context7.next = 11;
                break;

              case 8:
                _context7.prev = 8;
                _context7.t0 = _context7["catch"](0);
                console.log(_context7.t0);

              case 11:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee7, null, [[0, 8]]);
      }))();
    },
    confirm: function confirm() {
      this.addHistoryRecord(this.selectValue, 1);
      this.$emit("input", this.selectValue);
      this.$emit("confirm", this.selectValue);
    },
    clearAll: function clearAll() {
      var _this8 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
        return _regeneratorRuntime.wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                try {
                  Dialog.confirm({
                    title: "清空搜索历史",
                    message: "确认清空全部搜索历史？"
                  }).then( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
                    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
                      while (1) {
                        switch (_context8.prev = _context8.next) {
                          case 0:
                            _context8.next = 2;
                            return _this8.$http('kuasheng.KsSearchRecord.deleteAll', 10184, {});

                          case 2:
                            _this8.getHistory();

                            Toast('删除成功!');

                          case 4:
                          case "end":
                            return _context8.stop();
                        }
                      }
                    }, _callee8);
                  })));
                } catch (error) {
                  console.log(error);
                }

              case 1:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9);
      }))();
    }
  }
};