<template>
  <div class="regionBuy">
    <div class="el-card__header">
      <span>区域允许购买</span>
    </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="是否开启" class="is-required" >
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="msg.IsOpenMinPrice"
              active-text=""
              inactive-text="">
            </el-switch>
          </el-form-item>


          <el-form-item label="允许购买区域"  class="is-required" v-if="msg.IsOpenMinPrice==1">
            <div class="gez_list el-card"  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="msg.List.length==0">+新增规则</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="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: "regionBuy",

      data(){
          return{
            msg:{
              ID:0,
              IsOpenMinPrice:0,
              List:[],
            },
            dialogFormVisible:false,
            List:[],
            rules: {

            },
            isedit:false,
          }
      },
      components:{
        region_Choice,
      },
      methods:{
        getAreaBuyInfo(){
          this.apipost("/api/MallBase/GetAreaBuyInfo",{} , res => {
            if (res.data.resultCode == 1) {
                this.msg.IsOpenMinPrice  = res.data.data.IsOpenMinPrice;
                this.msg.List  = res.data.data.List;
                this.msg.ID  = res.data.data.ID;
            } else {
              this.Info(res.data.message);
            }
          })
        },
        edit(){
          this.dialogFormVisible=true;
          setTimeout(()=>{
            this.$refs.child.Receive(this.msg.List)
          },1000)
        } ,
        delete_b(){
          this.msg.List=[]

        },
        preserve(){
              let msg = this.msg;
              for(let i=0;i<msg.List.length;i++){
                delete msg.List[i].GrandpaID;
                delete msg.List[i].ParentID;
              }
              this.apipost("/api/MallBase/AddOrUpdateAreaBuy",msg , res => {
                if (res.data.resultCode == 1) {
                  this.Success(res.data.message);
                } else {
                  this.Info(res.data.message);
                }
              })

        },
        Addto(){
          this.dialogFormVisible=false
          this.msg.List =this.List;
        },
        change(data){
          this.List = data;

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

          this.dialogFormVisible=true;
          this.isedit=false;//设置为编辑状态
          setTimeout(()=>{
            this.$refs.child.Receive(this.List)
          },1000)
        },

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

<style scoped>
  .regionBuy .el-card__header{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    background: #fff;
  }
  .regionBuy .form-box {
    background: #fff;
    padding: 20px 50% 20px 0;
    margin-top: 10px;
  }
  .regionBuy .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;

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