<template>
  <view class="container">
    <tui-form ref="form" :show-message="false" :model="formData">
      <tui-form-item asterisk label="名称" prop="treetype">
        <tui-input padding="0" :borderBottom="false" placeholder="请输入名称" maxlength="30" :disabled="true"
                   v-model="formData.treetype"></tui-input>
      </tui-form-item>

      <tui-row>
        <tui-col :span="12">
          <tui-form-item label="高度" asterisk prop="treeheight" :position="3">
            <tui-input padding="0" :borderBottom="false" placeholder="请输入" textRight maxlength="10" :disabled="true"
                       v-model="formData.treeheight"></tui-input>
            <template v-slot:right>
              <text style="padding-left: 12rpx;color:#aaa;font-size:14px;">米</text>
            </template>
          </tui-form-item>
        </tui-col>
        <tui-col :span="12">
          <tui-form-item label="胸径" asterisk prop="dbh" :position="3">
            <tui-input padding="0" :borderBottom="false" placeholder="请输入" textRight maxlength="10" :disabled="true"
                       v-model="formData.dbh"></tui-input>
            <template v-slot:right>
              <text style="padding-left: 12rpx;color:#aaa;font-size:14px">厘米</text>
            </template>
          </tui-form-item>
        </tui-col>
      </tui-row>


      <tui-form-item
          asterisk
          label="位置"
          class="location-form-item"
          prop="growlocation"
      >
        <tui-input padding="0" :borderBottom="false" placeholder="请选择" :disabled="true"
                   v-model="formData.growlocation"></tui-input>
        <template v-slot:right>
          <tui-icon name="gps" :size="20"></tui-icon>
        </template>
      </tui-form-item>

      <tui-col :span="12">
        <tui-form-item

            label="纬度"
            class="location-form-item"

            prop="latitude"
        >
          {{ formData.latitude }}
        </tui-form-item>
      </tui-col>
      <tui-col :span="12">
        <tui-form-item
            label="经度"
            class="location-form-item"
            prop="latitude"
        >
          {{ formData.longitude }}
        </tui-form-item>
      </tui-col>


      <tui-form-item asterisk label="管护单位" prop="managedutyunit">

        <tui-input padding="0" :borderBottom="false" placeholder="请输入" maxlength="30" :disabled="true"
                   v-model="formData.managedutyunit"></tui-input>

        <!--        <tui-input padding="0" :borderBottom="false" placeholder="请选择" disabled-->
        <!--                   backgroundColor="transparent" v-model="formData.managedutyunit"></tui-input>-->
      </tui-form-item>
      <tui-form-item asterisk label="权属分类" arrow highlight prop="oldtreeownershipText">
        <tui-input padding="0" :borderBottom="false" placeholder="请选择" :disabled="true"
                   backgroundColor="transparent" v-model="formData.oldtreeownershipText"></tui-input>
      </tui-form-item>

      <tui-form-item label="图片信息" asterisk prop="treeImgList">

        <!--        <tui-image-group :imageList="formData.treeImgList" radius="0"></tui-image-group>-->

        <!-- 图片组组件 -->
        <tui-image-group
            :imageList="formData.treeImgList"
            radius="0"
            @click="handleImageClick"
        >
        </tui-image-group>

        <!-- 使用ThorUI的tui-upload组件 -->
        <!--           @remove="handleRemove"   :multiple="true"    :value="formData.treeImgList"-->
        <!--        <tui-upload  :disabled="true"-->
        <!--            :height="140"-->
        <!--            :width="140"-->
        <!--            :value="formData.treeImgListFile"-->
        <!--            :serverUrl="serverURL" :header="{'Authorization': userToken}"-->
        <!--            @complete="handleComplete"-->
        <!--            @remove="handleRemove"-->
        <!--            :limit="3"-->
        <!--            accept="image/*"-->
        <!--        >-->
        <!--          &lt;!&ndash; 自定义上传按钮 &ndash;&gt;-->
        <!--          <view class="upload-btn">-->
        <!--            <tui-icon name="plus" size="32" color="#999"></tui-icon>-->

        <!--          </view>-->
        <!--          &lt;!&ndash; 提示信息 &ndash;&gt;-->
        <!--          &lt;!&ndash;        <view class="upload-tips">&ndash;&gt;-->
        <!--          &lt;!&ndash;          最多上传3张图片，单张不超过5MB&ndash;&gt;-->
        <!--          &lt;!&ndash;        </view>&ndash;&gt;-->


        <!--        </tui-upload>-->
        <!-- 提示信息 -->
        <!--        <view class="upload-tips">-->
        <!--          最多上传3张图片，单张不超过5MB-->
        <!--        </view>-->
      </tui-form-item>


      <tui-row>
        <tui-col :span="12">
          <tui-form-item label="拉丁文" prop="latinname">
            <tui-input padding="0" :borderBottom="false" placeholder="请输入" maxlength="30" :disabled="true"
                       v-model="formData.latinname"></tui-input>
          </tui-form-item>
        </tui-col>
        <tui-col :span="12">
          <tui-form-item label="级别" arrow highlight prop="treeleveltext">
            <tui-input padding="0" :borderBottom="false" placeholder="请选择" :disabled="true"
                       backgroundColor="transparent" v-model="formData.treeleveltext"></tui-input>
          </tui-form-item>
        </tui-col>
      </tui-row>

      <tui-form-item label="生长环境" prop="growthenvironment">
        <tui-input padding="0" :borderBottom="false" placeholder="请输入" maxlength="50" :disabled="true"
                   v-model="formData.growthenvironment"></tui-input>
      </tui-form-item>


      <tui-row>
        <tui-col :span="12">
          <tui-form-item label="预估树龄" prop="estimationtreeage" :position="3">
            <tui-input padding="0" :borderBottom="false" placeholder="请输入" textRight maxlength="10" :disabled="true"
                       v-model="formData.estimationtreeage"></tui-input>
            <template v-slot:right>
              <text style="padding-left: 12rpx;color:#aaa;font-size: 14px">年</text>
            </template>
          </tui-form-item>
        </tui-col>
        <tui-col :span="12">
          <tui-form-item label="干周" prop="weekday" :position="3">
            <tui-input padding="0" :borderBottom="false" placeholder="请输入" textRight maxlength="10" :disabled="true"
                       v-model="formData.weekday"></tui-input>
            <template v-slot:right>
              <text style="padding-left: 12rpx;color:#aaa;font-size: 14px">厘米</text>
            </template>
          </tui-form-item>
        </tui-col>
      </tui-row>


      <tui-row>
        <tui-col :span="12">
          <tui-form-item label="东西冠幅" prop="canopyeastwest" :position="3">
            <tui-input padding="0" :borderBottom="false" placeholder="请输入" textRight maxlength="10" :disabled="true"
                       v-model="formData.canopyeastwest"></tui-input>
            <template v-slot:right>
              <text style="padding-left: 12rpx;color:#aaa">米</text>
            </template>
          </tui-form-item>
        </tui-col>
        <tui-col :span="12">
          <tui-form-item label="南北冠幅" prop="canopysouthnorth" :position="3">
            <tui-input padding="0" :borderBottom="false" placeholder="请输入" textRight maxlength="10" :disabled="true"
                       v-model="formData.canopysouthnorth"></tui-input>
            <template v-slot:right>
              <text style="padding-left: 12rpx;color:#aaa;font-size: 14px">米</text>
            </template>
          </tui-form-item>
        </tui-col>
      </tui-row>

    </tui-form>
    <tui-gallery :urls="urls" :show="show"  @hide="hide"></tui-gallery>
  </view>
</template>

<script>
import { uploadURL, OSSURL } from '@/config/app'
import { treeOwnership, treeLevelReq } from "@/api/tree";
const rules = [{
  name: "treetype",
  rule: ["required"],
  msg: ["请输入名称"]
}, {
  name: "treeheight",
  rule: ["required", "maxLength:10", "isAmount"],
  msg: ["请输入数高", "数高不能超过10个字符", "请输入正确"]
}, {
  name: "dbh",
  rule: ["required", "maxLength:10", "isAmount"],
  msg: ["请输入胸径", "胸径不能超过10个字符", "请输入正确"]
}, {
  name: "estimationtreeage",
  rule: ["maxLength:10", "isAmount"],
  msg: ["胸径不能超过10个字符", "请输入正确"]
},
  {
    name: "weekday",
    rule: ["maxLength:10", "isAmount"],
    msg: ["胸径不能超过10个字符", "请输入正确"]
  },
  {
    name: "canopyeastwest",
    rule: ["maxLength:10", "isAmount"],
    msg: ["胸径不能超过10个字符", "请输入正确"]
  },
  {
    name: "canopysouthnorth",
    rule: ["maxLength:10", "isAmount"],
    msg: ["胸径不能超过10个字符", "请输入正确"]
  }, {
    name: "growlocation",
    rule: ["required"],
    msg: ["请地图选择位置"]
  }, {
    name: "managedutyunit",
    rule: ["required"],
    msg: ["请选择权属单位"]
  }, {
    name: "oldtreeownershipText",
    rule: ["required"],
    msg: ["请选择权属分类"]
  }, {
    name: "treeImgListFile",
    rule: ["required", "custom"], // 增加required强制必选
    msg: ["请上传图片", "请至少上传一张图片"], // 对应两个规则的提示
    custom: function (value) {
      // 即使required通过，再校验数组长度
      return value.length > 0;
    }
  }];
export default {
  data() {
    return {
// 当前预览索引
      currentIndex: 0,
      // 是否显示预览弹窗
      show: false,
      urls: [],
      treeOwnershipData: [],
      treeLevelData: [], // 级别
      //仅对部分数据进行收集演示
      formData: {
        //数名
        treetype: '',
        //树高
        treeheight: '',
        //胸径
        dbh: '',
        //级别
        treelevel: '',
        treeleveltext: '',
        //权属单位
        managedutyunit: '',
        //权属分类
        oldtreeownership: '',
        oldtreeownershipText: '',
        latinname: '', // 拉丁名
        estimationtreeage: '', // 估测树龄
        canopysouthnorth: '', // 冠幅南北
        canopyeastwest: '', // 冠幅东西
        weekday: '', // 干周
        growlocation: '', // 位置
        growthenvironment: '', // 生长环境
        treeImgList: [], // 图片信息
        treeImgListFile: [], // 图片信息
        address: '',      // 位置名称
        latitude: '',     // 纬度
        longitude: '',     // 经度
        road: ''
      },
    }
  },
  onReady() {
    //开启即时校验，开启后输入即校验【参数必传】，第一参数：是否开启；第二参数：校验规则
    // 开启即时校验，参数：是否开启、校验规则
    this.$refs.form && this.$refs.form.immediateValidate(true, rules);
  },
  onLoad(options) {
    // const eventChannel = this.getOpenerEventChannel();
    // // eventChannel.on监听，获取上一页面通过eventChannel事件传送的数据
    // eventChannel.on('pageDataTreeInfo', data=> {
    //   console.log(data) // {data: 123, title: "A页面"，source: 1, content: "点个赞点个赞，求求了"}
    //   console.log('4123')
    //   this.formData =data
    // })
    this.formData = {
      "searchEntities": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null,
      "beginTime": null,
      "endTime": null,
      "exportType": null,
      "params": {},
      "id": 22,
      "treeid": 28,
      "treenumber": "daf23b62-d295-4131-b15d-fca6259d251f",
      "latinname": "3",
      "treetype": "312名称",
      "treeheight": "31",
      "canopy": null,
      "dbh": "312",
      "growthvigor": null,
      "treelevel": "treeLevel-2",
      "longitude": "116.422014",
      "latitude": "39.901119",
      "location": null,
      "maintainunit": null,
      "managedutyunit": "单位12321",
      "street": null,
      "oldtreeownership": "oldTreeOwnership-6",
      "datastate": null,
      "estimationtreeage": "12",
      "canopyeastwest": "4",
      "canopysouthnorth": "11",
      "weekday": "55",
      "growlocation": "北京市东城区崇文门东大街北京明城墙遗址公园内(西侧)",
      "growthenvironment": "123",
      "treephotoone": "https://test.jichengshanshui.com.cn:28310/yuanlin/alioss/yl/images/2025/08/27/0564bc2272e08f791946b2eab1f5c9e0.jpeg",
      "treephototwo": null,
      "treephotothree": null,
      "treephotofour": null,
      "treephotofive": null,
      "treeImgList": ["https://test.jichengshanshui.com.cn:28310/yuanlin/alioss/yl/images/2025/08/27/0564bc2272e08f791946b2eab1f5c9e0.jpeg"],
      "deleted": 0,
      "createby": "严家齐",
      "updateby": "严家齐",
      "createtime": "2025-08-27 15:01:48",
      "updatetime": "2025-08-27 15:01:48"
    }
    treeLevelReq().then(res => {
      this.treeLevelData = res.data
      const optiontreelevel = this.treeLevelData.find(item => item.dictValue === this.formData.treelevel);
      if (optiontreelevel) {
        this.formData.treelevel = optiontreelevel.dictValue;
        this.formData.treeleveltext = optiontreelevel.dictLabel;
      }
    })
    treeOwnership().then(res => {
      this.treeOwnershipData = res.data
      const option = this.treeOwnershipData.find(item => item.dictValue === this.formData.oldtreeownership);
      if (option) {
        this.formData.oldtreeownership = option.dictValue;
        this.formData.oldtreeownershipText = option.dictLabel;
      }
    })
    this.urls = this.formData.treeImgList.map((item, index) => {
      return {
        src: item
      }
    });
    this.formData.treeImgList = this.formData.treeImgList.map((item, index) => {
      return {
        id: index,
        src: item
      }
    });
  },
  methods: {
// 图片点击事件
    handleImageClick(e) {
      console.log(e.id)
      // 如果组件自带预览功能，可能不需要手动控制显示
      // 否则手动显示预览弹窗
      this.show = true;
    },
    hide(){
      this.show = false
    }
  }
}
</script>

<style>
.tui-title {
  width: 100%;
  font-size: 28rpx;
  color: #888;
  padding: 30rpx;
  box-sizing: border-box;
}

.tui-btn__box {
  width: 100%;
  position: fixed;
  bottom: 0;
}

.location-content {
  font-size: 14px;
}

/* 动画区域样式 */
.animated-area {
  height: 200rpx;
  transition: all 0.3s ease-out; /* 动画过渡效果 */
}
</style>