<template>
  <div style="display: inline;" v-loading="loading">

<el-button @click="FormReset"  type="primary" plain>增加规则</el-button>
    <el-dialog
      append-to-body
      :title="title"
      width="450px"
      height="150px"
      :visible.sync="dialogCreateVisible"
      :close-on-click-modal="false"
      >
        <el-form style="padding:0 30px;" :model="Formdata" ref="newTaskForm" :rules="FormRules" @submit.native.prevent>

          <el-form-item prop="rule_name">
            <el-input type="textarea" placeholder="规则内容，最多100字" v-model="Formdata.rule_name"></el-input>
          </el-form-item>
        </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="FormSend">保 存</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import qs from 'qs'

  export default {
    data() {
      return {
        loading: false,
        title:'新增规则',
        dialogCreateVisible:false,
        profile: this.$store.getters.user_info,
        FormApi: '/api/integral/pk/rules/create',   // pk 创建
        Formdata: {
          rule_name: '',
          pk_id:0
        },
        FormRules: {
          rule_name: [
            {required: true, message: '请输入规则', trigger: 'blur'},
            {min: 1, max: 100, message: '长度在 1 到 100字符', trigger: 'blur'}
          ]

        }
      }
    },
    methods: {

      FormReset: function (id) {
        this.title = '新增规则'
        this.Formdata = {
          rule_name: '',
          pk_id:this.$parent.$parent.$parent.pk_info.id
        }
        this.$nextTick(() => {
          const form = this.$refs['newTaskForm']
          if (typeof(id) == 'undefined') {
            form.resetFields()
            form.clearValidate()
          }
        })
        this.dialogCreateVisible = true;
        this.FormApi = '/api/integral/pk/rules/create'
      },
      
      //  修改规则
      loadInfo(data){
            this.title = '修改规则'
            this.FormApi = '/api/integral/pk/rules'
            this.Formdata.rule_name = data.row.name
            this.Formdata.pk_id = data.pk_id
            this.Formdata.rule_id= data.row.id
            console.log(this.Formdata)
            this.dialogCreateVisible = true
          
      },

      FormSend() {
        console.log(this.Formdata)
        const self = this
        const form = this.$refs['newTaskForm'];
        form.validate((valid) => {
          if (valid) {
            this.$http('post',self.FormApi,this.Formdata,
              // headers: {
              //   'Content-Type': 'application/x-www-form-urlencoded'
              // }
            ).then(function (response) {
              var message = "";
              var status = 0;
              if (response.status == 200) {
                var data = response.data;
                message = data.msg
                if (data.code == 1) {

                  self.dialogCreateVisible = false
                  self.$parent.$parent.$parent.loadPkRule()
                  self.$message({
                    message: message,
                    type: 'success'
                  });
                } else {
                  self.$message({
                    message: message,
                    type: 'error'
                  });
                }
              } else {
                message = "服务器太忙了";
              }

            }).catch(function (error) {
              console.log(error);
            })
          } else {
            return false
          }
        })
      }
    },
    created() {

    }
  }
</script>
