<template>
  <div class="map-list-content">
    <div class="screen-div">
      <div class="screen-item" style="margin-right:20px;margin-bottom:15px;">
        <div class="screen-item-label">年份</div>
        <a-select
          allowClear
          style="width:130px;"
          v-model:value="params.year"
          :options="props.yearOptions"
        />
      </div>
      <div class="screen-item" style="margin-right:17px;margin-bottom:15px;">
        <div class="screen-item-label">图斑来源</div>
        <a-select
          allowClear
          style="width:130px;"
          v-model:value="params.tubanlaiyuan"
          :options="keyIssusesIPatchSourceOptions"
        />
      </div>
      <div class="screen-item" style="margin-bottom:15px;">
        <div class="screen-item-label">批次</div>
        <a-select
          allowClear
          style="width:103px;"
          v-model:value="params.picihao"
          :options="props.batchOptions"
        />
      </div>
      <div class="screen-item" style="margin-right:13px;">
        <div class="screen-item-label">举证</div>
        <a-select
          allowClear
          style="width:130px;"
          v-model:value="params.is_build_name"
          :options="markTypeOptions"
        />
      </div>
      <div class="screen-item" style="margin-right:10px;">
        <!-- <div class="screen-item-label">图斑号</div> -->
        <a-input style="width:270px;" allowClear v-model:value="params.geomid" class="item-input" placeholder="请输入图斑编号" @press-enter="query"/>
      </div>
      <div class="screen-item" style="display: flex; justify-content: end;margin-bottom: 0px;">
          <a-button type="primary" class="item-button" :icon="h(SearchOutlined)" @click="query">查询</a-button>
      </div>
    </div>
    <div class="sift-div">
      <div class="layout-div">
        <img style="cursor: pointer;width:20px;height: 18px" src="@/assets/images/tiankongdi/back.png" class="img-box"  @click="emits('changeShowParent')"/>
        <div class="interval-div"></div>
        <div class="sift-item" @click="dataListSort('area')">
          <div class="sift-label">总面积</div>
          <div class="sift-icon">
            <div :style="`${showSortMark('area',1)? 'color: #086DEC;': ''}`">&#9650</div>
            <div :style="`${showSortMark('area',2)? 'color: #086DEC;': ''}`">&#9660</div>
          </div>
        </div>
        <div class="sift-item" @click="dataListSort('gengdi_area')">
          <div class="sift-label">耕地面积</div>
          <div class="sift-icon">
            <div :style="`${showSortMark('gengdi_area',1)? 'color: #086DEC;': ''}`">&#9650</div>
            <div :style="`${showSortMark('gengdi_area',2)? 'color: #086DEC;': ''}`">&#9660</div>
          </div>
        </div>
        <div class="sift-item" @click="dataListSort('yongjiujibennongtian_area')">
          <div class="sift-label">永农面积</div>
          <div class="sift-icon">
            <div :style="`${showSortMark('yongjiujibennongtian_area',1)? 'color: #086DEC;': ''}`">&#9650</div>
            <div :style="`${showSortMark('yongjiujibennongtian_area',2)? 'color: #086DEC;': ''}`">&#9660</div>
          </div>
        </div>
        <div class="sift-item" @click="dataListSort('CreateDate')">
          <div class="sift-label">下发时间</div>
          <div class="sift-icon">
            <div :style="`${showSortMark('CreateDate',1)? 'color: #086DEC;': ''}`">&#9650</div>
            <div :style="`${showSortMark('CreateDate',2)? 'color: #086DEC;': ''}`">&#9660</div>
          </div>
        </div>
      </div>
      <div style="display: flex;align-items: center;cursor: pointer;">
        <a-popover placement="bottom">
          <template #content>
            <div style="display:flex;">
              <div>当前状态：</div>
              <div>
                <a-checkbox-group 
                  v-model:value="params.nowStatus"
                  style="width: 100%" 
                  :options="auditMapStatusOptions"></a-checkbox-group>
                </div>
            </div>
          </template>
          <img src="@/assets/images/tiankongdi/filt.png" class="img-box mr-r-20" />
        </a-popover>
        <img src="@/assets/images/tiankongdi/collect-active.png" class="img-box"  @click="getCollectList" v-if="openCollect"/>
        <img src="@/assets/images/tiankongdi/collect.png" class="img-box"  @click="getCollectList" v-else/>
      </div>
     </div>
    <div class="data-list-div" style="padding-top: 3px;">
      <div
        v-for="(item, index) in dataList"
        :key="index"
        class="data-list-item"
        v-if="dataList.length > 0"
      >
      <div class="back-box" v-if="showDrawBack(item)">{{ drawBackSpan(item) }}</div>
      <div class="data-list-layout-div">
          <div class="data-list-title-div">
            <img src="/positioning.png" class="map-mark" @click="locationFun(item)"/>
            <div class="label-div">
              <div class="item-label">{{item.countyname}}</div>
              <div class="item-sub-label">
                <div style="margin-right:12px;max-width: 100px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">{{item.streetname}}</div>
                <span>{{item.case_no}}</span>
                <span class="item-mark" v-if="item.is_build_name">{{item.is_build_name}}</span>
              </div>
            </div>
          </div>
          <div class="button-div">
            <a-button type="primary" class="button-item" @click="goAudit(item)">审核</a-button>
          </div>
        </div>
        <div class="data-list-info-div">
          <div class="info-layout-div">
            <div class="info-item">
              <div class="info-label">总面积</div>
              <div class="info-data" :title="dataProcessing(item.area)">{{dataProcessing(item.area)}}</div>
            </div>
            <div class="info-item">
              <div class="info-label">农用地</div>
              <div class="info-data" :title="dataProcessing(item.nongyongdi_area)">{{dataProcessing(item.nongyongdi_area)}}</div>
            </div>
            <div class="info-item">
              <div class="info-label">耕地</div>
              <div class="info-data" :title="dataProcessing(item.gengdi_area)">{{dataProcessing(item.gengdi_area)}}</div>
            </div>
            <div class="info-item" style="width:127px;">
              <div class="info-label">永农面积</div>
              <div class="info-data" :title="dataProcessing(item.yongjiujibennongtian_area)">{{dataProcessing(item.yongjiujibennongtian_area)}}</div>
            </div>
          </div>
          <div class="info-layout-div">
            <!-- <div>
              <Icon
                :style="`font-size: 30px; color: #314A8C; cursor: pointer;${item.isouttime === 2? 'color: #D03542;': item.isouttime === 1? 'color: #F7710F;': 'color: #314A8C;'}`"
                icon="icon-park-solid:timer"
                @click="()=>{}" 
              />
            </div>
            <div style="background: rgb(237, 237, 237);width: 1px;height: 100%;margin-right: 10px;margin-left: 10px;"></div> -->
            <div style="cursor: pointer;">
              <img src="@/assets/images/tiankongdi/collect-active.png" class="img-box"   @click="collectItem(item)"  v-if="item.Fid"/>
              <img src="@/assets/images/tiankongdi/collect.png" class="img-box"   @click="collectItem(item)"  v-else/>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="pagination-div" v-if="dataList.length > 0">
      <a-pagination
        size="small"
        v-model:current="pageNumber"
        v-model:pageSize="pageSizeNumber"
        :total="total"
        :show-total="total => `共 ${total} 条数据`"
        show-size-changer
        show-quick-jumper
        @change="changePagination"
      />
    </div>
    <a-modal
      width="100%"
      wrap-class-name="full-modal"
      v-model:open="auditOpen"
      title="填报审核"
      footer=""
      :destroyOnClose="true"
    >
      <template #footer> </template>
      <div class="handoff">
        <a-button
          type="primary" 
          style="margin-right: 25px;"
          @click="prevData"
        >上一条</a-button>
        <a-button
          type="primary"
          @click="nextData"
        >下一条</a-button>
      </div>
      <Audit
        v-if="handoffShow"
        ref="posRef"
        :processId="processId"
        :taskId="taskId"
        :isRead="isRead"
        :type="type"
        :auditData="auditData"
        @closeModel="closeMolder"
      />
    </a-modal>
  </div>
</template>

<script setup lang="ts">
  import { onMounted, ref, defineEmits, computed, h, watch } from 'vue';
  import { StarOutlined,StarFilled } from '@ant-design/icons-vue';
  import Icon from '@/components/Icon/Icon.vue';
  import { getLoadTaskDetailList, addTaskFavorite, deleteTaskCase } from '@/api/keyproblem/keyissuesI/index';
  import { flowStore } from '@/store/modules/flow';
  import { getDetail } from '@/api/sys/WFSchemeInfo';
  import { Audit } from '@/views/demo/workflow/task/process/page';
  import { Empty } from 'ant-design-vue';
  import { message } from 'ant-design-vue';
  import { useUserStore } from '@/store/modules/user.ts'
  import { SearchOutlined, RollbackOutlined } from '@ant-design/icons-vue';
  import { keyIssusesIPatchSourceOptions, auditMapStatusOptions } from '@/utils/global'
  import { dataProcessing } from '@/views/demo/tiankongdi/util.ts'
  import {showDrawBack, drawBackSpan} from '@/views/demo/util'

  const userStore = useUserStore()

  const simpleImage = Empty.PRESENTED_IMAGE_SIMPLE;
  const emits = defineEmits(['changeTask', 'changeShowParent','openLoading','closeLoading']);

  const flowWfDataStore = flowStore();

  const props = defineProps(['areaId', 'yearOptions', 'year','level','batch','patchSource','batchOptions']);
  const processId = ref('');
  const taskId = ref('');
  const isRead: any = ref(0);
  const type = ref('');
  const auditData = ref()
  const order = ref(0)
  const auditOpen = ref(false);
  const showDataId = ref()
  const handoffShow = ref(true)
  const openCollect = computed(() => {
  if(params.value.type === null) return false
  return true
})
  const nextShowDataId = ref()
  const prevShowDataId = ref()
  watch(() => showDataId.value, () => {
    let index = dataList.value.findIndex(item => item.Id == showDataId.value)
    if(index < dataList.value.length - 1){
      nextShowDataId.value =  dataList.value[index + 1].Id
    }else{
      nextShowDataId.value = 0
    }
    if(index > 0){
      prevShowDataId.value = dataList.value[index - 1].Id
    }else{
      prevShowDataId.value = 0
    }
  })
  const params = ref({
    page: 1,
    limit: 10,
    level: props.level,
    areaid: props.areaId,
    geomid: null,
    year: props.year,
    picihao: props.batch,
    tubanlaiyuan: props.patchSource,
    is_build_name: null,
    type: null,
    sort: null,
    order: null,
    nowStatus: [],
  });
  const markTypeOptions = ref([
    { label: '合法', value: '合法' },
    { label: '违法', value: '违法' },
    { label: '其他', value: '其他' },
  ]);

  const dataList = ref([]);
  const total = ref(0);
  const pageNumber = ref(1)
  const pageSizeNumber = ref(10)
  function query() {
    params.value.page = 1;
    getTaskList();
  }
  async function getTaskList() {
    console.log(params.value);
    let requestData = {...params.value}
    if(requestData.nowStatus.length > 0){
      requestData.nowStatus = requestData.nowStatus.join(',')
    }else{
      requestData.nowStatus = null
    }
    emits('openLoading')
    const data = await getLoadTaskDetailList(requestData);
    emits('closeLoading')
    dataList.value = data.items;
    total.value = data.total;
  }
  function changePagination(page, pageSize) {
    console.log(page, pageSize);
    params.value.page = page;
    params.value.limit = pageSize;
    getTaskList();
  }
  async function goAudit(record) {
    showDataId.value = record.Id
    let data = await getDetail({ code: record.processcode });
    let scheme = JSON.parse(data.scheme.content);
    let wfData = scheme.wfData;
    flowWfDataStore.setWfDataAll(wfData);
    auditOpen.value = true;
    processId.value = record.processid;
    taskId.value = record.taskeid;
    type.value = record.type;
    auditData.value = record;
  }
  async function locationFun(record) {
    emits('changeTask', record);
  }
  onMounted(() => {
    getTaskList();
  });
  const prevData = async () => {
    if(prevShowDataId.value === 0){
      if(params.value.page === 1 || total.value == 0){
        message.warning('已经是第一条数据了')
        return
      }
      pageNumber.value = pageNumber.value - 1
      params.value.page = params.value.page -1
      await getTaskList();
      // showDataIndex.value = dataList.value.length -1
      handoffShow.value = false
      let record = dataList.value[dataList.value.length -1]
      let data = await getDetail({ code: record.processcode });
      let scheme = JSON.parse(data.scheme.content);
      let wfData = scheme.wfData;
      flowWfDataStore.setWfDataAll(wfData);
      processId.value = record.processid;
      taskId.value = record.taskeid;
      type.value = record.type;
      handoffShow.value = true
      showDataId.value = dataList.value[dataList.value.length - 1].Id
      auditData.value = dataList.value[dataList.value.length - 1]
      return
    }else{
      showDataId.value = prevShowDataId.value
    }
    handoffShow.value = false
    let record = dataList.value.find(item => item.Id == showDataId.value)
    let data = await getDetail({ code: record.processcode });
    let scheme = JSON.parse(data.scheme.content);
    let wfData = scheme.wfData;
    flowWfDataStore.setWfDataAll(wfData);
    processId.value = record.processid;
    taskId.value = record.taskeid;
    type.value = record.type;
    handoffShow.value = true
    auditData.value = record
  }
  const nextData = async () => {
    if(nextShowDataId.value === 0){
      if(Math.ceil(total.value / pageSizeNumber.value) <= pageNumber.value || total.value == 0){
        message.warning('已经是最后一条数据了')
        return
      }
      pageNumber.value = pageNumber.value + 1
      params.value.page = params.value.page + 1
      await getTaskList();
      // showDataIndex.value = 0
      handoffShow.value = false
      let record = dataList.value[0]
      let data = await getDetail({ code: record.processcode });
      let scheme = JSON.parse(data.scheme.content);
      let wfData = scheme.wfData;
      flowWfDataStore.setWfDataAll(wfData);
      processId.value = record.processid;
      taskId.value = record.taskeid;
      type.value = record.type;
      handoffShow.value = true
      showDataId.value = dataList.value[0].Id
      auditData.value = dataList.value[0]
      return
    }else{
      showDataId.value = nextShowDataId.value
    }
    handoffShow.value = false
    let record = dataList.value.find(item => item.Id == showDataId.value)
    let data = await getDetail({ code: record.processcode });
    let scheme = JSON.parse(data.scheme.content);
    let wfData = scheme.wfData;
    flowWfDataStore.setWfDataAll(wfData);
    processId.value = record.processid;
    taskId.value = record.taskeid;
    type.value = record.type;
    handoffShow.value = true
    auditData.value = record
  }
  const cancelCollectItem = (item) => {
    deleteTaskCase(item.Fid).then(res => {
      message.success('取消收藏成功')
      getTaskList();
    })
  }
  const collectItem = (item) => {
    if(item.Fid){
      cancelCollectItem(item)
      return
    }
    let userInfo = userStore.getUserInfo
    let params = {
      taskId: item.taskeid,
      favoriteUserId: userInfo.id
    }
    addTaskFavorite(params).then(res => {
      message.success('收藏成功')
      getTaskList();
    })
  }
  const getCollectList = () => {
    if(params.value.type === null){
      params.value.type = 1
    }else if(params.value.type === 1){
      params.value.type = null
    }
    params.value.page = 1
    pageNumber.value = 1
    getTaskList();
  }
  const showSortMark = (key, sort) => {
    if(params.value.sort === key && sort === order.value){
      return true
    }
    return false
  }
  const dataListSort = (type) => {
    if(params.value.sort === null || params.value.sort === type){
      order.value = (order.value + 1) % 3
    }else{
      order.value = 1
    }
    // emits('infoDataListSort', type, order.value)
    switch(order.value){
      case 0:
        params.value.sort = null
        params.value.order = null
        break
      case 1:
        params.value.sort = type
        params.value.order = 'asc'
        break
      case 2:
        params.value.sort = type
        params.value.order = 'desc'
        break
    }
    getTaskList();
  }
  const closeMolder = () => {
    getTaskList();
  }
</script>
<style lang="less">
  .full-modal {
    .ant-modal {
      max-width: 100%;
      top: 0;
    }

    .ant-modal-content {
      height: calc(100vh);
    }

    .ant-modal-body {
      height: 85%;
    }
  }
</style>
<style lang="less" scoped>
  .handoff{
    width: 100%;
    display: flex;
    justify-content: flex-end;
    padding-right: 25px;
  }
  .map-list-content {
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .screen-div {
    padding: 22px 12px 19px 13px;
    display: flex;
    width: 590px;
    // margin-top: 10px;
    flex-wrap: wrap;
    // background: @component-background;
    .screen-item {
      // width: 33.3%;
      display: flex;
      // margin-bottom: 15px;
      font-family: Alibaba PuHuiTi;
      font-weight: 500;
      font-size: 17px;
      color: #000000;
      height: 39px;
      .screen-item-label {
        font-family: Alibaba PuHuiTi;
        font-weight: 500;
        font-size: 17px;
        color: #000000;
        line-height: 30px;
        display: flex;
        align-items: center;
        margin-right: 9px;
      }
      :deep(.ant-select-selector){
        display: flex;
        align-items: center;
        font-family: HarmonyOS Sans;
        font-weight: 500;
        font-size: 19px;
        color: #000000;
        line-height: 30px;
        height: 39px;
        box-shadow: 2px 3px 3px 1px rgba(13,13,13,0.05);
      }
      .item-input{
        font-family: Alibaba PuHuiTi;
        font-weight: 400;
        font-size: 19px;
        color: #000000;
        line-height: 30px;
        width:223px;
        box-shadow: 2px 3px 3px 1px rgba(13,13,13,0.05);
      }
      .item-button{
        font-family: Alibaba PuHuiTi;
        font-weight: 400;
        font-size: 19px;
        color: #FFFFFF;
        line-height: 30px;
        height:38px;
        width: 97px;
        background:#086DEC;
      }
    }
    .screen-button-div {
      display: flex;
      justify-content: space-between;
      width: 100%;
    }
  }
  .sift-div{
    background:#fff;
     height: 77px;
     padding:19px 13px;
     margin-left: 10px;
     margin-right: 10px;
     border-radius: 10px 10px 0px 0px;
     display: flex;
     justify-content: space-between;
     .layout-div{
      display:flex;
      align-items: center;
      .back-button{
        font-size:22px;
        cursor:pointer;
      }
      .interval-div{
        height:100%;
        width:1px;
        background:#EDEDED;
        margin-left: 10px;
        margin-right: 10px;
      }
      .sift-item{
        display: flex;
        align-items: center;
        margin-right:15px;
        cursor:pointer;
        user-select:none;
        .sift-label{
          font-family: Alibaba PuHuiTi;
          font-weight: 500;
          font-size: 19px;
          color: #000000;
        }
        .sift-icon{
          font-size: 9px;
          margin-left:5px;
          opacity: 0.53;
        }
      }
     }
     .collect-div{
      display:flex;
      align-items:center;
     }
  }
  .data-list-div {
    flex: 1;
    overflow: auto;
    padding: 10px;
    // margin-top: 10px;
    height: 60vh;
    scrollbar-width: none; /* firefox */
    -ms-overflow-style: none; /* IE 10+ */
    .data-list-item{
    background:#fff;
    padding:20px 10px 10px 10px;
    // border-radius:6px;
    margin-bottom:4px;
    position: relative;
    .back-box{
      position: absolute;
      right: 0;
      top:0;
      background: #EA1C06;
      padding: 3px 6px;
      color: #fff;
      font-size: 12px;
      border-radius: 0px 0px 0px 8px;
    }
    .data-list-layout-div{
      display:flex;
      justify-content: space-between;
      padding-bottom: 8px;
      border-bottom: 1px solid #E5E5E5;
      height: 45px;
      .data-list-title-div{
        display:flex;
        align-items: center;
        .map-mark{
          width:17px;
          height:17px;
          cursor:pointer;
        }
        .label-div{
          display: flex;
          align-items: baseline;
          margin-right: 12px
        }
        .item-label{
          font-family: Alibaba PuHuiTi;
          font-weight: 500;
          font-size: 23px;
          color: #000000;
          margin-left: 9px;
          margin-right:10px;
        }
        .item-sub-label{
          font-family: Alibaba PuHuiTi;
          font-weight: 500;
          font-size: 17px;
          color: #000000;
          display: flex;
        }
        .item-mark{
          font-family: Alibaba PuHuiTi;
          font-weight: 500;
          font-size: 13px;
          color: #696969;
          margin-left: 17px;
          margin-top: 4px;
        }
      }
      .button-div{
        display: flex;
        align-items: center;
        .button-item{
          height: 27px;
          width: 52px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-family: Alibaba PuHuiTi;
          font-weight: 500;
          font-size: 15px;
          color: #fff;
          background: linear-gradient(-56deg, #0060FF, #007EFF, #0061FF);
        }
      }
    }
    .data-list-info-div{
      display: flex;
      justify-content: space-between;
      margin-top: 10px;
      height: 40px;
      .info-layout-div{
        display:flex;
        align-items: center;
        .info-time{
          font-family: HarmonyOS Sans;
          font-weight: 500;
          font-size: 15px;
          color: #000000;
          margin-right: 5px;
        }
      }
      .info-item{
        display:flex;
        background: rgba(237, 237, 237, 0.55);
        align-items: center;
        width: 110px;
        border-radius: 7px;
        margin-right: 9px;
        height: 33px;
        justify-content: center;
        .info-label{
          font-family: Alibaba PuHuiTi;
          font-weight: 400;
          font-size: 13px;
          color: #959494;
          // width:45%;
          display: flex;
          justify-content: center;
          padding-right: 5px;
          white-space: nowrap;
        }
        .info-data{
          font-family: HarmonyOS Sans;
          font-weight: 500;
          font-size: 13px;
          color: #000000;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
    }
    .data-list-item:nth-last-child(1){
      border-bottom-left-radius: 6px;
      border-bottom-right-radius: 6px;
    }
  }
  .data-list-div::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }
  .pagination-div {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .no-data {
    padding: 20px 0;
  }
  .rollback {
    background: @component-background;
    padding: 10px;
  }
  .img-box{
    width: 25px;
    cursor: pointer;
    height: 23px;
  }
  .mr-r-20{
    margin-right: 20px;
  }
  .img-box.small{
    width: 18px;
    margin-right: 6px;
    margin-bottom: 2px;
  }
</style>
