<template>
  <div class="mobile-page-container">
    <!-- 档期补差 -->
    <div class="item"
      v-for="(obj, index) in displayGroup">
      <p class="title">档期补差{{ index + 1 }}：</p>
      <van-cell-group inset>
        <van-field v-model="obj.modality"
          name="modality"
          label="提报内容"
          placeholder="请输入提报内容"
          auto-complete="off"
          :rules="[{ required: true, message: '请输入提报内容' }]" />
        <van-field v-model="obj.cost"
          name="cost"
          label="执行情况"
          placeholder="执行情况"
          auto-complete="off"
          :rules="[{ required: true, message: '请输入执行情况' }]" />
        <van-field label="档期补差照片"
          label-align="top"
          class="header-photo-section">
          <template #input>
            <van-uploader :max-count="2"
              accept="image/*"
              capture="camera"
              reupload
              :model-value="obj.displayPhotos"
              :name="index"
              :after-read="displayPhotosRead"
              preview-size="78"
              @delete="deletedisplayPhotos">
            </van-uploader>
          </template>
        </van-field>
        <van-field label="补差核查结果"
          label-align="top">
          <template #input>
            <van-checkbox-group v-model="obj.verify"
              direction="horizontal"
              shape="square">
              <van-checkbox name="已执行">已执行</van-checkbox>
              <van-checkbox name="部分执行">部分执行</van-checkbox>
              <van-checkbox name="绑赠差异">绑赠差异</van-checkbox>
              <van-checkbox name="未执行">未执行</van-checkbox>
            </van-checkbox-group>
          </template>
        </van-field>
      </van-cell-group>
    </div>
    <!-- 新增按钮 -->
    <van-button class="add-item"
      type="primary"
      @click="addItem">新增一组陈列</van-button>
    <!-- 总体判定 -->
    <!-- <div class="overall">
      <p class="title">档期陈列总体判定结果：</p>
      <van-checkbox-group v-model="overAll.verify"
        direction="horizontal"
        shape="square">
        <van-checkbox name="合格">合格</van-checkbox>
        <van-checkbox name="需整改">需整改</van-checkbox>
        <van-checkbox name="不合格">不合格</van-checkbox>
        <van-checkbox name="虚假">虚假</van-checkbox>
      </van-checkbox-group> 
    </div> -->
  </div>
</template>

<script setup>
import { uploadFileToOSSAPI } from '@/api'
import useUserStore from '@/store/modules/user'
import { v4 as uuidv4 } from 'uuid'

const route = useRoute()
/*************** 陈列组信息 ***************/
const displayGroup = ref([
  {
    modality: '', // 形式
    cost: '', // 费用
    displayPhotos: [], // 陈列照片
    verify: [] // 核查结果
  }
])
// 新增一组
const addItem = () => {
  displayGroup.value.push({
    modality: '', // 形式
    cost: '', // 费用
    displayPhotos: [], // 陈列照片
    verify: [] // 核查结果
  })
}

/*************** 照片 ***************/
const displayPhotosRead = async (file, { name, index }) => {
  const target = displayGroup.value[name]
  const pictureUrl = await uploadFileToOSSAPI(`risk/${route.query.storeCode}/scheduleAdjustmentPhoto/${useUserStore().userInfo.employeeNo}/${uuidv4()}.png`, file.file)
  target.displayPhotos.push({
    url: pictureUrl
  })
  showNotify({ type: 'success', message: '照片上传成功' })
}
// 删除照片
const deletedisplayPhotos = async (file, { name, index }) => {
  const target = displayGroup.value[name]
  target.displayPhotos.splice(index, 1)
  showNotify({ type: 'success', message: '照片删除成功' })
}


// 总体核查结果
const overAll = ref({})
</script>

<style scoped
  lang="scss">
  .mobile-page-container {
    padding: 10px 10px 0 10px;
    background-color: white;
    font-size: 14px;

    /* 公共样式 */
    .title {
      margin: 0;
      font-weight: 900;
      color: var(--main-color);
    }

    /* 每个分组 */
    .item {
      margin-bottom: 10px;

      .van-cell-group {
        padding: 0;
        margin: 0;

        .van-cell {
          padding-left: 0px;
        }

        .header-photo-section {
          margin-top: 10px;

          ::v-deep(.van-uploader__upload) {
            width: 80px !important;
            height: 80px !important;
            border: 1px dashed #ccc;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #f8f8f8;
          }
        }

        /* 核查结果 */
        .van-checkbox {
          margin-bottom: 10px;
        }
      }

      &:after {
        content: " ";
        display: block;
        width: 100%;
        border-bottom: 1px solid gray;
      }

    }

    /* 新增一组 */
    .add-item {
      width: 100%;
      height: 30px;
      margin-top: 10px;
    }

    /* 总体判定 */
    .overall {
      background-color: white;
      padding-top: 10px;

      .van-checkbox-group {
        margin-top: 10px;

        .van-checkbox {
          margin-bottom: 10px;
        }
      }
    }
  }
</style>