<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: gtz
 * @LastEditTime: 2021-03-15 10:48:36
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="clearAnnex"
  >
    <el-row :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="dataForm.name" :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.isStock')" prop="stock">
            <el-switch v-model="dataForm.stock" :active-value="1" :inactive-value="0" />
          </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-form>
    </el-row>
    <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>
  </el-dialog>
</template>

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

export default {
  components: {
    SingleImage
  },
  data() {
    return {
      visible: false,
      baseRouteName: null,
      dataForm: {
        id: 0,
        name: null,
        annexId: null,
        stock: 0
      },
      rules: {
        name: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.visual.areaName')]),
          trigger: 'blur'
        }],
        annexId: [{
          required: true,
          message: this.$i18nForm(['placeholder.upload', this.$t('module.basicData.visual.annex')]),
          trigger: 'blur'
        }],
        stock: [{
          required: true,
          message: this.$i18nForm(['placeholder.select', this.$t('module.basicData.visual.isStock')]),
          trigger: 'blur'
        }]
        // trajectory: [{
        //   required: true,
        //   message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.visual.baseRouteName')]),
        //   trigger: 'blur'
        // }]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          VisualDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    // 附件变更
    updateAnnex(fileInfo) {
      if (fileInfo) {
        this.dataForm.annexId = fileInfo.id
      } else {
        this.dataForm.annexId = ''
      }
    },
    changeTrajectory(val) {
      if (val || val === 0) {
        this.dataForm.trajectory = JSON.stringify([
          { lineName: this.baseRouteName, list: [] }
        ])
      } else {
        this.dataForm.trajectory = null
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'name': this.dataForm.name,
            'annexId': this.dataForm.annexId,
            'id': this.dataForm.id,
            'stock': this.dataForm.stock,
            'type': 1
          }
          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('refreshDataList')
                }
              })
            })
          } 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)
    }
  }
}
</script>
