<template>
  <div class="home_wrapper">
    <div
      class="header_main"
       :style="`background-image:url(${require(showBtn?'@/assets/images/sunVillage_info/list_head.png':'@/assets/images/sunVillage_info/list_head_red.png')})`"
    >
      查看{{otherType=='1'?'合同':otherType=='2'?'党务':otherType=='3'?'政务':''}}公开
      <div class="return_btn" @click="onClickLeft"></div>
      <div class="add_btn"></div>
    </div>
    <van-form @submit="onSubmit">
      <div class="list_main">

        <van-field
          readonly
          clickable
          placeholder="请选择"
          v-model="form.openAt"
          input-align="right"
          label-width="auto"
          :border="false"
          :rules="[{ required: true , message:'请选择公开年月' }]"
        >
          <template #label>
            <img src="../../assets/images/sunVillage_info/add_tit_icon_01.png" width="18">
            <p style="margin-left: 5px;">公开年月</p>
          </template>
        </van-field>
        <van-popup v-model="showBuildTime" position="bottom">
          <van-datetime-picker
            v-model="openNy"
            type="date"
            title="选择年月日"
            @confirm="onConfirmOpenNy"
            @cancel="showBuildTime = false"
          />
        </van-popup>

        <van-field readonly v-model="form.otherName" placeholder="请输入公开标题" input-align="right" :border="false" >
          <template #label>
            <img src="../../assets/images/sunVillage_info/add_icon_1.png" width="18">
            <p style="margin-left: 5px;">公开标题</p>
          </template>
        </van-field>

        <van-field
          readonly
          clickable
          placeholder="请选择"
          v-model="form.otherType"
          input-align="right"
          label-width="auto"
          :border="false"
          :rules="[{ required: true , message:'请选择一张图类型' }]"
        >
          <template #label>
            <img src="../../assets/images/sunVillage_info/add_tit_icon_02.png" width="18">
            <p style="margin-left: 5px;">类型</p>
          </template>
        </van-field>
        <van-popup v-model="showPictureType" position="bottom">
          <van-picker
            show-toolbar
            :columns="pictureTypeOptions"
            value-key="dictLabel"
            @confirm="onConfirmPictureType"
            @cancel="showBodyType = false"
          />
        </van-popup>

        <van-field readonly input-align="right" :border="false" >
          <template #label>
            <img src="../../assets/images/sunVillage_info/add_tit_icon_03.png" width="18">
            <p style="margin-left: 5px;">公开图片</p>
          </template>
        </van-field>
        <!--           @delete="deleteFile1"-->
        <van-uploader v-model="openPic" :show-upload="false" :deletable="false"  style="margin-top: 10PX" />

        <div style="border-top: 1px solid #ededed;margin-top: 10PX;">
          <van-field readonly input-align="right" :border="false" >
            <template #label>
              <img src="../../assets/images/sunVillage_info/add_tit_icon_04.png" width="18">
              <p style="margin-left: 5px;">附件</p>
            </template>
          </van-field>

          <div>
            <div v-for="(item,index) in openFile" :key="index" style="display: flex;align-items: center;margin-top: 10px;">
              <img src="../../assets/images/sunVillage_info/WORD.png" width="30" v-if="item.type == 'word'"/>
              <img src="../../assets/images/sunVillage_info/ECEL.png" width="30" v-if="item.type == 'excel'" />
              <a :href="item.url" style="margin-left: 10px;color: #333333">{{item.name}}</a>
            </div>
          </div>
<!--          <van-uploader accept="*" :after-read="afterReadOpenFile" style="margin-top: 10PX">-->
<!--            <img src="../../assets/images/sunVillage_info/addFile.png" width="120" />-->
<!--          </van-uploader>-->
        </div>

        <van-field readonly input-align="right" :border="false" >
          <template #label>
            <img src="../../assets/images/sunVillage_info/add_tit_icon_05.png" width="18">
            <p style="margin-left: 5px;">公开内容</p>
          </template>
        </van-field>

        <div id="openContent"></div>

        <van-field readonly v-model="form.remark" placeholder="备注" input-align="right" :border="false" >
          <template #label>
            <img src="../../assets/images/sunVillage_info/add_tit_icon_05.png" width="18">
            <p style="margin-left: 5px;">备注</p>
          </template>
        </van-field>

      </div>
    </van-form>
  </div>
</template>

<script>
  import {commonUpload, otherPublicDetail, pictureEdit} from "@/api/sunVillage_info/fixedAssets";
  import Cookies from "js-cookie";
  import request from '@/utils/request'
  export default {
    name: "certificateList",
    data() {
      return {
        showBuildTime:false,
        showPictureType:false,
        form:{
          openAt:this.format(new Date(),'yyyy-MM-dd'),
          openPic:'',
          openFile:'',
        },
        openPic:[],
        pictureTypeOptions:[],
        fileList:[],
        fileList1:[],
        openNy:new Date(),
        type:'',
        openFile:[],
        openFileList:[],
        queryParams:{
          translate_dict:1,
          id:''
        },
        openFile2:[],
        openPic2:[],
        otherType:'',
        showBtn:true,
      };
    },
    created() {
      this.houseGetDicts("asset_status").then((response) => {
        this.assetStatusOptions = response.data;
      });
      this.houseGetDicts("asset_type").then((response) => {
        this.assetTypeOptions = response.data;
      });
      this.houseGetDicts("use_type").then((response) => {
        this.useTypeOptions = response.data;
      });
      this.houseGetDicts("picture_type").then((response) => {
        this.pictureTypeOptions = response.data;
      });
      this.type = this.$route.query.type;
      this.queryParams.bookId = Cookies.get('bookId');
      this.queryParams.deptId = Cookies.get('deptId');
      this.queryParams.id = this.$route.query.id;
      this.otherType = this.$route.query.type;
      this.showBtn = this.$route.query.showBtn=='false'?false:true;
      this.getDetail();
    },
    methods: {
      getDetail(id){
        otherPublicDetail(this.queryParams).then((res) => {
          var that = this ;
          res.data.pictureTypeText = this.selectDictLabel(this.pictureTypeOptions, res.data.pictureType);

          if (res.data.openFile!='' && res.data.openFile != null && res.data.openFile != undefined){
            this.openFile = res.data.openFile.split(',')
            this.openFile2 = res.data.openFile.split(',')
            this.openFile.map((rr,i)=>{
              let name = rr.substr(27,rr.length);
              let type = '';
              if (name.indexOf('.doc') > -1){
                type = 'word';
              }else if(name.indexOf('.xls') > -1){
                type = 'excel';
              }
              this.openFile[i] = {name:name,type:type,url:'/api'+rr}
            })
          }
          if (res.data.openPic!='' && res.data.openPic != null && res.data.openPic != undefined){
            this.openPic = res.data.openPic.split(',')
            this.openPic2 = res.data.openPic.split(',')
            this.openPic.map((rrr,i)=>{
              this.openPic[i] = {url:'/api'+rrr}
            })
          }
          document.getElementById('openContent').innerHTML = res.data.openContent
          that.form = res.data;
        })
      },
      onSubmit(){
        var that = this;
        that.form.openFile = that.openFile2.join(',')
        that.form.openPic = that.openPic2.join(',')
        this.form.deptName = Cookies.get('deptName');
        pictureEdit(that.form).then((r1) => {
          if (r1.code == 200){
            that.$notify({ type: 'success', message: '修改成功' });
            setTimeout(function(){
              history.back(-1);
            },2000)
          }
        })
      },
      onConfirmOpenNy(data){
        this.form.openAt = this.format(data,'yyyy-MM-dd');
        this.openNy = data;
        this.showBuildTime = false;
      },
      deleteFile1(file){
        console.log(file)
        // this.form.openPic.splice(index,1);
      },
      afterRead(file) {
        // 此时可以自行将文件上传至服务器
        // this.openPic.push(file.file);
        let params1 = new FormData();
        params1.append("file", file.file);
        commonUpload(params1).then((r1) => {
          this.openPic2.push(r1.fileName);
        })
      },
      afterReadOpenFile(file){
        let params1 = new FormData();
        params1.append("file", file.file);
        commonUpload(params1).then((r1) => {
          this.openFile2.push(r1.fileName);
        })
        let name = file.file.name;
        let type = '';
        if (name.indexOf('.doc') > -1){
          type = 'word';
        }else if(name.indexOf('.xls') > -1){
          type = 'excel';
        }
        this.openFileList.push({name:file.file.name,type:type})
        this.openFile.push(file.file);
      },
      onConfirmPictureType(data){
        this.form.pictureTypeText = data.dictLabel;
        this.form.pictureType = data.dictValue;
        this.showPictureType = false;
      },
    },
  }
</script>

<style scoped lang="scss">
  /deep/ .van-button--primary{
    background: url("../../assets/images/sunVillage_info/btn_bg.png") no-repeat;
    background-size: 100% 100%;
    border: none;
  }
  .home_wrapper{
    background: #e9e9e9;
    min-height: 100vh;
    width: 100vw;
    .header_main {
      height: 116px;
      background: url('../../assets/images/sunVillage_info/list_head.png') no-repeat;
      background-size: 100% 100%;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      font-size: 36px;
      line-height: 116px;
      text-align: center;
      color: #fff;
      position: relative;

      .return_btn {
        width: 24px;
        height: 43.2px;
        background: url('../../assets/images/sunVillage_info/list_icon_5.png') center center no-repeat;
        background-size: 20px 36px;
        position: absolute;
        left: 38px;
        top: 36px;
      }
    }
    .release_head{
      height: 90px;
      padding:0 23px;
      display: flex;
      align-items: center;
      font-size: 26px;
      color: #929292;
      .people{
        flex: 1;
        display: flex;
        align-items: center;
        .icon{
          width: 24px;
          height: 21px;
          background: url('../../assets/images/sunVillage_info/details_icon_1.png') no-repeat;
          background-size: 100% 100%;
          display: block;
          margin-right: 8px;
        }
      }
      .time{
        flex: 1;
        display: flex;
        align-items: center;
        justify-content:flex-end;
        .icon{
          width: 25px;
          height: 25px;
          background: url('../../assets/images/sunVillage_info/details_icon_2.png') no-repeat;
          background-size: 100% 100%;
          display: block;
          margin-right: 8px;
        }
      }
    }
    .release_conetnt{
      padding:0 22px;
      font-size: 32px;
      color: #252525;
      line-height: 44px;
      img{
        max-width: 100%;
        margin-bottom: 16px;
      }
      p{
        margin-bottom: 16px;
      }
    }
    .list_main{
      padding:25px;
      background: #ffffff;
      width: 94%;
      margin: 25px auto 0;
      border-radius: 15PX;
      box-shadow: 4px 6px 5px rgba(63, 68, 75, 0.1);
    }
    .titBox{
      display: flex;
      align-items: center;
    }
    .tit{
      font-size: 36px;
      font-weight: bold;
    }
    /deep/ .van-cell{
      padding-left: 0!important;
      padding-right: 0!important;
      padding-bottom: 0!important;
    }
    /deep/ .van-field__label{
      /*padding-left: 10PX;*/
      width: auto;
      display: flex;
      align-items: center;
    }
    /deep/ .van-cell--required::before{
      left: 85PX;
    }
    /deep/ .van-field__error-message{
      display: none;
    }
  }
</style>
