<template>
  <div class="addFreeshipping">
    <div class="form-box" style="margin-top: 0">
      <div>
        <span style="color: rgb(64, 158, 255);cursor: pointer;margin-left: 20px" @click="cancel">包邮规则</span>
        <span style="margin: 0 5px;color: #C0C4CC;">/</span>
        <span>添加规则</span>
      </div>
    </div>
    <div id="pane-first">
      <div class="form-box">
        <el-form :model="msg" style="padding:0 50px;" :rules="rules" ref="msg" label-width="110px">
          <el-form-item label="规则名称" prop="RulesName" class="is-required">
            <el-input v-model="msg.RulesName" class="inputM_l"/>
          </el-form-item>
          <el-form-item label="包邮金额" prop="AllRegionPrice" class="is-required">
            <el-tooltip class="item" effect="dark" content="订单满XXX包邮" placement="top"
                        style="position: absolute;left: -10px;top:13px">
              <i class="el-tooltip el-icon-info"></i>
            </el-tooltip>
            <el-input v-model="msg.AllRegionPrice" class="inputM_l"/>
          </el-form-item>


          <el-form-item label="运费规则"  class="is-required" prop="List">
            <div class="gez_list el-card inputM_l"  v-if="msg.List.length>0">
              <div style="width: 500px;" >
                <div >区域：<span v-for="(item,index) in msg.List" :key="index" class="quyu">
                    {{item.RegionName}}</span>
                </div>              </div>
              <div style="width: 110px;display: flex;flex-direction: row;justify-content: space-around">
                <el-tooltip class="item" effect="dark" content="编辑" placement="top" >
                  <img src="../../assets/img/setup/edit.png" alt="" class="imgstyle" @click="edit()">
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="删除" placement="top">
                  <img src="../../assets/img/setup/del.png" alt="" class="imgstyle" @click="delete_b()">
                </el-tooltip>
              </div>
            </div>
            <span style="color: #409EFF;font-size: 14px;cursor: pointer;" @click="addOf" v-if="this.msg.List.length==0">+新增规则</span>
          </el-form-item>
        </el-form>

      </div>
      <el-button type="primary" style="margin: 12px 0 " @click="preserve('msg')">保存</el-button>
      <el-button style="margin: 12px 0 " @click="cancel">取消</el-button>

    </div>
    <el-dialog title="新增规则" :visible.sync="dialogFormVisible"  >
      <el-form :model="msg" :rules="rules" ref="msg">
        <el-form-item label="包邮地区" prop="List">
          <region_Choice @event1="change($event)"  ref="child"></region_Choice>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="Addto()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import region_Choice from './view/regionChoice'

  export default {
        name: "addFreeshipping",
      data(){
          return{
            msg:{
              ID:0,
              RulesName:'',
              AllRegionPrice:0,
              List:[],
            },
            List:[],
            isedit:false,
            editindex:0,
            dialogFormVisible:false,
            rules: {
              RulesName: [
                {required: true, message: "请填写包邮规则名称", trigger: "blur"}
              ],
              AllRegionPrice: [
                {required: true, message: "请输入数字", trigger: "blur"}
              ],
              List:[
                { type: 'array', required: true, message: '请选择包邮地区', trigger: 'change' }
              ],

            },
          }
      },
      components:{
        region_Choice,
      },
    created(){
      if(this.$route.query.ID){
        this.getLogisticsPinkage(this.$route.query.ID)
      }
    },
      methods:{
        getLogisticsPinkage(ID){
          this.apipost("/api/MallBase/GetLogisticsPinkage",{'ID':ID} , res => {
            if (res.data.resultCode == 1) {
              this.msg.ID =res.data.data.ID;
              this.msg.RulesName =res.data.data.RulesName;
              this.msg.AllRegionPrice =res.data.data.AllRegionPrice;
              this.msg.List =res.data.data.List;
            } else {
              this.Info(res.data.message);
            }
          })
        },
        cancel(){
          this.$router.go(-1);//返回上一层
        },

        change(data){
          this.List = data;

        },
        delete_b(){
          this.msg.List=[]
        },
        edit(){
          this.dialogFormVisible=true;
          setTimeout(()=>{
            this.$refs.child.Receive(this.msg.List)
          },1000)

        },
        addOf(){
         this.List=[]

          this.dialogFormVisible=true;
          this.isedit=false;//设置为编辑状态
          setTimeout(()=>{
            this.$refs.child.Receive(this.List)
          },1000)
        },
        preserve(formName){
          this.$refs[formName].validate((valid) => {
            if (valid) {
              let msg = this.msg;
              for(let i=0;i<msg.List.length;i++){
                  delete msg.List[i].GrandpaID;
                  delete msg.List[i].ParentID;
              }
              this.msg.AllRegionPrice = parseInt(this.msg.AllRegionPrice)
              this.apipost("/api/MallBase/AddOrUpdateLogisticsPinkage  ",msg , res => {
                if (res.data.resultCode == 1) {
                  this.Success(res.data.message);
                  this.$router.push({
                    name: 'ruleSetup',
                    query: {
                      activeName:'third',
                      blank: "y"
                    }
                  });
                } else {
                  this.Info(res.data.message);
                }
              })
            } else {
              return false;
            }
          });
        },
        Addto(){
          this.dialogFormVisible=false
          this.msg.List =this.List;
        },

      },

    }
</script>

<style scoped>
  .addFreeshipping .form-box {
    background: #fff;
    padding: 20px 50% 20px 0;
    margin-top: 10px;
  }
  .addFreeshipping .prompt{
    margin: -10px 20px 20px;
    background-color: #F4F4F5;
    padding: 10px 15px;
    color: #909399;
    display: inline-block;
    font-size: 13px;
  }
  .addFreeshipping .gez_list{
    width: 650px;
    margin-bottom: 12px;
    padding: 20px;
    border: 1px solid #EBEEF5;
    background-color: #FFF;
    color: #303133;
    display: flex;
    flex-direction: row;
    justify-content: space-between;

  }
  .addFreeshipping .imgstyle{
    width: 32px;
    height: 32px;
    margin: 0 5px;
  }
  .addFreeshipping .quyu{
    background-color: #f4f4f5;
    color: #909399;
    padding: 10px;
    line-height: 30px;
    height: 30px;
    font-size: 12px;
    border-radius: 4px;
    white-space: nowrap;
    margin: 5px;
  }

  .addFreeshipping .inputM_l{
    margin-left: 5px;
  }


</style>
