<template>
    <div class="app-container">
      <van-nav-bar
        left-arrow
        fixed
        placeholder
        @click-left="$router.back(-1)"
        @click-right="goAdd()"
      >
        <template #title>
          <p style="font-weight: bold;">重大事项</p>
        </template>
        <template #right>
          <van-icon name="add" size="18"/>
        </template>
      </van-nav-bar>

      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getList"
      >
        <van-swipe-cell v-for="(item,index) in applicationList" :key="index">
          <van-cell :title="item.eventName" :value="item.auditStatus" center :to="{name:'approvalDetail13', query: {id:item.id}}">
            <template #icon>
              <van-icon name="../../../../../static/images/onlineHome/icon_yn3.png" size="30" color="#539FFD" style="margin-right: 10px;" />
            </template>
            <template #label>
              <p>{{item.eventTime}}</p>
            </template>
          </van-cell>
          <template #right>
            <van-row>
              <van-col>
                <van-button v-if="item.auditStatus=='草稿'||item.auditStatus=='驳回'" square text="修改" type="info" :to="{name:'approvalModify13', query: {id:item.id}}" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="item.auditStatus=='草稿'||item.auditStatus=='驳回'" color="#FFA63E" square text="提交" type="info" @click="onSubmit(item.id)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="item.auditStatus=='草稿'||item.auditStatus=='驳回'" square text="删除" type="danger" @click="deleteList(item.id,index)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="item.auditStatus=='草稿'||item.auditStatus=='驳回'" square text="附件" style="background-color: rgb(98,173,102,0.2);color: #62AD66;" @click="openLoader(item.id,index)" class="delete-button" />
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
      <van-popup v-model="show" lock-scroll closeable position="top" :style="{ height: '30%' }" >
        <div style="padding: 0 13% 0 5%;">
          <van-divider>附件</van-divider>
          <van-uploader v-model="fileList" :after-read="afterRead" @delete="deleteFile1" multiple />
        </div>
      </van-popup>
    </div>
</template>

<script>
import { delMajorevent , customSubmitMajorevent , delTransfer , listMajorevent } from "@/api/onlineHome/bankAgriculture/paymentApproval";
import {attachmentList, commonAttach, systemAttachment} from "@/api/sunVillage_info/fixedAssets";
import request from "@/utils/request";
export default {
  name: "approvalList3",
  data() {
    return {
      applicationList:[],
      auditStatusOptions:[],
      loading: false,
      finished: false,
      queryParams:{
        pageNum:1,
        pageSize:10,
        transferType:"3",
      },
      show: false,
      fileList:[],
      uploadFiles1:[],
      projectId:'',
      projectIndex:''
    };
  },
  created() {
    this.getDicts("audit_status").then((response) => {
      this.auditStatusOptions = response.data;
    });
  },
  methods: {
    goAdd(){
      window.location = 'approvalAdd13';
    },
    getList(){
      setTimeout(() => {
        listMajorevent(this.queryParams).then(response => {
          console.log(response)
          for (var i = 0; i < response.rows.length; i++) {
            response.rows[i].auditStatus = this.selectDictLabel(this.auditStatusOptions, response.rows[i].auditStatus);
            this.applicationList.push(response.rows[i]);
          }
          if(this.applicationList.length >= response.total){
            this.finished = true;
            return;
          }else{
            this.loading = false;
            this.queryParams.pageNum += 1 ;
          }
        });
      }, 1000);
    },
    openLoader(id,index){
      this.show = true;
      this.projectId = id;
      this.projectIndex = index;
      this.fileList = [];
      let oData1= {
        tableId: id,
        tableName: "t_yinnong_transfer",
        bizPath: "transfer",
        fileType: '',
      }
      attachmentList(oData1).then(res => {
        res.rows.map(r => {
          let baseUrl = location.protocol+"//"+location.host+request.defaults.baseURL
          this.fileList.push({"url":baseUrl + r.fileUrl,"file":new File([],r.fileName,{}),"id":r.id})
        })
      })
    },
    deleteList(id,index){
      this.$dialog.confirm({
        message: '您确认删除草稿？',
      })
      .then(() => {
        // on confirm
        this.applicationList.splice(index,1)
        delMajorevent(id).then(res => {
          if(res.code = 200){
            this.$toast.success('删除成功');
          }
        });
      })
      .catch(() => {
        // on cancel
      });
    },
    onSubmit(id){
      this.$dialog.confirm({
        message: '您确认提交草稿？',
      })
      .then(() => {
        customSubmitMajorevent(id).then(res => {
          this.$toast.success('提交成功');
          setTimeout(function(){
            history.go(0)
          },2000)
        })
      })
      .catch(() => {
        // on cancel
      });
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      this.uploadFiles1.push(file.file);
      let params1 = new FormData();
      params1.append("tableId", this.projectId);
      params1.append("tableName", "t_yinnong_transfer");
      params1.append("bizPath", "transfer");
      params1.append("fileType", 'bookkeeping_type');
      params1.append("file", file.file);
      commonAttach(params1).then((r1) => {
        this.$notify({ type: 'success', message: '上传成功' });
      })
    },
    deleteFile1(file){
      console.log(file)
      systemAttachment(file.id).then(res => {
        this.$notify({ type: 'success', message: '删除成功' });
      })
    },
  },
}
</script>

<style scoped lang="scss">
  .app-container {
    padding: 0.2rem 3%;
  }
  /deep/.van-cell__title{
    flex: 0.7;
  }
  /deep/.van-cell__title span{
    font-family: Arial;
    font-size: 0.4rem;
    font-weight: normal;

  }
  /deep/.van-cell__label span{
    color: #1D6FE9;
    font-weight: bold;
    i{
      font-size: 0.2rem;
    }
  }
  /deep/.van-cell__value{
    flex: 0.3;
    color: #1D6FE9;
    font-weight: bold;
  }
  /deep/.van-swipe-cell{
    margin-bottom: 0.2rem;
    border-radius: 0.2rem;
    overflow: hidden;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
  }
  /deep/van-ellipsis{
    font-weight: bold;
  }
  .van-row{
    height: 100%;
  }
  .van-col{
    height: 100%;
  }
  .delete-button {
    height: 100%;
  }
</style>
