<template>
  <view>
    <view class="wrapper">
      <view class="title">选择发布类型</view>

      <view class="list">
        <view
          v-for="(item, index) in list"
          :key="item.id"
          class="item"
          :style="{ marginTop: index === 0 ? 0 : '60rpx', background: item.background }"
          @click="_onListClick(item, index)"
        >
          <image class="item-icon" :src="item.img" />
          <view class="item-text">{{ item.name }}</view>
        </view>
      </view>

      <u-modal confirmText="我已知晓" :show="modalShow" @confirm="onModalConfirm">
        <view class="modal" :style="`max-height: ${modalHeight}rpx; overflow-y: scroll; white-space: pre-wrap`">
          <u-parse :content="activeItem.desc" />
        </view>
      </u-modal>

      <!-- 取消举报说明
      <view class="footer">
        <u-button class="report-btn" @click="onReportClick">举报说明</u-button>
      </view>

      <u-modal :show="showReport" title="举报说明" @confirm="onReportConfirm">
        <view :style="`max-height: ${modalHeight}rpx; overflow-y: scroll; white-space: pre-wrap`">{{ content }}</view>
      </u-modal>
      -->
    </view>
  </view>
</template>

<script>
const _list = [
  {
    name: '交通违法举报',
    icon: '/static/icon/ic-wf@2x.png',
    background: 'linear-gradient(156deg, #ffaeae 0%, #fe4d4d 100%)',
  },
  {
    name: '城市不文明行为举报',
    icon: '/static/icon/ic-bwm@2x.png',
    background: 'linear-gradient(156deg,#8bbcff 0%, #0d71ff 100%)',
  },
  {
    name: '交通设施故障上报',
    icon: '/static/icon/ic-gz@2x.png',
    background: 'linear-gradient(156deg,#ffdc8b 0%, #ffb00d 100%)',
  },
];

const faultName = '交通设施故障上报'

export default {
  data() {
    return {
      list: [],
      content:
        '1.本平台仅接收荣县行政区域内的交通违法网上举报，且违法行为发生后30日内举报有效。 \n2.举报人不得在驾驶过程中或其他有碍交通安全的情况下进行举报。 \n3.举报人提供的照片和视频应当客观真实，并通过合法途径、合法方式取得。举报人应当如实描述违法行为发生时间、地点，违法机动车号牌、外观和具体违法行为，并能够与举报视频相互印证。缺乏上述要素的，公安交警部门依法不予以受理 \n4.对多次举报失实，恶意举报的举报人，经查证属实的，公安机关交通管理部门不再受理其举报。故意捏造违法事实陷害他人、以举报为手段敲诈勒索的，将按相关规定纳入公民社会征信体系。涉嫌违法犯罪的，依法追究法律责任。 \n5.不属于本平台接受范围的交通违法举报以及对交通执法管理的意见建议，可以联系属地公安交警部。 \n6.举报人在拍摄交通违法视频前，应当仔细阅读各类违法举报的拍摄要点及要求，拍摄的视频文件大小不超过50M。 \n7.如使用行车记录仪视频进行举报，切勿进行压缩、转码等操作，应剪辑行车记录仪设备存储卡内原始视频后上传，以确保上传视频清晰。',
      showReport: false,

      isFirstRecord: 0,
      isFirstRecordId: 0,

      faultTask: { id: 0, parent: { id: 0 } },
      activeItem: {},
      modalShow: false,
    };
  },

  computed: {
    modalHeight() {
      return Math.ceil(window.innerHeight * 2 * 0.6);
    },
  },

  onLoad(query) {
    this.fetchPhotoTaskTypeData();
    // this.fetchPhotoTaskTypeDetail();

    this.isFirstRecord = this.getIsFirstRecord();
  },

  onShow(options) {},

  methods: {
    async fetchPhotoTaskTypeData() {
      const { list } = await uni.$u.http.get('/api/app/phototasktype/list', { params: { parent_id: 0 } });
      this.list = list.map((value, index) => ({ ...value, background: _list[index].background }));

      const fault = this.list.find(v => v.name === faultName)
      this.fetchFalutTask(fault.id)
    },

    async fetchPhotoTaskTypeDetail() {
      const { detail } = await uni.$u.http.get('/api/app/phototasktype/detail', { params: { id: 0 } });
      console.log(detail);
    },

    async fetchFalutTask(id) {
      const { list: [task] } = await uni.$u.http.get('/api/app/phototasktype/list', { params: { parent_id: id } })
      this.faultTask = task
    },

    async onReportClick() {
      console.log('onReportClick');
      this.showReport = true;
    },

    async onReportConfirm() {
      if (!this.isFirstRecord) {
        this.showReport = false;
        this.isFirstRecord = this.getIsFirstRecord();
        uni.$u.route({ url: 'pages/record-type/index', params: { id: this.isFirstRecordId } });
      }
      this.showReport = false;
    },

    _onListClick(item, index) {
      const { name } = item
      // 交通设施故障上报直接进入填写表单
      if (name === faultName) {
        const { id, parent: { id: parentId } } = this.faultTask
        uni.$u.route({
          url: 'pages/record-content/index',
          params: {
            parent_id: parentId,
            detail_id: id,
          },
        });
        return
      }
      this.activeItem = item
      this.modalShow = true
    },

    onListClick(item, index) {
      if (!this.isFirstRecord) {
        this.isFirstRecordId = item.id;
        this.showReport = true;
        return;
      }
      uni.$u.route({ url: 'pages/record-type/index', params: { id: item.id } });
    },

    getIsFirstRecord() {
      const isFirstRecord = uni.getStorageSync('isFirstRecord');
      if (!isFirstRecord) uni.setStorageSync('isFirstRecord', 1);
      return isFirstRecord;
    },

    onModalConfirm() {
      this.modalShow = false
      this.onListClick(this.activeItem)
    }
  },
};
</script>

<style scoped lang="scss">
@import 'index.scss';
</style>
