<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>规格名：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入规格名"
                v-model="postData.SpecName"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>选择案场：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="CaseId" placeholder="请选择">
                <el-option
                  v-for="item in cases"
                  :key="item.CaseId"
                  :label="item.CaseName"
                  :value="item.CaseId">
                </el-option>
              </el-select>
            </div>
          </div>
        </li>
        <li style="text-align:center">
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
          <el-button type="danger" size="mini" @click="cancel">取消</el-button>
        </li>
      </ul>
    </form>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: '',
  data () {
    return {
      postData: {
        SpecId: '',
        SpecName: '',
        Status: '',
        OrgId: '',
        CaseId: ''
      }
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      OrgId: x => x.app.user.OrgId,
    }),
    CaseId: {
      get () {
        return this.postData.CaseId === '' ? this.defaultCaseId || '' : this.postData.CaseId
      },
      set (val) {
        this.postData.CaseId = val
      }
    }
  },
  components: {},
  created () {
    this.getInfo()
  },
  methods: {
    getInfo () { // 获取规格信息
      this.$ajax(this.$api.goodsManager.getGoodsSpecById.url, {
        method: this.$api.goodsManager.getGoodsSpecById.method,
        urlData: {id: this.$route.query.id}
      }).then(res => {
        this.postData = res
      })
    },
    submit () { // 提交数据
      if (this.postData.SpecName === '') {
        this.$message({
          type: 'error',
          message: '规格名称不能为空'
        })
        return false
      }
      this.$ajax(this.$api.goodsManager.editGoodsSpec.url, {
        method: this.$api.goodsManager.editGoodsSpec.method,
        data: this.postData,
        urlData: {id: this.postData.SpecId}
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.$router.push({ name: 'goodsSpecManager' })
      })
    },
    cancel () {
      this.$router.go(-1)
    }
  },
  mounted () { }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
