<template>
  <div class="root">
    <el-form ref="ruleForm" :model="listQuery" :rules="rules" label-width="100px" class="add-ruleForm">
      <el-form-item label="商铺类型" prop="typeName">
        <el-input v-model="listQuery.typeName" style="width: 200px;"/>
      </el-form-item>
      <el-form-item label="索引" prop="sort">
        <el-input v-model="listQuery.sort" style="width: 200px;"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      listQuery: {
        typeName: '',
        sort: ''
      },
      rules: {
        typeName: [
          { required: true, message: '请输入商铺类型', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
  },
  methods: {
    submitForm(formName) { // 提交
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addBuilding()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) { // 重置
      // 重置为未注册
      this.isRegistered = true
      // 重置为非户主
      this.roleDisabled = false

      console.log(this.isRegistered)
      this.$refs[formName].resetFields()
    },
    addBuilding() {
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.$store.dispatch('shopType/AddShopType', this.listQuery).then((res) => {
        if (res.code === '0') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'shop-setting' })
          loading.close()
          return
        }
        this.$message.error(res.message)
        loading.close()
      }).catch(() => {
        loading.close()
        console.log('error AddBuilding')
      })
    }
  }
}
</script>

<style scoped>
.add-ruleForm{
  width: 800px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
}
</style>
