<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.TypeName"
                maxlength="6"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>种类英文：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入种类英文"
                v-model="postData.EnglishName"
                maxlength="10"
                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: {
        TypeId: '',
        TypeName: '',
        Status: '',
        OrgId: '',
        CaseId: '',
        EnglishName: '',
      },
    }
  },
  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.getGoodsTypeById.url, {
        method: this.$api.goodsManager.getGoodsTypeById.method,
        urlData: {id: this.$route.query.id}
      }).then(res => {
        // console.log(res)
        this.postData = res
      })
    },
    submit () { // 提交数据
      if (this.postData.TypeName === '') {
        this.$message({
          type: 'error',
          message: '种类名称不能为空'
        })
        return false
      }
      if (this.postData.EnglishName === '') {
        this.$message({
          type: 'error',
          message: '种类英文不能为空'
        })
        return false
      }
      this.$ajax(this.$api.goodsManager.editGoodsType.url, {
        method: this.$api.goodsManager.editGoodsType.method,
        data: this.postData,
        urlData: {id: this.postData.TypeId}
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.$router.push({ name: 'goodsTypeManager' })
      })
    },
    cancel () {
      this.$router.go(-1)
    }
  },
}
</script>

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