<!-- 任务表单 select组件 -->
<template>
  <view>
    <uv-form-item
      :labelWidth="100"
      :label="item?.taskFiledValue"
      :borderBottom="false"
      labelPosition="left"
      :required="item?.isMustfill == '1'"
      @click="showAction(item)"
    >
      <uv-input
        input-align="right"
        v-model="propsMessage"
        disabled
        disabledColor="#ffffff"
        :placeholder="`请选择${item?.taskFiledValue}`"
        border="none"
      />
      <template v-slot:right>
        <uv-icon name="arrow-right"></uv-icon>
      </template>
    </uv-form-item>

    <uv-picker
      ref="pickerRef"
      :columns="[actionList]"
      keyName="label"
      @confirm="confirm"
    ></uv-picker>
  </view>
</template>

<script setup lang="ts">
import { computed, ref, unref } from 'vue'
import { getPackageListByTaskTypeApi } from '@/service/modules/getTask'

const emmits = defineEmits([
  'update:selectValue',
  'isMultiplePickerSearch',
  'confirmFn',
  'updatePackageListFn',
  'update:selectDisabled'
])

let props = defineProps({
  item: Object,
  index: Number,
  selectValue: String,
  dictList: Object,
  sendParams: Object,
  selectDisabled: Boolean,
  disabled: Boolean
})

//
const pickerRef = ref()
// 选择列表
const actionList = ref()
// 当前的配置
const currFieldItem = ref()

const showAction = async (item: any) => {
  if (props.disabled) return
  if (props.selectDisabled) return
  if (!validateSendParams(item)) return
  if (props.dictList) {
    actionList.value = props.dictList[item.dictGroupName]
    currFieldItem.value = item
    pickerRef.value.open()
  }
}
// 下拉选择确认
const confirm = async (e: any) => {
  let select = e.value[0]
  const fieldItem = unref(currFieldItem)
  // 保存选中的内容
  emmits('confirmFn', fieldItem.taskFiledKey, select.label, select.value)

  // 会议举办1 一对一专访19选择执行包获取对应的drugProducer
  if (
    fieldItem.taskFiledKey === 'temp32' &&
    (fieldItem.taskTypeId === '1' || fieldItem.taskTypeId === '19')
  ) {
    const selectedItem =
      props.dictList &&
      props.dictList[fieldItem.dictGroupName].find((ele: any) => ele.value === select.value)
    drugProducer.value = selectedItem.drugProducer
  }

  // 会议举办1 培训34
  if (
    (fieldItem.taskTypeId == '1' || fieldItem.taskTypeId == '34') &&
    fieldItem.taskFiledKey == 'temp3'
  ) {
    // 禁用下面的下拉
    emmits('update:selectDisabled', true)
    let res = await getPackageListByTaskTypeApi(select.value)
    if (res.data) {
      // 更新list数据
      emmits('updatePackageListFn', res.data)
    }
    emmits('update:selectDisabled', false)
  }
}
const drugProducer = ref()
const getDrugList = (drugProducer: string) => {}

const validateSendParams = (item: any) => {
  // // 医院科室推广会1  选择会议类型
  if (item.taskTypeId === '1' && item.taskFiledKey == 'temp32') {
    if (!props.sendParams?.temp3) {
      uni.showToast({
        title: '请先选择会议类型',
        icon: 'none'
      })
      return
    }
  }
  // 患者教育培训34  选择培训类型
  if (item.taskTypeId === '34' && item.taskFiledKey == 'temp32') {
    if (!props.sendParams?.temp3) {
      uni.showToast({
        title: '请先选择培训类型',
        icon: 'none'
      })
      return
    }
  }
  // 手术跟台42 推广产品
  if (item.taskTypeId === '42' && item.taskFiledKey === 'temp12') {
    emmits('isMultiplePickerSearch')
    return
  }
  // 药店调查18 药店选择
  if (item.taskTypeId === '18' && item.taskFiledKey === 'temp3') {
    let sendVal
    if (props.sendParams && props?.item) {
      sendVal = props.sendParams[props.item.taskFiledKey + 'Id']
    }
    uni.navigateTo({
      url: `/pages-sub-task/pharmacy-select/index?taskTypeId=${props?.item?.taskTypeId}&sendParamsKey=${props?.item?.taskFiledKey}&sendVal=${sendVal}`
    })
    return
  }
  // 会议类型下拉选项为1条时，禁用下拉选项
  if (
    item.taskTypeId == 19 &&
    item.taskFiledKey === 'temp3' &&
    props.dictList &&
    props.dictList[item.dictGroupName] &&
    props.dictList[item.dictGroupName].length === 1
  ) {
    return
  }

  // 选择推广药品
  if (item.dictGroupName === 'package_drug_list') {
    if (!props.sendParams?.temp32) {
      uni.showToast({
        title: '请先选择关联执行包',
        icon: 'none'
      })
      return
    }
    if (!drugProducer.value) {
      uni.showToast({
        title: '请重新选择关联执行包',
        icon: 'none'
      })
      return
    }
    getDrugList(drugProducer.value)
  }
  return true
}

const propsMessage = computed({
  get: () => {
    return props.selectValue
  },
  set: (val) => {
    emmits('update:selectValue', val)
  }
})
</script>

<style lang="scss" scoped></style>
