<template>
  <div class="app-container">
    <el-row class="m-t-0">
      <el-col :span="24">
        <el-container>
          <el-header height="42px" class="l-h-42 p-l-10 p-r-10 border border-b-n">
            <div class="l">
              <i v-if="pageState === 'add'" class="el-icon-plus f-s-20"></i>
              <i v-else class="el-icon-edit f-s-20"></i>
              <font class="m-l-10 f-s-18 fb">{{ $route.meta.title }}</font>
            </div>
          </el-header>
          <el-main class="border p-20 m-b-20">
            <el-form ref="form" :model="form" label-width="120px" :label-position="$store.state.app.isWideScreen ? 'top' : 'right'">

              <!-- 商铺选择 -->
              <el-form-item v-if="pageState === 'add'" label="所属商铺">
                <SelectionShopId v-model="form.shop_id" />
              </el-form-item>

              <!-- 类别名称 -->
              <el-form-item label="机型名称">
                <el-input v-model="form.class_name" placeholder="例如：运输型无人机" />
              </el-form-item>

              <!-- 飞机类型 -->
              <el-form-item label="机型类型">
                <el-select v-model="form.category" placeholder="请选择类型">
                  <el-option label="四旋翼" value="四旋翼" />
                  <el-option label="固定翼" value="固定翼" />
                  <el-option label="垂直起降" value="垂直起降" />
                </el-select>
              </el-form-item>

              <!-- 轴距 -->
              <el-form-item label="轴距">
                <el-input v-model="form.wheelbase" placeholder="单位：厘米">
                  <template slot="append">cm</template>
                </el-input>
              </el-form-item>

              <!-- 最大载重 -->
              <el-form-item label="最大载重">
                <el-input v-model="form.weight_max" placeholder="单位：克">
                  <template slot="append">克</template>
                </el-input>
              </el-form-item>

              <!-- 描述 -->
              <el-form-item label="机型描述">
                <el-input v-model="form.describe" type="textarea" placeholder="描述备注，非必填" />
              </el-form-item>

              <!-- 提交按钮 -->
              <el-form-item v-if="pageState === 'add'">
                <el-button type="primary" icon="el-icon-plus" @click="addAir">创建</el-button>
                <el-button @click="setForm({ shop_id: form.shop_id })" class="iconfont icon-qingchu">
                  <font class="m-l-5">重填</font>
                </el-button>
              </el-form-item>
              <el-form-item v-else>
                <el-button type="primary" icon="el-icon-edit" @click="saveAir">更新</el-button>
              </el-form-item>
            </el-form>
          </el-main>
        </el-container>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import SelectionShopId from '@/components/SelectionShopId'

export default {
  name: 'RegisterAdd',
  data () {
    return {
      form: {
        shop_id: '',
        class_name: '',
        category: '四旋翼',
        wheelbase: '',
        weight_max: '',
        module: '',
        describe: '',
        onoff: true
      },
      airId: this.$route.params.id,
      pageState: 'add',
      plane: null
    }
  },
  components: {
    SelectionShopId
  },
  computed: {
    airList () {
      return this.$store.state.airList
    }
  },
  methods: {
    setForm (data) {
      this.form.shop_id = data.shop_id
      this.form.class_name = data.class_name
      this.form.category = data.category
      this.form.wheelbase = data.wheelbase
      this.form.weight_max = data.weight_max
      this.form.module = data.module
      this.form.describe = data.describe || ''
      this.form.onoff = data.onoff === '1' || data.onoff === true
      if (Object.keys(data).length === 0) {
        this.$message.warning('清空表单')
      }
    },
    initPage () {
      if (this.airId === undefined) {
        this.pageState = 'add'
      } else {
        this.pageState = 'edit'
        this.plane = this.airList.find((item) => item.id === this.airId)
        if (this.plane) {
          const data = {
            shop_id: this.plane.shop_id,
            class_name: this.plane.class_name,
            category: this.plane.category,
            wheelbase: this.plane.wheelbase,
            weight_max: this.plane.weight_max,
            module: this.plane.module,
            describe: this.plane.describe,
            onoff: this.plane.onoff
          }
          this.setForm(data)
        }
      }
    },
    async addAir () {
      const res = await this.$store.dispatch('fetchAddAir', this.form)
      if (res.data.status === 1) {
        this.$router.push('/register/index')
      }
    },
    async saveAir () {
      this.form.id = this.airId
      const res = await this.$store.dispatch('fetchSaveAir', this.form)
      if (res.data.status === 1) {
        this.$router.push('/register/index')
      }
    }
  },
  watch: {
    airList () {
      this.initPage()
    }
  },
  created () {
    if (this.airList.length > 0) {
      this.initPage()
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>
