<!--用户选择组件-->
<template>
  <div>
    <StAddBiz @handleOpen="handleOpen" :loading="loadingEcho" v-bind="attrs" :showButton="false" @change="callBakSelect" />
    <CaSelectModal @register="regModal" :checkValue="checkValue" @getSelectResult="setValue" modalTitle="添加控制项" v-bind="getBindValue" />
  </div>
</template>
<script lang="ts">
  import { SelectValue } from 'ant-design-vue/es/select';
  import { defineComponent, provide, reactive, ref, unref, watch, watchEffect, nextTick } from 'vue';
  import { array } from 'vue-types';
  import StAddBiz from './base/StAddBiz.vue';
  import CaSelectModal from './modal/CaSelectModal.vue';
  import { useModal } from '/@/components/Modal';
  import { useRuleFormItem } from '/@/hooks/component/useFormItem';
  import { useAttrs } from '/@/hooks/core/useAttrs';
  import { propTypes } from '/@/utils/propTypes';

  export default defineComponent({
    name: 'StSelectLable',
    components: {
      CaSelectModal,
      StAddBiz,
    },
    inheritAttrs: false,
    props: {
      value: propTypes.oneOfType([propTypes.string, propTypes.array]),
      label: propTypes.oneOfType([propTypes.string, propTypes.array]),
      labelKey: {
        type: String,
        default: 'caName',
      },
      rowKey: {
        type: String,
        default: 'id',
      },
      params: {
        type: Object,
        default: () => {},
      },
    },
    emits: ['options-change', 'change', 'update:value'],
    setup(props, { emit }) {
      const emitData = ref<any[]>();
      //注册model
      const [regModal, { openModal }] = useModal();
      //表单值
      const [state] = useRuleFormItem(props, 'value', 'change', emitData);

      //下拉框选项值
      const selectOptions = ref<SelectValue>([]);
      //下拉框选中值
      let selectValues = reactive<Recordable>({
        value: [],
        change: false,
      });
      const checkValue = ref([]);
      // 是否正在加载回显数据
      const loadingEcho = ref<boolean>(false);
      //下发 selectOptions,xxxBiz组件接收
      provide('selectOptions', selectOptions);
      //下发 selectValues,xxxBiz组件接收
      provide('selectValues', selectValues);
      //下发 loadingEcho,xxxBiz组件接收
      provide('loadingEcho', loadingEcho);

      const tag = ref(false);
      const attrs = useAttrs();

      /**
       * 监听组件值
       */
      watchEffect(() => {
        props.value && initValue();
        if (!props.value) {
          selectValues.value = [];
          return false;
        }
      });

      watch(selectValues, () => {
        if (selectValues) {
          state.value = selectValues.value;
        }
      });

      const actIds = ref([]);
      function handleOpen() {
        tag.value = true;
        openModal(true, {
          selActIds: selectValues.value,
          isUpdate: false,
        });
      }
      function callBakSelect(data) {
        checkValue.value = data;
      }
      /**
       * 将字符串值转化为数组
       */
      function initValue() {
        let value = props.value ? props.value : [];
        if (value && typeof value === 'string' && value != 'null' && value != 'undefined') {
          state.value = value.split(',');
          selectValues.value = value.split(',');
        } else {
          if (Array.isArray(value[0])) {
            let caNams = Object.assign([], value[0]);
            actIds.value = Object.assign([], value[1]);
            let options: { label: string; value: string }[] = [];
            for (let i = 0; i < actIds.value.length; i++) {
              options.push({
                label: caNams[i],
                value: actIds.value[i],
              });
              selectValues.value = options;
              setValue(options, actIds.value);
            }
          }
        }
      }

      /**
       * 设置下拉框的值
       */
      function setValue(options, values) {
        selectOptions.value = options;
        state.value = values;
        selectValues.value = values;
      }
      const getBindValue = Object.assign({}, unref(props), unref(attrs), selectOptions.value);

      nextTick(() => {
        setTimeout(() => {
          initValue();
        }, 500);
      });

      return {
        state,
        attrs,
        selectOptions,
        getBindValue,
        selectValues,
        loadingEcho,
        tag,
        checkValue,
        regModal,
        setValue,
        handleOpen,
        callBakSelect,
      };
    },
  });
</script>
<style lang="less" scoped>
  .j-select-row {
    @width: 82px;

    .left {
      width: calc(100% - @width - 8px);
    }

    .right {
      width: @width;
    }

    .full {
      width: 100%;
    }

    :deep(.ant-select-search__field) {
      display: none !important;
    }
  }
</style>
