<template>
  <div class="riseGiveRule">
    <div id="pane-first">
      <div class="form-box">
        <el-form :model="msg" style="padding:0 50px;" :rules="rules" ref="msg" label-width="150px">
          <el-form-item label="是否开启" prop="IsOpenMinPrice">
            <el-switch
              class="inputM_l"
              :active-value="1"
              :inactive-value="0"
              v-model="msg.IsOpenMinPrice"
              active-text=""
              inactive-text="">
            </el-switch>
          </el-form-item>

          <el-form-item label="全地区起送金额设置" prop="IsOpenAllRegionPrice" v-if="msg.IsOpenMinPrice==1">
            <el-switch
              class="inputM_l"
              :active-value="1"
              :inactive-value="0"
              v-model="msg.IsOpenAllRegionPrice"
              active-text=""
              inactive-text="">
            </el-switch>
          </el-form-item>

          <el-form-item label="全地区起送金额" prop="AllRegionPrice" class="is-required" v-if="msg.IsOpenMinPrice==1&&msg.IsOpenAllRegionPrice==1">
            <el-input v-model="msg.AllRegionPrice" class="inputM_l" min="0" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''">
              <template slot="append"></template>

            </el-input>

          </el-form-item>

          <el-form-item label="运费规则"  v-if="msg.IsOpenMinPrice==1">
            <div class="gez_list el-card" v-for="(item,index) in msg.Detail" :key="index">
              <div style="width: 500px;">
                <span>起送金额:{{item.FirstPrice}}元</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>

    </div>

    <el-dialog title="新增规则" :visible.sync="dialogFormVisible"  >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" >
        <el-form-item label="起送金额" prop="FirstPrice" class="is-required" label-width="100px">
          <el-input v-model="ruleForm.FirstPrice" class="inputM_l" min="0" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="地区选择" prop="List" label-width="100px">
          <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 './regionChoice'
    export default {
        name: "riseGiveRule",
      data(){
          return{
              msg:{
                ID:0,
                IsOpenMinPrice:0,
                IsOpenAllRegionPrice:1,
                AllRegionPrice:0,
                Detail:[],
              },
            ruleForm:{
              ID:0,
              FirstPrice:0,
              List:[],
            },
            dialogFormVisible:false,
            isedit:false,
            editindex:0,

            rules:{
              IsOpenMinPrice: [
                {required: true, message: "", trigger: "blur"}
              ],
              IsOpenAllRegionPrice: [
                {required: true, message: "", trigger: "blur"}
              ],
              AllRegionPrice: [
                {required: true, message: "请输入数字", trigger: "blur"}
              ],
              List:[
                { type: 'array', required: true, message: '请选择地区', trigger: 'change' }

              ]
            }
          }
      },
      components:{
        region_Choice,
      },
      methods:{
        GetMinPriceInfo(){//获取起送规则
          this.apipost("/api/MallBase/GetMinPriceInfo",{} , res => {
            if (res.data.resultCode == 1) {
                this.msg.ID = res.data.data.ID;
                this.msg.IsOpenMinPrice = res.data.data.IsOpenMinPrice;
                this.msg.IsOpenAllRegionPrice = res.data.data.IsOpenAllRegionPrice;
                this.msg.AllRegionPrice = res.data.data.AllRegionPrice;
                this.msg.Detail = res.data.data.Detail;
            } else {
              this.Info(res.data.message);
            }
          })
        },
        addOf(){
          this.ruleForm={
            FirstPrice:0,
            List:[],
          }
          this.dialogFormVisible=true;
          this.isedit=false;//设置为编辑状态

          let M_List= this.msg.Detail
          let cM_List=[]
          if(M_List.length>0){
            for(let i=0;i<M_List.length;i++){
              cM_List = cM_List.concat(M_List[i].List)
              if(i==M_List.length-1){
                setTimeout(()=>{
                  this.$refs.child.Receive([],cM_List,true) //帅选出哪里不可以再次选择
                },1000)
              }
            }
          }else {
            setTimeout(()=>{
              this.$refs.child.Receive([],[])
            },1000)
          }

        },
        edit(index,data){
          this.isedit=true;//设置为编辑状态
          this.editindex=index;//编辑的索引
          this.ruleForm = data
          this.dialogFormVisible=true;

          let M_List= this.msg.Detail
          let cM_List=[]
            for(let i=0;i<M_List.length;i++){
              if(i!=index){
                cM_List = cM_List.concat(M_List[i].List)
              }
              if(i==M_List.length-1){
                setTimeout(()=>{
                  this.$refs.child.Receive(this.ruleForm.List,cM_List,true) //帅选出哪里不可以再次选择
                },1000)
              }
            }

        },
        delete_b(index){
          this.msg.Detail.splice(index,1)

        },
        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/AddOrUpdateMinPrice",msg , res => {
                if (res.data.resultCode == 1) {
                  this.Success(res.data.message);
                } else {
                  this.Info(res.data.message);
                }
              })
            } else {
              return false;
            }
          });
        },
        Addto(formName){
          this.$refs[formName].validate((valid) => {
            if (valid) {
              this.dialogFormVisible=false
              let Detail = {
                'ID':0,
                'FirstPrice':parseInt(this.ruleForm.FirstPrice),
                '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;
            }
          });
        },
        change(data){

          this.ruleForm.List = data;

        },
      },
      mounted(){
        this.GetMinPriceInfo()
      }
    }
</script>

<style scoped>
  .riseGiveRule .form-box {
    background: #fff;
    padding: 20px 50% 20px 0;
    margin-top: 10px;
  }
  .riseGiveRule .quyu{
    background-color: #f4f4f5;
    color: #909399;
    padding: 10px;
    line-height: 30px;
    height: 30px;
    font-size: 12px;
    border-radius: 4px;
    white-space: nowrap;
    margin: 5px;
  }
  .riseGiveRule .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;

  }
  .riseGiveRule .imgstyle{
    width: 32px;
    height: 32px;
    margin: 0 5px;
  }
</style>
