<template>
<div class="goods-add">
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>添加商品</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card>
    <el-alert
      title="添加商品信息"
      type="info" center show-icon :closable="false">
    </el-alert>

    <el-steps :active="active - 0" align-center finish-status="process">
      <el-step title="基本信息"></el-step>
      <el-step title="商品参数"></el-step>
      <el-step title="商品属性"></el-step>
      <el-step title="商品图片"></el-step>
      <el-step title="商品内容"></el-step>
      <el-step title="商品完成"></el-step>
    </el-steps>

    <el-form ref="form" :model="form" label-width="100px" :rules="rules" label-position="top">
      <el-tabs  tab-position="left"  v-model="active" :before-leave="tabSwitch" @tab-click="tabClick">
        <el-tab-pane label="基本信息" name="0">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="form.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input type="number" v-model="form.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="form.goods_weight" type="number"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input type="number" v-model="form.goods_number"></el-input>
          </el-form-item>

          <el-form-item label="商品分类" prop="goods_cat">

            <el-cascader
              :options="category" :props="selectedOptions"
              v-model="selectKey"
              @change="handleChange"></el-cascader>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="商品参数" name="1">

          <el-form-item :key="item.attr_id" v-for="item in many" :label="item.attr_name">
            <el-checkbox-group @change="change" v-model="item.attr_vals">
              <el-checkbox border v-for="(v,i) in item.attr_vals"  :key="i" :label="v"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane name="2" label="商品属性">
          <el-form-item :key="item.attr_id" v-for="item in only" :label="item.attr_name">
            <el-checkbox-group  v-model="item.attr_vals">
              <el-checkbox border v-for="(v,i) in item.attr_vals"  :key="i" :label="v"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane name="3"
                     label="商品图片">

          <el-upload
            :headers="headers"
            :action="uploadUrl"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="uploadSuccess"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-tab-pane>
        <el-tab-pane name="4" label="商品内容">商品内容</el-tab-pane>
      </el-tabs>
    </el-form>
  </el-card>

  <el-dialog
    title="预览"
    :visible.sync="picVisible"
    width="50%"
    >
    <img class="preview-pic" :src="imageUrl" alt="">
  </el-dialog>
</div>
</template>

<script>
export default {
  name: 'add',
  data () {
    return {
      active: '0',
      form: {
        goods_name: '',
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0,
        goods_cat: '',
        pics: []
      },
      selectedOptions: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover'
      },
      uploadUrl: 'http://127.0.0.1:8888/api/private/v1/upload',
      many: [],
      only: [],
      headers: {
        Authorization: window.sessionStorage.getItem('token')
      },
      selectKey: [],
      category: [],
      imageUrl: '',
      picVisible: false,
      rules: {
        goods_name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入数量', trigger: 'blur' }
        ],
        goods_cat: [
          {
            required: true, message: '请选择商品分类'
          }
        ]
      }
    }
  },
  methods: {
    uploadSuccess (response) {
      if (response.meta.status !== 200) {
        return this.$message.error(response.meta.msg)
      }
      this.form.pics.push({
        pic: response.data.tmp_path
      })
    },
    handleRemove (file) {
      const path = file.response.data.tmp_path
      this.form.pics.splice(this.form.pics.indexOf(path), 1)
    },
    handlePreview (file) {
      this.imageUrl = file.response.data.url
      this.picVisible = true
    },
    async threeCategories (type) {
      const { data: res } = await this.$http.get(`categories/${this.selectKey[2]}/attributes`, {
        params: {
          sel: type
        }
      })
      this[type] = res.data
      this[type].forEach(item => {
        item.attr_vals = item.attr_vals.length > 0 ? item.attr_vals.split(' ') : []
      })
    },
    tabClick () {
      if (this.active === '1') {
        this.threeCategories('many')
      } else if (this.active === '2') {
        this.threeCategories('only')
      }
    },
    tabSwitch (dist, source) {
      if (source === '0') {
        if (this.selectKey.length !== 3) {
          this.$message.error('请先选择分类')
          return false
        }
      }
    },
    handleChange () {
      if (this.selectKey.length !== 3) {
        this.selectKey = []
      } else {
        this.form.goods_cat = this.selectKey.join(',')
      }
    },
    async categories () {
      const { data: res } = await this.$http.get('categories')
      this.category = res.data
    }
  },
  mounted () {
    this.categories()
  }
}
</script>

<style lang="less" scoped>
.el-checkbox{
  margin: 0 5px 0 0;
}
  .preview-pic{
    width: 100%;
  }
</style>
