<!-- 选择商业公司与医院 -->
<template>
  <view class="get-list">
    <view class="header">
      <view class="search">
        <uv-input
          :placeholder="`搜索${typeName}名称`"
          clearable
          border="surround"
          prefixIcon="search"
          prefixIconStyle="font-size: 32px;color: #909399"
          shape="circle"
          @input="searchValChange"
          :customStyle="customStyle"
          v-model="params.name"
        />
        <view class="add-btn" @click="showModal">新增</view>
      </view>
    </view>
    <view class="line"></view>
    <view class="content" v-if="params.total != 0">
      <uv-radio-group v-model="radioValue" placement="column ">
        <uv-radio
          @change="radioGroupChange(item)"
          size="18"
          :customStyle="{ margin: '8px' }"
          v-for="item in dataList"
          :key="item.id"
          :label="item.yymc || item.gsmc"
          :name="item.id"
        >
        </uv-radio>
      </uv-radio-group>
    </view>
    <noData v-else />
    <uv-modal
      ref="modalRef"
      :title="`新增${typeName}`"
      show-cancel-button
      @confirm="onConfirm"
      @cancel="onCancel"
    >
      <view class="slot-content">
        <uv-input
          border="surround"
          v-model="newName"
          :customStyle="inputCustomStyle"
          clearable
          :placeholder="`请输入${typeName}名称`"
        />
      </view>
    </uv-modal>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import noData from '@/components/noData/index.vue'
import { debounce } from '@/utils/index'
import { useTask } from '@/store/task'
import {
  getListHospitalByNameApi,
  getListCompanyByNameApi,
  getHospitalDetailApi,
  getHospitalByNameApi,
  getCompaniesDetailApi,
  getCompaniesByNameApi
} from '@/service/modules/getTask'

const useTaskStore = useTask()

const sendParamsKey = ref()
const taskTypeId = ref()

class Params {
  total = -1
  current = 1
  size = 20
  name = ''
}
const params = ref(new Params())
const radioValue = ref()

const typeName = ref('')
onLoad(async (e) => {
  typeName.value = e?.taskTypeId === '15' ? '医院' : '商业公司'
  taskTypeId.value = e?.taskTypeId
  sendParamsKey.value = e?.sendParamsKey
  dataList.value = []
  await getList()
  if (e?.sendVal) {
    let curr = dataList.value.find((item) => item.yymc === e.sendVal || item.gsmc === e.sendVal)
    if (curr) radioValue.value = curr.id
  }
})

const dataList = ref<any[]>([])
async function getList() {
  uni.showLoading({
    title: '加载中...'
  })
  let res
  if (taskTypeId.value === '15') {
    res = await getListHospitalByNameApi(params.value)
  } else {
    res = await getListCompanyByNameApi(params.value)
  }

  if (res.data) {
    params.value.total = res.data.total
    dataList.value = dataList.value.concat(res.data.records)
  }
  uni.hideLoading()
}

const customStyle = {
  background: '#f6f6f6',
  border: 'none',
  height: '40rpx'
}

const searchValChange = debounce(() => {
  params.value.current = 1
  dataList.value = []
  getList()
})

// 底部
onReachBottom(() => {
  if (dataList.value.length === params.value.total) return
  params.value.current++
  getList()
})

const radioGroupChange = (item: any) => {
  if (taskTypeId.value === '15') {
    sendInfoHospital(item)
  } else {
    sendInfoCompanies(item)
  }
}

// 根据医院详情回显数据 对用接口的
const sendInfoHospital = async (item: any) => {
  const res = await getHospitalDetailApi(item.id)
  if (res.code === 0) {
    sendInfoTask(res.data)
  }
}
const sendInfoHospitalByAdd = async () => {
  const res = await getHospitalByNameApi({ name: newName.value })
  if (res.data) {
    sendInfoTask(res.data)
  } else {
    sendSigleSelect()
  }
}
const sendInfoCompaniesByAdd = async () => {
  const res = await getCompaniesByNameApi({ name: newName.value })
  if (res.data) {
    sendInfoTask(res.data)
  } else {
    sendSigleSelect()
  }
}
// 获取商业公司详情
const sendInfoCompanies = async (item: any) => {
  const res = await getCompaniesDetailApi(item.id)
  if (res.code === 0) {
    sendInfoTask(res.data)
  }
}

const sendSigleSelect = () => {
  const info = {
    sendParamsKey: sendParamsKey.value,
    taskTypeId: taskTypeId.value,
    name: newName.value
  }
  useTaskStore.setSingleSelectByAdd(info)
  uni.navigateBack()
}

const sendInfoTask = (info: any) => {
  const data = {
    sendParamsKey: sendParamsKey.value,
    taskTypeId: taskTypeId.value,
    data: info
  }
  useTaskStore.setSingleSelect(data)
  uni.navigateBack()
}

const inputCustomStyle = {
  width: '550rpx'
}
const modalRef = ref()
const newName = ref('')
const showModal = () => {
  modalRef.value.open()
}
const onCancel = () => {
  newName.value = ''
  modalRef.value.close()
}
const onConfirm = async () => {
  if (taskTypeId.value === '15') {
    sendInfoHospitalByAdd()
  } else {
    sendInfoCompaniesByAdd()
  }
}
</script>

<style lang="scss" scoped>
.get-list {
  min-height: 85vh;
  .header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 999;
    background-color: #fff;

    .search {
      padding: 0 30rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .add-btn {
        margin-left: 20rpx;
        text-align: center;
        width: 150rpx;
        height: 56rpx;
        line-height: 56rpx;
        background: #6eb657;
        border-radius: 50rpx;
        color: #fff;
      }
    }
  }
  .line {
    margin-top: 80rpx;
    height: 20rpx;
    background: #f2f2f2;
    width: 100%;
  }

  .content {
    padding: 0 30rpx;
    background-color: #fff;
  }
}
</style>
