<template>
  <div>
    <van-nav-bar title="修改积分分类" left-text="返回" @click-left="$route_back" left-arrow right-text="保存" @click-right="data_verify"></van-nav-bar>
    <div class="body_com has_header">
      <scroller>
        <van-cell-group>
          <van-field v-model="data.name" label="分类名称" name="名称" placeholder="请输入名称" v-validate="'required|min:2|max:10'" required />
        </van-cell-group>
        <van-cell-group>
          <CategorySelectorCell title="选择父级分类" v-model="rule_cate" :max="2" :multi="false"></CategorySelectorCell>
        </van-cell-group>
        <div style="padding:0.32rem;">
          <van-button size="large" @click="del" type="danger">删除</van-button>
        </div>
      </scroller>
    </div>
  </div>
</template>
<script>
  import request from '@/utils/request'
  import CategorySelectorCell from '@/components/common/CategorySelectorCell'
  export default {
    name: "rule_category_edit",
    components: {
      CategorySelectorCell
    },
    data(){
      return {
        data: {
          rule_id: 0,
          name: '',
          pid: 0
        },
        rule_cate: [],
      }
    },
    watch: {
      rule_cate(val){
        if(val.length > 0){
          this.data.pid = val[0].id
        }else{
          this.data.pid = 0
        }
      }
    },
    methods: {
      del() {
        let self = this
        this.$dialog.confirm({
          title: '删除分类吗',
          message: '确定要删除当前分类吗？'
        }).then(() => {
          self.$toast.loading({
            mask: true,
            message: '处理中...'
          })
          request('delete','/api/integral/rule',{rule_id: self.data.rule_id}).then((res) => {
            self.$toast.clear()
            if (res.data.code == 1) {
              self.$toast.success('删除成功')
              self.$keep_alive_update('delete_cate_info', self.data)
              setTimeout(() => {
                self.$route_back()
              }, 300)
            } else {
              self.$toast(res.data.msg)
            }
          })
        }).catch(() => {
          // on cancel
        })
      },
      data_verify(){
        let self = this
        self.$validator.validateAll().then(result => {
          if (!result) {
            self.$notify({ type: 'danger', message: self.$validator.errors.items[0].msg });
          }else{
            self.send()
          }
        })
      },
      send(){
        let self = this
        self.send_loading = true
        self.$toast.loading({
          message: '正在处理'
        })
        request('put','/api/integral/rule', self.data).then(res => {
          self.send_loading = false
          self.$toast.clear()
          if(res.data.code == 1){
            self.$toast('修改成功')
            setTimeout(() => {
              self.$keep_alive_update('update_rule_cate', self.data)
              self.$route_back()
            }, 500)
          }else{
            self.$toast(res.data.msg)
          }
        }).catch(e => {
          self.send_loading = false
          self.$toast.clear()
        })
      }
    },
    created(){
      if (this.$route.query.rule_id) {
        this.data.rule_id = this.$route.query.rule_id
        this.data.pid = this.$route.query.pid
        this.data.name = this.$route.query.rule_name
        this.$nextTick(() => {
          this.rule_cate = [{id: this.$route.query.pid*1, name: this.$route.query.pname}]
        })
      }
    }
  }
</script>
<style scoped>
  .body_com{height: calc(100% - 0.92rem); position: relative;}
</style>
