<style scoped>
  .addFreightRule .form-box {
    background: #fff;
    padding: 20px 50% 20px 0;
    margin-top: 10px;
  }
  .addFreightRule .prompt{
    margin: -10px 20px 20px;
    background-color: #F4F4F5;
    padding: 10px 15px;
    color: #909399;
    display: inline-block;
    font-size: 13px;
  }
  .addFreightRule .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;

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

</style>
<template>
  <div class="addFreightRule">
      <div class="form-box">
          <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="90px">
            <el-form-item label="规则名称" prop="RulesName" class="is-required">
              <el-input v-model="msg.RulesName"/>
            </el-form-item>
            <el-form-item label="计费方式">
              <el-radio-group v-model="msg.ChargeMode" >
                <el-radio :label="1">按重计费</el-radio>
                <el-radio :label="2">按件计费</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="运费规则"  class="is-required" prop="Detail">
              <div class="gez_list el-card" v-for="(item,index) in msg.Detail" :key="index">
                <div style="width: 500px;">
                  <span>首重/件(克/个):{{item.First}} <span  class="xie">/</span> 首费(元):{{item.FirstPrice}} <span class="xie">/</span>续重/件(克/个):{{item.Second}}<span  class="xie">/</span>续费(元){{item.SecondPrice}}</span>
                  <div>区域：<span v-for="(item2,index2) in item.List" :key="index2" class="quyu">
                    {{item2.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(index,item)">
                  </el-tooltip>
                  <el-tooltip class="item" effect="dark" content="删除" placement="top">
                    <img src="../../assets/img/setup/del.png" alt="" class="imgstyle" @click="delete_b(index)">
                  </el-tooltip>
                </div>
              </div>
                <span style="color: #409EFF;font-size: 14px;cursor: pointer;" @click="addOf">+新增规则</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="ruleForm" :rules="rules" ref="ruleForm" >
          <el-row :gutter="0">
            <el-col :span="11" >
              <el-form-item label="首重(克)：" label-width="100px" style="padding: 0px 1rem;"  prop="First">
                <el-input v-model="ruleForm.First" step="1"  min="0" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''" />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="首费（元）" label-width="100px" style="padding: 0px 1rem;" prop="FirstPrice">
                <el-input v-model="ruleForm.FirstPrice" step="1"  min="0" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="0">
            <el-col :span="11">
              <el-form-item label="续重(克)：" label-width="100px" style="padding: 0px 1rem;" prop="Second">
                <el-input v-model="ruleForm.Second" step="1"  min="0" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''" />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="续费（元）" label-width="100px" style="padding: 0px 1rem;" prop="SecondPrice">
                <el-input v-model="ruleForm.SecondPrice" step="1"  min="0" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="地区选择" prop="List">
            <region_Choice @event1="change($event)" :List="ruleForm.List" 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('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
  import region_Choice from './view/regionChoice'
    export default {
        name: "addFreightRule",
        data(){
          return{
            msg:{
              ID:0,
              RulesName:'',
              ChargeMode:1,
              Detail:[],
            },
            ruleForm:{
              First:0,
              FirstPrice:0,
              Second:0,
              SecondPrice:0,
              List:[],
            },


            isedit:false,
            editindex:0,
            dialogFormVisible:false,
            rules: {
              RulesName: [
                {required: true, message: "请输入规则名称", trigger: "blur"}
              ],

              Detail:[
                { type: 'array', required: true, message: '请输入运费规则', trigger: 'change' }
              ],
              First:[
                {required: true, message: "请输入数字", trigger: "blur"}

              ],
              FirstPrice:[
                {required: true, message: "请输入数字", trigger: "blur"}
              ],
              Second:[
                {required: true, message: "请输入数字", trigger: "blur"}
              ],
              SecondPrice:[
                {required: true, message: "请输入数字", trigger: "blur"}
              ],
              List:[
                { type: 'array', required: true, message: '请选择地区', trigger: 'change' }

              ]
            },
          }
      },
      created(){
        if(this.$route.query.ID){
            this.getLogisticsRules(this.$route.query.ID)
        }
      },
      components:{
        region_Choice,
      },
      methods:{
        cancel(){
          this.$router.go(-1);//返回上一层
        },

        addRule(){

        },
        getLogisticsRules(ID){//编辑时获取详情数据
          this.apipost("/api/MallBase/GetLogisticsRules",{'ID':ID} , res => {
            if (res.data.resultCode == 1) {
                this.msg =res.data.data
            } else {
              this.Info(res.data.message);
            }
          })
        },
        change(data){


          this.ruleForm.List = data;

        },
        delete_b(index){
          this.msg.Detail.splice(index,1)
        },
        edit(index,data){
          this.isedit=true;//设置为编辑状态
          this.editindex=index;//编辑的索引
          this.ruleForm = data
          this.dialogFormVisible=true;
          setTimeout(()=>{
                  this.$refs.child.Receive(this.ruleForm.List)
          },1000)

        },
        addOf(){
          this.ruleForm={
            First:0,
            FirstPrice:0,
            Second:0,
            SecondPrice:0,
            List:[],
          }

          this.dialogFormVisible=true;
          this.isedit=false;//设置为编辑状态

          setTimeout(()=>{
            this.$refs.child.Receive([])
          },1000)
        },
        Addto(formName){
          this.$refs[formName].validate((valid) => {
            if (valid) {
              this.dialogFormVisible=false
              let Detail = {
                'ID':0,
                'First':parseInt(this.ruleForm.First),
                'FirstPrice':parseInt(this.ruleForm.FirstPrice),
                'Second':parseInt(this.ruleForm.Second),
                'SecondPrice':parseInt(this.ruleForm.SecondPrice),
                'List':this.ruleForm.List,
              }

              if( this.isedit==true){
                this.msg.Detail[this.editindex] =Detail;
                this.isedit=false //编辑之后复原
              }else {
                this.msg.Detail.push(Detail)
              }
            } else {
              return false;
            }
          });




        },
        preserve(formName){

          this.$refs[formName].validate((valid) => {
            if (valid) {
              let msg = this.msg;
              for(let i=0;i<msg.Detail.length;i++){
                for(let j = 0;j<msg.Detail[i].List.length;j++){
                  delete msg.Detail[i].List[j].GrandpaID;
                  delete msg.Detail[i].List[j].ParentID;
                }
              }
              this.apipost("/api/MallBase/AddOrUpdateLogisticsRules",msg , res => {
                if (res.data.resultCode == 1) {
                  this.Success(res.data.message);
                  this.$router.push({
                    name: 'ruleSetup',
                    query: {
                      activeName:'second',
                      blank: "y"
                    }
                  });
                } else {
                  this.Info(res.data.message);
                }
              })
            } else {
              return false;
            }
          });



        },
        isNumber(rule, value, callback) {

          if (value === '') {
            return callback();
          }  //这是用来判断如果不是必须输入的，则直接跳出
          const r = /^\+?[0-9][0-9]*$/; // 正整数
          // 如果判断不符合正则，则不是正整数不能提交
          if (!r.test(value)) {
            return callback(new Error('请输入数字'));
          } else {
            return callback();
          }

        },
      },


    }
</script>


