<!-- @format -->

<template>
  <div class="form-group">
    <div class="controls" style="display: flex">
      <!--选择区域-->
      <div class="controls iaas-input" style="flex: 1; position: relative">
        <input style="width: 100%" class="base-input" disabled />
        <span @click="searchPerson()" class="select-btn"
          >&nbsp;&nbsp;{{ buttonLabel }}&nbsp;&nbsp;
        </span>
      </div>
    </div>

    <!--多选结果展示区域-->
    <div class="form-group">
      <div class="multi-content" style="display: flex; width: 100%">
        <label class="control-label"><span></span></label>
        <div class="multi-content-show" style="flex: 1">
          <div class="multi-content multi-placeholder">{{ multiplePlaceholder }}</div>
          <div class="multi-content">
            <span class="multi-item-label" v-for="item in selectedItems">
              <span style="margin-right: 10px">{{ item[labelField] }}</span>
              <ns-icon name="close" @click="onRemove(item)" />
            </span>
          </div>
        </div>
      </div>
      <ns-modal v-model:visible="visible" @ok="handleOk" width="900px">
        <ns-view-list-table
          v-if="visible"
          ref="nsTableRef"
          v-bind="tableConfig"
          :model="data"
          :rowKey="valueField" />
      </ns-modal>
    </div>
  </div>
</template>
<script lang="ts">
  import moment from 'moment';
  import { defineComponent, ref, watch, unref, computed } from 'vue';
  import { NsMessage } from '/nerv-lib/component/message';
  import { http } from '/nerv-lib/util/http';
  import { isArray } from 'lodash-es';
  export default defineComponent({
    name: 'SelectRolePerson',
    components: {},
    props: {
      dataSource: {
        type: Object,
        default: [],
      },
      labelField: {
        type: String,
        default: 'name',
      },
      valueField: {
        type: String,
        default: 'uuid',
      },
    },
    emits: ['change', 'updata:longLat', 'validate', 'getChildrenOptions'],
    setup(props, { emit }) {
      const nsTableRef = ref();
      const visible = ref<Boolean>(false);
      let buttonLabel = ref('选择员工');
      let multiplePlaceholder = ref('点击选择按钮选择');
      let selectedItems = ref([]);
      let selectedItemsList = ref([]);
      const valueField = ref();
      valueField.value = props.valueField;
      if (props) {
        if (props && isArray(props.dataSource)) {
          selectedItems.value = props.dataSource;
          console.log(props.dataSource);
          selectedItems.value.forEach((ele) => {
            selectedItemsList.value.push(ele[valueField.value]);
          });
          change();
        }
      }
      // const changeTable = (item) => {
      //   emit('change', currentList);
      // };
      const dealSelectItem = (selectKey: any, selectRow: any) => {
        const initList = [];
        selectKey.forEach((item) => {
          let info = selectRow.filter((x) => x?.[props.valueField] === item)[0];
          if (!info) {
            let dInfo = props.dataSource.filter((x) => x?.[props.valueField] === item)[0];
            !dInfo ? '' : initList.push(dInfo);
          } else {
            initList.push(info);
          }
        });
        selectedItems.value = initList;
      };
      const tableConfig = {
        api: '/api/parking_merchant/objs/person/pageList',
        params: {
          page: 0,
          pageSize: 10,
        },
        rowSelection: {
          selectedRowKeys: selectedItemsList,
          onChange: (selectedRowKeys, selectedRows) => {
            // selectedItemsList.value = selectedRowKeys;
            // selectedItems.value = selectedRows;
            dealSelectItem(selectedRowKeys, selectedRows);
            selectedItemsList.value = selectedRowKeys;
          },
          getCheckboxProps: (record) => {
            console.log(record);
            return {
              undefined,
              props: {
                disabled: false,
                // name: record.name,
                defaultChecked: selectedItemsList.value.indexOf(record[valueField.value]) != -1,
              },
            };
          },
        },
        scroll: { x: 1350 },
        columns: [
          {
            title: '姓名',
            dataIndex: 'personName',
            width: 200,
          },
          {
            title: '性别',
            dataIndex: 'sex',
            width: 100,
            customRender: (value) => {
              switch (value.text) {
                case 0:
                  return '女';
                case 1:
                  return '男';
                default:
                  return '-';
              }
            },
          },
          {
            title: '员工编号',
            dataIndex: 'personNum',
            width: 150,
          },
          {
            title: '手机号码',
            dataIndex: 'telNum',
            width: 150,
          },
          {
            title: '账号状态',
            dataIndex: 'accountState',
            width: 150,
            customRender: (value) => {
              switch (value.text) {
                case 1:
                  return '启用';
                case 0:
                  return '禁用';
                default:
                  return '-';
              }
            },
          },
          {
            title: '用户状态',
            dataIndex: 'personStatus',
            customRender: (value) => {
              switch (value.text) {
                case 1:
                  return '在职';
                case 2:
                  return '离职';
                default:
                  return '-';
              }
            },
          },
        ],

        formConfig: {
          schemas: [
            {
              field: 'accountState',
              label: '账号状态',
              component: 'NsSelect',
              componentProps: {
                placeholder: '请选择',
                options: [
                  {
                    label: '全部',
                    value: '',
                  },
                  {
                    label: '启用',
                    value: 1,
                  },
                  {
                    label: '禁用',
                    value: 0,
                  },
                ],
              },
            },
            {
              field: 'personStatus',
              label: '用户状态',
              component: 'NsSelect',
              componentProps: {
                placeholder: '请选择',
                options: [
                  {
                    label: '全部',
                    value: '',
                  },
                  {
                    label: '在职',
                    value: 1,
                  },
                  {
                    label: '离职',
                    value: 2,
                  },
                ],
              },
            },
            {
              field: 'telNum',
              label: '手机号码',
              component: 'NsInput',
              componentProps: {
                placeholder: '请输入手机号码',
              },
            },
            {
              field: 'personName',
              label: '用户姓名',
              component: 'NsInput',
              componentProps: {
                placeholder: '请输入用户姓名',
              },
            },
          ],
        },
        rowKey: 'personUuid',
      };
      function onRemove(item) {
        if (item) {
          for (let i = selectedItems.value.length - 1; i >= 0; i--) {
            if (selectedItems.value[i][props.valueField] == item[props.valueField]) {
              selectedItems.value.splice(i, 1);
            }
          }
          change();
        }
      }
      function searchPerson() {
        visible.value = true;
      }

      const handleOk = async () => {
        let selectedRows = unref(nsTableRef).nsTableRef.tableState.selectedRows;
        visible.value = false;
        if (isArray(selectedRows)) {
          selectedRows.forEach((ele) => {
            let temp = true;
            selectedItems.value.forEach((element) => {
              if (ele[props.valueField] == element[props.valueField]) {
                temp = false;
              }
            });
            if (temp) {
              let obj = {};
              obj[props.valueField] = ele[props.valueField];
              obj[props.labelField] = ele[props.labelField];
              selectedItems.value.push(obj);
            }
          });
        }
        change();
      };
      function change() {
        let list = [];
        selectedItems.value.forEach((element) => {
          list.push(element[props.valueField]);
        });
        emit('change', list);
      }
      watch(
        () => props.dataSource,
        () => {
          selectedItems.value = props.dataSource;
          change();
        },
        {
          immediate: true,
        },
      );
      return {
        buttonLabel,
        multiplePlaceholder,
        selectedItems,
        tableConfig,
        visible,
        nsTableRef,
        valueField,
        selectedItemsList,
        //方法
        handleOk,
        searchPerson,
        change,
        onRemove,
      };
    },
    data() {
      return {
        editingKey: '',
        propsList: [],
      };
    },
    created() {},
    methods: {},
  });
</script>
<style lang="less" scoped>
  .select-btn {
    position: absolute;
    right: 0;
    top: 0;
    background: #0ed2bf;
    color: white;
    bottom: 0;
    cursor: pointer;
  }
  //多选部分显示样式
  .multi-content-show {
    border: 1px solid #dfdfdf;
    margin-top: 10px;
    min-height: 90px;
    width: 500px;
    max-height: 150px;
    overflow-y: scroll;
    .multi-content {
      padding: 10px;
      display: flex;
      flex-wrap: wrap;
      line-height: 22px;
      &.multi-placeholder {
        color: #354052;
        opacity: 0.3;
      }
    }
    .multi-item-label {
      height: 24px;
      padding: 4px 10px 4px 10px;
      font-size: 12px;
      color: #37474f;
      border: 1px solid #cfd8dc;
      background: #f9f9f9;
      border-radius: 12px;
      margin-right: 20px;
      margin-bottom: 5px;
      line-height: 24px;
      position: relative;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .iconfont {
        width: 24px;
        height: 24px;
        position: absolute;
        right: 0.3em;
        top: 0;
        color: #cfd8dc;
        font-size: 12px;
        transform: scale(0.67, 0.67);
        cursor: pointer;
        text-align: center;
        &:hover {
          color: #607d8b;
        }
      }
    }
  }
  ul,
  li {
    list-style: none;
  }
</style>
