<template>
  <div >
    <basicDetails
        ref="details"
        titleText="企业设置"
        :oldFormPath="null"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        turnPageId=""
        idname="sys_enterpriseid"
        ownertable="sys_site"
        tags=""
        :tabs="['联系人','地址管理','财务信息']"
        @onEditSuccess="queryMainData($route.query.id)"
        :pageChange="true"
       >
      <template #d_head>
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules" ref="form" label-width="90px" label-position="right" size="mini">
            <el-col :span="5">
              <el-form-item label="企业名称:" prop="enterprisename">
                <el-input v-model="form.enterprisename" placeholder="企业名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="企业简称:" prop="abbreviation">
                <el-input v-model="form.abbreviation" placeholder="企业简称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="法人:" prop="contact">
                <el-input v-model="form.contact" placeholder="法人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="电话:" prop="phonenumber">
                <el-input v-model="form.phonenumber" placeholder="电话"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <div style="display:flex;align-items: center;">
                <p class="inline-16">企业操作页LOGO：</p>
                <uploadFile v-if="showImgUp" style="margin-top:10px" :folderid="folderid" accept=".JPG,.PNG,.jpg,.png" btntype="image" :bindData="{ownertable:'sys_site',ownerid:siteinfo.siteuid,usetype:'actionlogo'}" @onSuccess="uploadSuccess"></uploadFile>
                <div v-else class="image-panel">
                  <i class="el-icon-error close-btn" @click="deleteFileLink"></i>
                  <img v-show="img.usetype === 'actionlogo'" v-for="img in siteinfo.attinfos" :key="img.index" style="width:100%" :src="img.url" alt="">
                </div>
              </div>
              <small style="display:block;margin-top:20px" class="info">注：建议上传图片大小1024x1024，大小不超过2M，格式为JPG/PNG</small>
            </el-col>
          </el-form>
        </el-row>
      </template>
      <div slot="customOperation" >
        <el-button type="warning" size="small" @click="submit">保存</el-button>
      </div>
      <div slot="slot0" >
        <contacts :data="form"></contacts>
      </div>
      <div slot="slot1" >
        <addressManage :data="form"></addressManage>
      </div>
      <div slot="slot2" >
        <financialInfo :data="form"></financialInfo>
      </div>
    </basicDetails>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'

import uploadFile from '../../../../components/upload/hw_obs_upload.vue'
import contacts from '../components/contacts/index'
import addressManage from '../components/addressManage/index'
import financialInfo from '../components/financialInfo/index'
export default {
  components:{
    uploadFile,
    contacts,
    addressManage,
    financialInfo
  },
  computed:{
    ...mapGetters({
      siteinfo:'siteinfo'
    }),
    show () {
      console.log(this.siteinfo);
      
      if (this.siteinfo.attinfos.some(item=>item.usetype === 'actionlogo')) {
        return false
      } else {
        return true
      }
    }
  },
  data () {
    return {
      dialogEditVisible:false,
      form:{},
      rules:{
        enterprisename:[
          { required: true, message: '请输入企业名称', trigger: 'blur' },
        ],
        abbreviation:[
          { required: true, message: '请输入企业简称', trigger: 'blur' },
        ],
        contact:[
          { required: true, message: '请输入法人', trigger: 'blur' },
        ],
        phonenumber:[
          { required: true, message: '请输入电话', trigger: 'blur' },
        ],
        /*sitename:[
          { required: true, message: '请输入企业简称', trigger: 'blur' }
        ]*/
      },
      folderid:JSON.parse(sessionStorage.getItem('folderid')).appfolderid,
      mainData:{},
      mainAreaData:{},
      basicInfo:{},
      showImgUp:false,
    }
  },
  methods:{
    submit () {
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false;
        const res = await this.$api.requested({
          "id": 20220920084101,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$store.dispatch('changeDetailDrawer',false)
          this.$emit('onSuccess')
        })
      });
    },
    async queryMainData(id) {
      const res = await this.$api.requested({
        "id": 20221022165203,
        "content": {
        }
      })
      this.form = {
        "address": res.data.address,
        "abbreviation": res.data.abbreviation,
        "city": res.data.city,
        "changeby": res.data.changeby,
        "county": res.data.county,
        "phonenumber": res.data.phonenumber,
        "enterprisename": res.data.enterprisename,
        "sys_enterpriseid":res.data.sys_enterpriseid,
        "province": res.data.province,
        "contact": res.data.contact,
        "taxno": res.data.taxno
      }
      this.mainData = res.data
    },
    async deleteFileLink (item) {
      console.log(this.$route.query.basicInfo)
      const res = await this.$api.requested({
        "classname": "system.attachment.Attachment",
        "method": "deleteFileLink",
        "content": {
          "linksids":[this.siteinfo.attinfos[0].linksid]
        }
      })
      /*res.code === 1?this.$emit('onSuccess'):''*/
      res.code === 1?this.queryImg():''

    },
    uploadSuccess () {
      this.$emit('onSuccess')
      setTimeout(() => {
        this.form = Object.assign({},this.form,this.siteinfo)
        this.siteInfos()
      }, 100);
    },
    siteInfos () {
      this.$store.dispatch('querySiteInfo',{
        "classname": "webmanage.site.site",
        "method": "querySite",
        "content": {}
      })
      this.showImgUp = false
    },
    async queryImg(){
      const res = await this.$api.requested({
        "classname": "webmanage.site.site",
        "content": {},
        "method": "querySite",
      })
      console.log(res,"图片")
      if (res.data.attinfos.length === 0){
        this.showImgUp = true
      }else {
        this.showImgUp = false
      }
    }
  },
  mounted() {
   /* this.form = Object.assign({},this.form,this.siteinfo)*/
    console.log(this.$route.query.basicInfo,'输出')
    this.basicInfo = this.$route.query.basicInfo
    if (this.siteinfo.attinfos.length === 0){
      this.showImgUp = true
    }else {
      this.showImgUp = false
    }
    this.queryMainData()
  }
}

</script>
<style scoped>
.dialog-footer{
  margin-top: 50px;
  text-align: center;
}

</style>