<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="iconfont el-icon-plus f-s-20"></i>
              <i v-else class="iconfont 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">
            <el-form ref="form" :model="form" label-width="120px">
              <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.name" placeholder="起名可以是中文" />
              </el-form-item>
              <el-form-item label="购买日期">
                <el-date-picker v-if="pageState == 'add' ? true : false" v-model="form.date" type="date"
                  placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="timestamp">
                </el-date-picker>
                <el-date-picker v-else v-model="form.date" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日"
                  value-format="timestamp" disabled>
                </el-date-picker>
              </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="form.onoff === true ? '启用' : '停用'">
                <el-switch v-model="form.onoff" />
              </el-form-item>
              <el-form-item label="飞机描述">
                <el-input v-model="form.desc" type="textarea" placeholder="描述备注，非必填" />
              </el-form-item>
              <el-form-item v-if="pageState == 'add' ? true : false">
                <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: '',
        name: '',
        date: '',
        onoff: true,
        weight_max: '',
        desc: ''
      },
      airId: this.$route.params.id, // get参数 获取飞机id 没有为添加页面
      pageState: 'add', // 页面状态
      plane: null
    }
  },
  components: {
    SelectionShopId
  },
  computed: {
    // 获取飞机列表
    airList () {
      return this.$store.state.airList
    }
  },
  methods: {
    // 设置表单
    setForm (data) {
      if (data.desc == null) {
        data.desc = ''
      }
      this.form.shop_id = data.shop_id
      this.form.name = data.name
      this.form.date = data.date
      this.form.onoff = data.onoff
      this.form.weight_max = data.weight_max
      this.form.desc = data.desc
      if (Object.keys(data).length === 0) {
        this.$message.warning('清空表单')
      }
    },
    // 初始化页面 添加or编辑
    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,
            name: this.plane.name,
            date: this.plane.apply_time + '000',
            onoff: this.plane.onoff === '1',
            weight_max: this.plane.weight_max,
            desc: this.plane.describe
          }
          this.setForm(data)
        }
      }
    },
    /**
     * @description: 创建新飞机
     */
    async addAir () {
      const res = await this.$store.dispatch('fetchAddAir', this.form)
      if (res.data.status === 1) {
        this.$router.push('/register/index')
      }
    },
    /**
     * @description: 更新飞机
     */
    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>
