<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: gtz
 * @LastEditTime: 2021-03-15 10:48:36
 * @Description:
-->
<template>
  <span v-if="injectData.stock">
    <el-button type="text" size="small" @click="emitClick">{{ 'btn.edit' | i18nFilter }}</el-button>
    <el-dialog
      :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
      :close-on-click-modal="false"
      :visible.sync="visible"
      top="10vh"
      @close="clearAnnex"
    >
      <div class="location-box">
        <el-row class="location-form" :gutter="15">
          <el-form
            ref="dataForm"
            :model="dataForm"
            :rules="rules"
            size="medium"
            label-width="160px"
            label-position="left"
          >
            <el-col :span="24">
              <el-form-item :label="$t('module.basicData.visual.areaName')" prop="name">
                <el-input v-model="areaName" disabled :placeholder="['placeholder.input', $t('module.basicData.visual.areaName')] | i18nFilterForm" clearable :style="{width: '100%'}" />
              </el-form-item>
            </el-col>
            <!-- <el-col v-if="!dataForm.id" :span="24">
              <el-form-item :label="$t('module.basicData.visual.baseRouteName')" prop="trajectory">
                <el-input v-model="baseRouteName" :placeholder="['placeholder.input', $t('module.basicData.visual.baseRouteName')] | i18nFilterForm" clearable :style="{width: '100%'}" @change="changeTrajectory" />
              </el-form-item>
            </el-col> -->
            <el-col :span="24">
              <el-form-item :label="$t('module.basicData.visual.annex')" prop="annexId">
                <Single-image v-if="visible" :file-id="dataForm.annexId" @input="updateAnnex" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item v-if="dataForm.id" :label="$t('module.basicData.visual.editLocation')">
                <el-button style="float: left" type="primary" @click="showPointModel">{{ $t('module.basicData.visual.editLocation') }}</el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
        <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
      </span>
      <Visual-location-info-point ref="locationpoint" :inject-data="dataForm" @emitData="emitData" />
    </el-dialog>
  </span>
</template>

<script>
import Vue from 'vue'
import SingleImage from '@/components/Upload/SingleImage'
import VisualLocationInfoPoint from './VisualLocationInfoPoint'
import { VisualUpdate, VisualAdd, VisualList } from '@/api/basicData/visual'

export default {
  components: {
    SingleImage,
    VisualLocationInfoPoint
  },
  props: {
    injectData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      visible: false,
      baseRouteName: null,
      areaName: null,
      dataForm: {
        id: null,
        annexId: null
      },
      rules: {
        annexId: [{
          required: true,
          message: this.$i18nForm(['placeholder.upload', this.$t('module.basicData.visual.annex')]),
          trigger: 'blur'
        }],
        trajectory: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.visual.baseRouteName')]),
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    emitClick() {
      this.areaName = this.injectData.name
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.injectData.id) {
          VisualList({
            parentId: this.injectData.id,
            current: 1,
            size: 10
          }).then(res => {
            if (res.data.records.length) {
              this.dataForm = res.data.records[0]
            }
          })
        }
      })
    },
    // 附件变更
    updateAnnex(fileInfo) {
      if (fileInfo) {
        this.dataForm.annexId = fileInfo.id
      } else {
        this.dataForm.annexId = ''
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'annexId': this.dataForm.annexId,
            'id': this.dataForm.id,
            'parentId': this.injectData.id,
            'type': 2
          }
          if (this.dataForm.id) {
            VisualUpdate(data).then(res => {
              this.$message({
                message: this.$t('baseTip.OperationSucc'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('emitData', {
                    type: ['refresh']
                  })
                }
              })
            })
          } else {
            VisualAdd(data).then(res => {
              this.$message({
                message: this.$t('baseTip.OperationSucc'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    },
    // 清除附件信息
    clearAnnex() {
      Vue.set(this.dataForm, 'annexId', null)
    },
    showPointModel() {
      this.$refs.locationpoint.emitClick()
    },
    emitData(v) {
      console.log(v)
      if (v.type.indexOf('refresh') >= 0) {
        this.emitClick()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.location-box {
  width: 100%;
  overflow: hidden;
  .location-form{
    width: calc(100% + 24px);
    max-height: calc(80vh - 181px);
    overflow-y: scroll;
  }
}
</style>
