<template>
  <div>
    <div>
      <div>
        <el-form ref="ref_form"
                 :inline="true"
                 :model="formData"
                 :rules="dataRule"
                 :disabled="formType === 'detail'"
                 class="form">

          <el-form-item label="分类名称"
                        prop="categoryName"
                        label-width="150px"
                        style="display: block">
            <el-input class="item_width_1"
                      maxlength="20"
                      show-word-limit
                      placeholder="请输入分类名称"
                      v-model="formData.categoryName">
            </el-input>
          </el-form-item>

          <el-form-item label="奖励积分"
                        style="display: block"
                        prop="awardPoint"
                        label-width="150px">
            <el-input-number class="item_width_1"
                             v-model="formData.awardPoint"
                             :min="0"
                             label="奖励积分"></el-input-number>
          </el-form-item>

        </el-form>
      </div>

    </div>
    <!-- <div class="div_btn">
      <el-button @click="handleCancle">取 消</el-button>
      <el-button v-if="formType != 'detail'"
                 type="primary"
                 :disabled="btnDisable"
                 @click="handleComfirm">确 定</el-button>
    </div> -->
     <div class="resi-btns">
        <el-button size="small"
                   @click="handleCancle">取 消</el-button>
        <el-button v-if="formType != 'detail'" type="primary"
                   size="small"
                   :disabled="btnDisable"
                   @click="handleComfirm">确 定</el-button>
      </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务
import { requestPost } from '@/js/dai/request'



let loading // 加载动画
export default {
  data () {
    return {
      formType: 'add', //表单操作类型 add新增，edit编辑，detail详情

      btnDisable: false,

      keyWords: '',
      formData: {
        customerId: '',
        categoryName: '',
        categoryId: '',
        awardPoint: null
      },

    }
  },
  components: {},
  mounted () {

  },

  methods: {

    async initForm (type, row) {
      this.startLoading()
      this.$refs.ref_form.resetFields();



      this.formType = type

      if (row) {
        this.formData = JSON.parse(JSON.stringify(row))

      } else {
        this.resetData()
      }
      this.formData.customerId = this.customerId
      this.endLoading()

    },

    async handleComfirm () {

      this.btnDisable = true
      setTimeout(() => {
        this.btnDisable = false
      }, 10000)
      this.$refs['ref_form'].validate((valid, messageObj) => {
        if (!valid) {
          app.util.validateRule(messageObj)
          this.btnDisable = false
        } else {
          this.addService()
        }

      })


    },
    async addService () {

      let url = '/heart/serviceitem/saveorupdate'
      // let url = "http://yapi.elinkservice.cn/mock/245/heart/serviceitem/saveorupdate"

      const { data, code, msg } = await requestPost(url, this.formData)

      if (code === 0) {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.resetData()
        this.$emit('dialogOk')
        this.btnDisable = false
      } else {
        this.btnDisable = false
        this.$message.error(msg)
      }

    },

    handleCancle () {
      this.resetData()
      this.$emit('dialogCancle')

    },



    resetData () {

      this.formData = {
        customerId: '',
        categoryName: '',
        categoryId: '',
        awardPoint: null
      }
    },
    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  computed: {
    dataRule () {
      return {

        categoryName: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' },
        ],
        awardPoint: [
          { required: true, message: '奖励积分不能为空', trigger: 'blur' }
        ],


      }
    },

  },
  props: {

    customerId: {
      type: String,
      default: ""
    }
  }
}
</script>
<style scoped>
.item_width_1 {
  width: 500px;
}
.item_width_2 {
  width: 400px;
}
.item_width_3 {
  margin-left: 10px;
  width: 200px;
}
.item_width_4 {
  width: 200px;
}

.div_btn {
  display: flex;
  justify-content: flex-end;
}
.el-tabs {
  margin: 0 20px;
}
.el-upload__tip {
  color: rgb(155, 155, 155);
  margin: 0;
}
.form {
  margin-top: 30px;
}
</style>

<style lang=scss>
.el-dialog__body {
  padding: 0 10px 20px !important;
}
</style>
