<template>
  <ay-container>
    <div class="s-fz">
      <div class="section">所属物流车</div>
      <div class="chunk center justify-between">
        <div>
          <div class="font-bold mb-spac">{{ func.convert.nullView(data.truckInfo.carNumber) }}</div>
          <div>所属物流：{{ func.convert.nullView(data.truckInfo.orgName) }}</div>
        </div>
        <div class="p-color" v-if="data.truckInfo.truckId" @click="methods.detail">车辆详情</div>
      </div>
      <div class="center justify-between">
        <div class="section">承运协作车</div>
        <div class="p-color" @click="methods.save">添加爱车</div>
      </div>
      <div>
        <div
          class="chunk center justify-between items-end"
          v-for="(t, i) in data.truckInfo.personTruckList"
          :key="i"
        >
          <div>
            <div class="font-bold mb-spac">{{ func.convert.nullView(t.carNumber) }}</div>
            <div>协作物流：{{ func.convert.nullView(t.orgName) }}</div>
          </div>
          <div class="p-color bs-fz">
            <uni-icons
              type="compose"
              color=""
              size="none"
              class="mr-spacd4"
              @click="methods.save(t)"
            />
            <uni-icons type="trash-filled" color="" size="none" @click="methods.del(t)" />
          </div>
        </div>
      </div>
    </div>
    <ayb-carNumber ref="carNumber" />
  </ay-container>
</template>

<script lang="ts" setup>
const carNumber = ref()
const data = reactive({
  truckInfo: {} as 司机车辆管理信息,
})
const methods = {
  getTruckInfo() {
    webapi.strategy.get_truck_info().then((res) => {
      data.truckInfo = res
    })
  },
  detail() {
    ay.goPage(config.pages.truckInfo_detail, { params: { truckId: data.truckInfo.truckId } })
  },
  del(ti: PersonDriver对象 = {}) {
    func.native
      .showModal({
        title: '确认删除',
        content: `确定要删除【${ti.carNumber}】车辆信息吗？`,
        showCancel: true,
      })
      .then((res) => {
        if (res.confirm) {
          webapi.strategy.unbind_person_truck({ id: ti.id }).then((res) => {
            if (res) {
              func.native.showToast('删除成功')
              methods.getTruckInfo()
            }
          })
        }
      })
  },
  save(ti: PersonDriver对象 = {}) {
    if (!ti.id && data.truckInfo.personTruckList.length >= 3) {
      func.native.showToast('承运协作车最多添加3辆')
      return
    }
    carNumber.value.open(ti.carNumber).then((cn) => {
      webapi.strategy
        .bind_person_truck<{ userId: string }>({
          userId: store.user.userInfo.user_id,
          newCarNumber: cn,
          id: ti.id,
        })
        .then((res) => {
          if (res) {
            if (ti.id) {
              func.native.showToast('修改成功')
            } else {
              func.native.showToast('添加成功')
            }
            methods.getTruckInfo()
          }
        })
    })
  },
}

ay.entrance((args) => {
  methods.getTruckInfo()
})
</script>

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