<template>
  <div class="app-container">
    <van-sticky style="position:relative;">
      <div class="bannerBg">
        <van-nav-bar
          style="background:transparent;border-bottom-width:0;height:2rem;margin-bottom:.5rem;"
          @click-left="goBack()"
        >
          <template #left>
            <van-icon name="arrow-left" size="18" color="#fff" />
          </template>
          <!-- <template #right>
          <van-icon name="map-marked" size="18" color="#fff" />
        </template>-->
          <template #title>
            <p style="color:#fff">入户调查</p>
          </template>
        </van-nav-bar>
      </div>
      <div style="width:95%;left:2.5%;position:absolute;background:#fff;border-radius:15px;box-shadow: 0px 3px 5px 0px rgba(0,0,0,0.16);padding: .3rem;top:1.2rem">
        <van-row>
          <van-col span="4" :offset="1" style="text-align:center;" @click="$router.push({path:'/homesteadSurvey/add'})">
            <van-icon :name="require('../../assets/images/housesteadSurvey/zjd'+(active==1?'A':'D')+'.png')" size="1rem" style="margin-top:0px;"/>
            <p :style="{color:(active==1?'rgb(34, 183, 242)':'#000')}">宅基地</p>
          </van-col>
          <van-col span="2">
            <div style="border-top: 2px dashed #b5b4b4;margin-top: 20px;"></div>
          </van-col>
          <van-col span="4"  style="text-align:center;" @click="$router.push({path:'/homesteadSurvey/add2'})" >
            <van-icon :name="require('../../assets/images/housesteadSurvey/nhxx'+(active==2?'A':'D')+'.png')" size="1rem" style="margin-top:0px;"/>
            <p :style="{color:(active==2?'rgb(34, 183, 242)':'#000')}">农户</p>
          </van-col>
          <van-col span="2">
            <div style="border-top: 2px dashed #b5b4b4;margin-top: 20px;"></div>
          </van-col>
          <van-col span="4" style="text-align:center;" @click="$router.push({path:'/homesteadSurvey/add3'})" >
            <van-icon :name="require('../../assets/images/housesteadSurvey/fwxx'+(active==3?'A':'D')+'.png')" size="1rem" style="margin-top:0px;"/>
            <p  :style="{color:(active==3?'rgb(34, 183, 242)':'#000')}">房屋</p>
          </van-col>
          <van-col span="2">
            <div style="border-top: 2px dashed #b5b4b4;margin-top: 20px;"></div>
          </van-col>
          <van-col span="4" style="text-align:center;" @click="$router.push({path:'/homesteadSurvey/add4'})">
            <van-icon :name="require('../../assets/images/housesteadSurvey/fsss'+(active==4?'A':'D')+'.png')" size="1rem" style="margin-top:0px;"/>
            <p :style="{color:(active==4?'rgb(34, 183, 242)':'#000')}">附属物</p>
          </van-col>
        </van-row>
      </div>
    </van-sticky>
    <div style="margin:30px auto 0;width: 95%;">
      <p class="title" style="position:relative;padding-left:10px;line-height:20px;font-size: 16px;margin-bottom: 5px;">附属设施</p>
    </div>
    <van-swipe-cell v-for="(item,index) in fssslist" :key="'fsss'+index" style="border-radius: 16px;box-shadow: 0px 3px 5px 0px rgba(0,0,0,0.16); width:95%;margin:15px auto 0;background:#fff;">
      <div style="padding:15px;">
        <p style="display: flex;align-items: center;">
          <img src="../../assets/images/housesteadSurvey/add03.png" alt="" style="margin-right: 5px;">
          <span style="line-height: 1;font-size: 16px;">{{item.fssslx}}</span>
        </p>
        <div style="display:flex;line-height:20px;margin-top: 15px;font-size: 14px;color: #999999;">
          <p style="flex:1;text-align:left;">建筑面积(㎡):</p>
          <p  style="flex:1;text-align:right;">{{item.jzmj}}</p>
        </div>
        <div style="display:flex;line-height:20px;margin-top: 5px;font-size: 14px;color: #999999;">
          <p style="flex:1;text-align:left;">建筑物情况:</p>
          <p style="flex:1;text-align:right;">{{item.jzwqk}}</p>
        </div>
        <div style="text-align:center;overflow:auto;display: flex;justify-content: center;margin-top: 10px;">
          <p style="width:49%;display: flex;align-items: center;justify-content:center;border: 1px solid transparent;background: #22B7F2;padding: 3px 0;border-radius: 50px;" @click="showPopupzrz(item)">
            <img src="../../assets/images/housesteadSurvey/add01.png" alt="">
            <span style="font-size: 14px;color: #fff;margin-left: 5px;" >查看附属设施信息</span>
          </p>
        </div>
      </div>
      <template #right>
        <van-button square text="删除" type="danger" class="delete-button" @click="deletefsss(item.id,index)"/>
      </template>
    </van-swipe-cell>
    <div style=" width:95%;margin:20px auto;display: flex;justify-content: center;">
      <div style="width:100%;text-align:center;overflow:auto;border-radius: 16px;box-shadow: 0px 3px 5px 0px rgba(0,0,0,0.16);padding: 15px 0px;background: #ffffff;color: #22B7F2;">
        <p style="font-size: 16px;" @click="showPopupAddzrz()">+ 添加附属设施信息</p>
      </div>
    </div>
    <div style="width:95%;margin:20px auto;text-align:center;overflow:auto;display: flex;justify-content: space-around;">
      <p style="width:32%;border: 1px solid transparent;background: #FA5353;padding: 5px 0;border-radius: 50px;">
        <span style="font-size: 14px;color: #fff;margin-left: 5px;"  @click="ycbj()">异常标记</span>
      </p>
      <p style="width:32%;border: 1px solid transparent;background: #22B7F2;padding: 5px 0;border-radius: 50px;">
        <span style="font-size: 14px;color: #fff;margin-left: 5px;"  @click="qmqr()">签名确认</span>
      </p>
      <p style="width:32%;border: 1px solid #22B7F2;background: #ffffff;padding: 5px 0;border-radius: 50px;">
        <span style="font-size: 14px;color: #22B7F2;margin-left: 5px;"  @click="dcsh()">调查审核</span>
      </p>
    </div>
    <van-popup v-model="dcshShow" position="bottom">
      <van-picker
        show-toolbar
        :columns="examineData"
        value-key="dictLabel"
        @confirm="onSurveyStatusChanged"
        @cancel="dcshShow = false"
      />
    </van-popup>
    <van-popup v-model="showesign" position="bottom" closeable
               close-icon="close">
      <van-row style="margin:0 10%">
        <p class="title" style="margin:20px 0;position:relative;padding-left:20px;font-size:0.5rem">电子签名</p>
      </van-row>
      <van-row style="margin: 0 5% 5% 5%;border:1px solid #BFBFBF;">
        <vue-esign
          ref="esign"
          class="mySign"
          :height="200"
          :isCrop="signature.isCrop"
          :lineWidth="signature.lineWidth"
          :lineColor="signature.lineColor"
          :bgColor.sync="signature.bgColor"
          v-if="!signature.resultImg"
        />
        <img :src="signature.resultImg" alt="" width="100%" height="100%;" v-if="signature.resultImg"/>
      </van-row>
      <van-row type="flex" justify="center" v-if="!signature.resultImg" style="margin-bottom:20px;">   <!--v-if="!formEnabled.acceptingForm.townFormEnabled"-->
        <van-col span="6" style="text-align:center;"><van-button type="primary" round size="small" @click="handleReset">清空画板</van-button></van-col>
        <van-col span="6" style="text-align:center;"><van-button type="primary" round size="small" @click="handleGenerate">保存提交</van-button></van-col>
      </van-row>
    </van-popup>
  </div>
</template>

<script>
import {listShyqr,addShyqr,updateShyqr,delShyqr,setSyqrInfo} from "@/api/homesteadSurvey/shyqr";
import {listZjdzd,getZjdzd,updateZjdzd,zjddcSign,addZjdzd,uploadFile,getQueryLand,submitOnly} from "@/api/homesteadSurvey/zjdzd";
import MapGisDrawing from "@/components/Map/MapGisDrawing";
import {listZrz,addZrz,updateZrz,getZrzZjdDmList,delZrz,updateZrzStatus} from "@/api/homesteadSurvey/zrz";
import {listNmfw,addNmfw,updateNmfw,delNmfw} from "@/api/homesteadSurvey/nmfw";
import {listSuyqr,addSuyqr,updateSuyqr} from "@/api/homesteadSurvey/suyqr";
import {listNhhncy,addNhhncy,updateNhhncy,delNhhncy} from "@/api/homesteadSurvey/nhhncy";
import {addNh, delNh, exportNh, getNh, listNh, updateNh,setNhInfo} from "@/api/homesteadSurvey/nh";
import {listFsss,addFsss,updateFsss,delFsss,getFsssZjdDmList,updateFsssStatus} from "@/api/homesteadSurvey/fsss";
import { listTown, getTown } from "@/api/homesteadSurvey/town";
import { listVillage} from "@/api/homesteadSurvey/village";
import { sysConfig} from "@/api/homesteadSurvey/index";
import { Notify, Dialog, Toast } from 'vant';
import axios from "axios";
export default {
  name: "homesteadAdd",
  components: { MapGisDrawing},
  data() {
    return {
      active:4,
      dcshShow:false,
      showesign:false,
      examineData:[{dictLabel:"审批通过",dictValue:"5"},{dictLabel:"审批驳回",dictValue:"4"}],
      zjdzdxx:{surveyStatus:"5"},
      fssslist:[],
      //电子签名
      signature: {
        lineWidth: 6, // 画笔的线条粗细
        lineColor: "#000000", // 画笔的颜色
        bgColor: "", // 画布的背景颜色
        resultImg: "", // 最终画布生成的base64图片
        isCrop: false, // 是否裁剪，在画布设定尺寸基础上裁掉四周空白部分
      },
    };
  },
  created(){
    this.zjdzdxx = JSON.parse(localStorage.getItem("zjdzdxxItem"));
    if(this.zjdzdxx.surveySign != null){
      this.signature.resultImg = "http://localhost/api/"+this.zjdzdxx.surveySign;
    }
    this.getList();
  },
  mounted(){

  },
  methods: {
    getList(){
      listFsss({zjddm:this.zjdzdxx.zjddm}).then(response => {
        this.fssslist = response.rows;
      });
    },
    onSurveyStatusChanged(val){
      this.$set(this.zjdzdxx, 'surveyStatus', val.dictValue);
      updateZjdzd(this.zjdzdxx).then(response => {
        let _this =this
        _this.$toast({
          icon: 'success', // 找到自己需要的图标
          message: '保存成功',
          duration:"1000",
          onClose:function(){
            _this.dcshShow = false;
            _this.$router.push({path:'/homesteadSurvey/add4',query: {zjddm: _this.zjdzdxx.zjddm}});
          }
        })
      });
    },
    // 清空画板
    handleReset() {
      this.resultImg = null
      if(this.$refs.esign){
        this.$refs.esign.reset();
      }
    },
    // 生成签字图
    handleGenerate() {
      console.info( this.$refs.esign
        .generate() );
      this.$refs.esign
        .generate() // 使用生成器调用把签字的图片转换成为base64图片格式
        .then((res) => {
          this.signature.resultImg = res;
          let wj = this.dataURLtoBlob(res);
          let param = new FormData()  // 创建form对象
          param.append('file', wj)  // 通过append向form对象添加数据
          param.append('date', this.format(new Date(), 'yyyy-MM-dd'))  // 通过append向form对象添加数据
          zjddcSign(param, this.zjdzdxx.zjddm).then(response => {
            this.$notify({type: 'success', message: '签字成功'});
            this.showesign = false;
            // location.reload();
            getZjdzd(this.zjdzdxx.id).then((response) => {
              localStorage.setItem("zjdzdxxItem",JSON.stringify(response.data))
            });

            this.getList();
          });

        })
        .catch((err) => {
          // 画布没有签字时会执行这里提示一下
          this.$toast.fail('请签名后再保存签字');
        });
    },
    dataURLtoBlob (dataurl, filename = 'file') {
      let arr = dataurl.split(',')
      let mime = arr[0].match(/:(.*?);/)[1]
      let suffix = mime.split('/')[1]
      let bstr = atob(arr[1])
      let n = bstr.length
      let u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new File([u8arr], `${filename}.${suffix}`, {
        type: mime
      })
    },
    goBack(){
      if(this.ztMap && this.enterMap ==1){
        if(this.form.id == null){
          this.backMap.backMapZjdAData.theGeom= "";
        }else{
          this.backMap.backMapZjdAData = this.form;
        }
        this.$cookies.set("search",this.backMap);
      } else {
        this.$cookies.set("search","");
      }
      this.$router.push({name: this.$router.back(-1)});
      // }
    },
    showPopupzrz(item){     this.$router.push({name:'fsssAdd',query:item});},
    showPopupAddzrz(){
      this.$router.push({name:'fsssAdd',query:{zjddm:this.zjdzdxx.zjddm}});
    },
    shownmfwlist(){},
    deletezrz(){},
    qmqr(){
      this.showesign = true;
    },
    ycbj(){
      this.$set(this.zjdzdxx, 'surveyStatus', "2");
      updateZjdzd(this.zjdzdxx).then(response => {
        let _this =this
        this.$toast({
          icon: 'success', // 找到自己需要的图标
          message: '保存成功',
          duration:"1000",
          onClose:function(){
            _this.$router.push({path:'/homesteadSurvey/add4',query: {zjddm: _this.zjdzdxx.zjddm}});
          }
        })
      });
    },
    dcsh(){
      this.dcshShow = true;
    },
  }
}
</script>

<style scoped>
>>> .bannerBg{
  width: 100%;
  color:#fff;
  padding:10px;
  background: linear-gradient(134deg,#7ac943 1%, #22b7f2);
}
>>> .van-hairline--bottom::after {
  border-bottom-width: 0;
}
>>> .title:before
{
  content:"";
  width: 6px;
  height: 20PX;
  background: #7ac943;
  border-radius: 3px;
  position:absolute;
  left:0;
  bottom:0;
}
>>> .delete-button {
  height: 100%;
}
>>> .van-swipe-cell__wrapper{
  margin-right:-3px;
}
>>> .label-class .van-collapse-item__title--expanded{
  font-weight: bold;
}
</style>
