<template>
  <div class="edit-dialog-wrap">
    <el-dialog
      title="完成订单评价"
      :visible.sync="dialogEdit"
      width="600px"
      :before-close="handleEditClose"
      >
      <div class="edit-dialog-body" style="padding: 0px 20px;">
        <div class="edit-row">
          <el-radio v-model="acceptData.radio" label="0">固定奖励</el-radio>
          <span class="pad-l-10">获得</span><el-input v-model="acceptData.point" placeholder="" @keyup.native="($event,value) => numInput($event,value)" type="tel" :maxlength="inputLength"></el-input><span class="pad-l-10">积分/笔</span>
        </div>
        <div class="edit-row">
          <el-radio v-model="acceptData.radio" label="1">按金额设置</el-radio>
          <span class="pad-l-10">订单实付金额每满 </span>
          <el-input v-model="acceptData.money" placeholder="" @keyup.native="($event,value) => moneyInput($event,value)" type="tel" :maxlength="inputLength"></el-input>
          <span>元,得</span>
          <el-input v-model="acceptData.moneyPoint" placeholder="" @keyup.native="($event,value) => moneyPointInput($event,value)" type="tel" :maxlength="inputLength"></el-input>
          <span class="pad-l-10">积分</span>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditcancel">取 消</el-button>
        <el-button type="primary" @click="dialogEditConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // import { getRequest } from './api';
  import strLength from './../../common/js/strlen';
  import qs from 'qs';
  export default {
    name: 'edit-dialog',
    props: ['showEditDialog','projectName','transData'],
    data () {
      return {
        repProjectName: 'gic-web', // 项目名
        acceptData: {
          radio: '1',
          point: '',
          money: 10,
          moneyPoint: 10
        }, //接收的数据

        dialogEdit: false,
        inputLength: 6

      }
    },
    methods:{
      // 输入框输入
      numInput(e,r){
        var that = this
        console.log(e,r)
        // 过滤非数字
        that.inputNum = strLength.getCharVal(e.target.value.replace(/[^\d]/g,''),that.inputLength)

        that.acceptData.point = that.inputNum


      },
      // 输入框输入
      moneyInput(e,r){
        var that = this
        console.log(e,r)
        // 过滤非数字
        that.moneyInputNum = strLength.getCharVal(e.target.value.replace(/[^\d]/g,''),that.inputLength)
        that.acceptData.money = that.moneyInputNum

      },
      // 输入框输入
      moneyPointInput(e,r){
        var that = this
        console.log(e,r)
        // 过滤非数字
        that.moneyNum = strLength.getCharVal(e.target.value.replace(/[^\d]/g,''),that.inputLength)
        that.acceptData.moneyPoint = that.moneyNum

      },
      // 设置接收参数
      setNewData(flag) {
        var that = this;
        that.dialogEdit = flag;
        console.log("弹框是否显示: ",flag)
      },

      // 检查编辑弹窗
      checkEditPoint() {
        var that = this
        if (!that.acceptData.point) {
          that.$message.error({
            duration: 1000,
            message: "请输入积分,且不能小于等于0"
          })
          return false;
        }else{
          return true;
        }
      },

      checkEditMoney() {
        var that = this
        if (!that.acceptData.money || !that.acceptData.moneyPoint) {
          that.$message.error({
            duration: 1000,
            message: "请输入金额和积分,且不能小于等于0"
          })
          return false;
        }else{
          return true;
        }
      },

      // 确认
      dialogEditConfirm() {
        var that = this;
        console.log("确认",that.acceptData.radio)
        if (that.acceptData.radio == 0) {
          console.log(0)
          if(!that.checkEditPoint()){
            return;
          }else {
            console.log(1,that.acceptData)
            that.$emit('setEdit',that.acceptData)
          }

        }else {
          console.log(1)
          if(!that.checkEditMoney()){
            return;
          }else {
            console.log(2)
            that.$emit('setEdit',that.acceptData)
          }

        }

      },

      handleEditClose() {
        var that = this;
        that.dialogEditcancel();
      },

      //  取消
      dialogEditcancel() {
        var that = this;
        that.$confirm('确认关闭?', '', {
          confirmButtonText: '确 定',
          cancelButtonText: '取 消',
          type: ''
        }).then(() => {
          console.log("点击确认")
          that.radio = null;
          that.dialogEdit = false;
          that.$emit('setEdit')
        }).catch(() => {
           console.log("取消 取消关闭")
        });
      },

      // 获取卡券列表
      getCardList() {
        var that = this
        var para = {
          currentPage: that.cardCurrentPage,
          pageSize: that.cardPageSize,
          searchParam: that.searchText,
          requestProject: that.repProjectName,
          cardLimitType: '',
          cardTypes: '',
        }
        that.axios.post('/api-plug/get-coupon-list',{})
        .then((res) => {
          console.log(res,res.data,res.data.errorCode)
          var resData = res.data
          if (resData.errorCode == 0) {

            return;
          }
          that.$message.error({
            duration: 1000,
            message: error.message
          })

        })
        .catch(function (error) {
          console.log(error);
          // that.toLogin()
          that.$message.error({
            duration: 1000,
            message: error.message
          })
        });
      }
    },
    watch: {
      showEditDialog: function(newData,oldData){
        var that = this;
        // console.log("新数据:",newData,oldData)
        that.setNewData(newData)
      },
      projectName: function(newData,oldData){
        var that = this;
        // console.log("新数据:",newData,oldData)
        that.repProjectName = newData || 'gic-web';
      },
      transData: function(newData,oldData){
        var that = this;
        newData.radio = String(newData.radio)
        that.acceptData = newData;
      },
    },

    /* 接收数据 */
    mounted(){
      console.log("传递的参数对象:",this.showEditDialog)
      var that = this;

      // 项目名
      that.repProjectName = that.projectName || 'gic-web';
      that.transData.radio = String(that.transData.radio)
      that.acceptData = that.transData;

      that.setNewData(that.showEditDialog)
    },

  }
</script>

<style lang="scss" scoped>
  .edit-dialog-wrap {
    .pad-l-10 {
      padding-left: 10px;
    }
    .pad-r-10 {
      padding-right: 10px;
    }
    .el-input {
      width: 60px;
      padding: 0 10px;
    }

    .edit-row{
      line-height: 32px;

      span {
        display: inline-block;
        vertical-align: middle;
      }
      &+.edit-row {
        margin-top: 10px;
      }
    }
  }

</style>
