<template>
  <Panel :title="'服务项目'">
    <div class="content">
      <div class="list">
        <ul>
          <li class="item" v-for="(item, index) in list" :key="index">
            <div class="index">{{ index + 1 }}</div>
            <div class="name">
              <el-popover
                  width="485"
                  @show="show(item)"
                  @hide="hide"
              >
                <template #reference>
                  {{ item.itemName }}
                </template>
                <div class="detail">
                  <div style="display: flex;justify-content: space-between">
                    <div style="font-size: 18px;font-weight: 500;color: #000">{{
                        itemInfo.name
                      }}[{{ chrgitm_lv[itemInfo.chrgitmLv as keyof typeof chrgitm_lv || '-'] || '-' }}]
                    </div>
                    <div>￥{{ item.unitPrice }}/{{ item.unit }}</div>
                  </div>
                  <div style="display: flex;justify-content: space-between">
                    <div>限制条件:{{ itemInfo.lmtUsedFlag == 0 ? '否' : itemInfo.lmtUsedFlag == 1 ? '是' : '-' }}</div>
                    <div> 医保码:{{ item.itemSocialCode || '-' }}</div>
                  </div>
                </div>
              </el-popover>
            </div>
            <div class="price">￥{{ item.unitPrice }}</div>
            <div class="unit">
              <div style="display: flex; align-items: center;">
                <el-input-number v-model="item.selectedNum" min="1" size="small"></el-input-number>
                <span style="margin-left: 10px; line-height: 30px;">{{ item.unit }}</span>
              </div>
            </div>
            <div class="sub-price">￥{{ item.unitPrice * item.selectedNum }}</div>
            <div class="delete">
              <div @click="deleteItem(item.id)" class="delete-btn">
                <el-icon>
                  <Close/>
                </el-icon>
              </div>
            </div>
          </li>
        </ul>
      </div>
      <div class="search">
        <div class="search-input">
          <SearchInput
              :request-api="serviceSearchApi"
              :show-config="serviceShowConfig"
              @selectedCallBack="serviceSelect"
              :placeholder="'请输入项目名称'"
              :disabled="disabled"
              @focus="focus"
          >
          </SearchInput>
        </div>
        <span style="margin-right: 24px">￥{{
            list.reduce((acc, cur) => acc + cur.unitPrice * cur.selectedNum, 0)
          }}</span></div>
    </div>
  </Panel>
</template>
<script setup lang="ts">
import {defineModel, computed, defineEmits, ref} from "vue";
import Panel from "@/components/common/Panel.vue";
import {Close} from '@element-plus/icons-vue'
import SearchInput from "@/components/SearchInput.vue";
import {post} from "@/utils/request.ts";
import chrgitm_lv from "@/assets/config/directory/chrgitmLv.json"

const props = defineProps({
  status: {
    type: Number,
    default: 0
  }
})
const disabled = computed(() => {
  if (props.status === 1) {
    return true
  }
})
const serviceSearchApi = "item/search";
const serviceShowConfig = [
  {
    label: "服务名称",
    prop: "itemName",
  },
  {
    label: "单价",
    prop: "unitPrice",
  },
  {
    label: "单位",
    prop: "unit",
  },
]
const serviceSelect = (row: any) => {
  row.selectedNum = 1
  list.value.push(row)
}

const list = defineModel<any[]>({default: () => []});
const deleteItem = (id: any) => {
  list.value = list.value.filter((item) => item.id !== id);
};
const emit = defineEmits(['focus'])
const focus = (e: any) => {
  emit('focus', e)
}
const itemInfo = ref<any>({});
const show = (item: any) => {
  post('social/directory/getItemByCode', {code: item.itemSocialCode}).then((res: any) => {
    itemInfo.value = res
  })
}
const hide = () => {
  itemInfo.value = {}
}
</script>

<style scoped lang="scss">
@use "@/assets/scss/base";

.content {
  display: flex;
  flex-direction: column;

  .list {
    flex: 1;
    min-height: 0;

    .item {
      height: 30px;
      border-top: 1px solid #EAEAEC;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;

      .index {
        height: 100%;
        width: 50px;
        text-align: center;
        line-height: 30px;
      }

      .name {
        flex: 1;
        margin-left: 10px;
        height: 100%;
        line-height: 30px;
        white-space: nowrap; /* 防止文本换行 */
        overflow: hidden; /* 隐藏溢出的文本 */
        text-overflow: ellipsis; /* 显示省略号 */
      }

      .code {
        flex: 1;
        margin-left: 10px;
        height: 100%;
        line-height: 30px;
      }

      .price {
        height: 100%;
        width: 100px;
        text-align: center;
        line-height: 30px;
      }

      .unit {
        height: 100%;
        width: 180px;
        margin-left: 10px;
        line-height: 30px;
      }

      .sub-price {
        height: 100%;
        line-height: 30px;
      }

      .delete {
        height: 100%;
        width: 100px;
        text-align: center;
        line-height: 30px;

        .delete-btn {
          cursor: pointer;

          &:hover {
            color: base.$primary-color;
          }
        }
      }
    }
  }

  .search {
    width: 100%;
    height: 64px;
    border-top: 1px solid #EAEAEC;
    display: flex;
    align-items: center;

    .search-input {
      height: 100%;
      flex: 1;
    }
  }
}
</style>