<template>
  <div class="app-container">
    <el-form label-width="100px">
      <el-form-item label="推荐位置：">
        <el-select v-model="form.position" placeholder="请选择推荐位置">
          <el-option v-for="item in locationOptions" :key="item.value" :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称：">
        <el-input v-model="form.name" placeholder="请输入商品名称" />
      </el-form-item>
      <el-form-item label="商品简介：">
        <el-input v-model="form.description" type="textarea" rows="4" placeholder="请输入商品简介" />
      </el-form-item>
      <el-form-item label="商品价格：">
        <el-input v-model="form.price" placeholder="请输入商品价格" />
      </el-form-item>
      <el-form-item label="商品图片：">
        <Upload listType="picture-card" :url="form.pic" @upload="getUpload" />
      </el-form-item>
      <el-form-item label="跳转链接：">
        <el-input v-model="form.forwardUrl" placeholder="请输入跳转链接" />
      </el-form-item>
      <el-form-item label="有效期：">
        <el-date-picker v-model="date" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item label="权重：">
        <el-input-number v-model="form.sort" :min="0" :max="9" />
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { create, detail, edit } from '@/api/goods/list'
import { locMixin } from './mixin'
export default {
  mixins: [locMixin],
  data() {
    return {
      // 表单
      form: {
        sort: 0
      },
      // 有效期
      date: []
    }
  },
  watch: {
    date(val) {
      this.form.startTime = val[0]
      this.form.endTime = val[1]
    }
  },
  mounted() {
    if (this.$route.query.id) {
      detail({
        id: this.$route.query.id
      }).then(res => {
        if (res.code === 0) {
          this.form = res.data
          this.date = [res.data.startTime, res.data.endTime]
        }
      })
    }
  },
  methods: {
    // 上传图片
    getUpload(e) {
      this.form.pic = e.file
    },
    // 取消
    cancel() {
      this.$tab.closeOpenPage("/goods/goodsList");
    },
    // 提交
    getSubmit() {
      if (this.$route.query.id) {
        edit(this.form).then(res => {
          if (res.code === 0) {
            this.$message.success('修改成功！')
            this.cancel()
          }
        })
      } else {
        create(this.form).then(res => {
          if (res.code === 0) {
            this.$message.success('新增成功！')
            this.cancel()
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 500px;
}
</style>