<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="等级名称" prop="gradeName">
        <el-input v-model="ruleForm.gradeName" placeholder="请输入等级名称"></el-input>
      </el-form-item>
      <el-form-item label="升级条件" prop="upgradeCondition">
        <el-input v-model="ruleForm.upgradeCondition" placeholder="请输入升级条件"></el-input>
      </el-form-item>
      <el-form-item label="会员折扣" prop="memberDiscount">
        <el-input v-model="ruleForm.memberDiscount" placeholder="请输入升级条件"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProviceCity, getDictionary } from '@/api/commonAPI'
import { getOrder, addOrder, editOrder } from '@/api/order/orderSearch'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    callid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      props: { // 自定义省市下拉数据的key值
        value: 'code',
        label: 'name',
        children: 'entityJson'
      },
      sourceOptions: [], // 工单来源下拉数据
      ruleForm: {
        unique_id: '', //		否	string	callid
        ordercode: '',
        gradeName: '',// 否  string  等级名称
        upgradeCondition: '',// 否  string  升级条件
        memberDiscount: '',//  否   string   会员折扣
      },
      rules: {
        gradeName: [{
          required: true,
          trigger: 'blur',
          message: '请输入等级名称',
        }]
      },
      loading: false
    }
  },
  created() {
    if (this.callid) {
      this.ruleForm.unique_id = this.callid
    }
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.ruleForm.ordercode = this.rowid
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.loading = true
          // 添加
          if (!this.rowid) {
            addOrder(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                if (this.callid) {
                  this.$parent.getOrderRecord() // 重新加载来电弹屏工单记录
                } else {
                  this.$parent.getList() // 重新加载父级数据
                }
                this.$message.success('恭喜你，添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editOrder(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    // 详情
    getDetail(rid) {
      getOrder(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data.model
        }
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form .form_select{
	width: 100%;
}
</style>