add.vue 7.72 KB
<template>
  <el-dialog :title='isEdit?"编辑":"新增"' class="manage-dialog" v-if="addDialogVisible" :visible.sync="addDialogVisible"
    :close-on-click-modal="false" @close="addDialogClose()">
    <el-form :model="addForm" status-icon :rules="rules" ref="addForm">
      <el-form-item v-if="!isAccount" label="商场" prop="mallId">
        <el-select v-model="addForm.mallId" class="mall-opt" filterable clearable :placeholder="$t('pholder.shopSelect')"
          @change="chooseMall">
          <el-option v-for="item in mallOpt" :label="item.name" :value="item.id" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="!isAccount" label="店铺" prop="zoneId">
        <el-select v-model="addForm.zoneId" class="mall-opt" filterable clearable :placeholder="$t('pholder.zoneSelect')">
          <el-option :label="$t('pholder.all')" value='0' />
          <el-option v-for="item in shopOpt" :label="item.name" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="配置">
        <div v-for="(item,index) in configList">
          <el-input-number class="numberInput" :controls='false' :min="0" v-model="item.value"></el-input-number>
          <span class="toolBar" @click="addConfig"><i class="el-icon-plus"></i></span>
          <span class="toolBar" v-if="configList.length>1" @click="delConfig(index)"><i
              class="el-icon-minus"></i></span>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="addDialogVisible = false" class="dialog-btn">{{$t('dialog.cancel')}}</el-button>
      <el-button type="primary" @click="addSubmit('addForm')" class="dialog-btn dialog-confirm-btn">
        {{$t('dialog.confirm')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        mallOpt: [],
        shopOpt: [],
        configList: [{
          value: ''
        }],
        addDialogVisible: false,
        addForm: {},
        rules: {
          name: [{
            required: true,
            message: "请输入业态名称",
            trigger: "blur"
          }]
        },
        isEdit: '',
        isAccount: ''
      };
    },
    methods: {
      getMallList() {
        this.mallOpt = [];
        this.$api.base.mall({
            accountId: this.$cookie.get("accountId"),
            status_arr: '1,3'
          }, null, true)
          .then(res => {
            let result = res.data.data;
            if (result.length > 0) {
              this.mallOpt = result;
            }
          })
      },
      chooseMall(val) {
        this.addForm.zoneId = ''
        this.getShopList(val)
      },
      getShopList(val){
        var tabelParams = {
          accountId: this.$cookie.get("accountId"),
          mallId: this.addForm.mallId,
          floorId: null,
          sortName: 'floor.name, zone.name',
          status:1
        };
        this.$api.base
          .zone(tabelParams, null, true)
          .then(data => {
            this.shopOpt = [];
            let result = data.data;
            this.shopOpt = result.data
          })
      },
      addConfig() {
        this.configList.push({
          value: ''
        })
      },
      delConfig(index) {
        if (this.configList.length == 1) {
          this.$message({
            type: 'warning',
            message: '至少存在一个停留时间段'
          })
          return
        }
        this.configList.splice(index, 1);
      },
      dialogInit(type, parmas, account) {
        this.isAccount = account;
        this.configList = [{
          value:''
        }]
        if (account) {
          // 集团
          if (type == 'add') {
            this.addForm = {
              accountId: this.$cookie.get("accountId"),
              mallId: '-1',
              zoneId: '-1',
            }
          } else {
            this.addForm = {
              accountId: this.$cookie.get("accountId"),
              mallId: '-1',
              zoneId: '-1',
              config:parmas.config
            }
            let arr = parmas.config.split(",");
            this.configList = []
            for (var i = 0; i < arr.length; i++) {
              this.configList.push({
                value:arr[i]
              })
            }
          }
        } else {
          // 广场店铺
           this.getMallList()
          if (type == 'add') {
            this.addForm = {
              accountId: this.$cookie.get("accountId"),
              mallId:'',
              zoneId:'',
            }
          } else {
            this.chooseMall(parmas.mallId)
            this.addForm = {
              accountId: this.$cookie.get("accountId"),
              mallId: parmas.mallId,
              zoneId: parmas.zoneId=='-1'?'':parmas.zoneId,
              config:parmas.config
            }
            let arr = parmas.config.split(",");
            this.configList = []
            for (var i = 0; i < arr.length; i++) {
              this.configList.push({
                value:arr[i]
              })
            }
          }
        }
        this.isEdit = type
        this.addDialogVisible = true;
      },
      addSubmit(formName) {
        let configArr = []
        for (var i = 0; i < this.configList.length; i++) {
          if(this.configList[i].value>0){
            if(i+1<this.configList.length){
              if(this.configList[i].value >= this.configList[i+1].value){
                this.$message({
                  showClose: true,
                  type: "warning",
                  message: '请填写正确的内容'
                });
                return
              }
            }
          }else{
            this.$message({
              showClose: true,
              type: "warning",
              message: '请填写正确的内容'
            });
            return
          }
        }
        this.$refs[formName].validate(valid => {
          for (var i = 0; i < this.configList.length; i++) {
            configArr.push(this.configList[i].value)
          }
          this.addForm.config = configArr.toString()
          if (valid) {
            if(!this.isAccount){
              if(!this.addForm.mallId){
                this.$message({
                  showClose: true,
                  type: "warning",
                  message: '请选择商场'
                });
                return
              }
              this.addForm.zoneId = this.addForm.zoneId?this.addForm.zoneId:'-1'
            }
            if (this.isEdit == 'add') {
              this.$api.management.addStayTime(this.addForm)
                .then(data => {
                  let result = data.data;
                  this.resultFun(result.code,result.msg)
                })
            } else {
              this.$api.management.editStayTime(this.addForm).then(data => {
                let result = data.data;
                this.resultFun(result.code,result.msg)
              })
            }
          } else {
            return false;
          }
        });
      },
      resultFun(code,msg){
        if (code == 200) {
          this.$message({
            showClose: true,
            type: "success",
            message: msg
          });
          this.addDialogVisible = false;
          if(this.isAccount){
            this.$parent.getAccountData()
          }else{
            this.$parent.getTableData()
          }
        } else {
          this.$message({
            showClose: true,
            type: "error",
            message: msg
          });
        }
      }
    }
  };
</script>

<style scoped>
  .numberInput {
    width: 200px;
  }

  .toolBar {
    cursor: pointer;
    font-size: 32px;
    color: #3bb8ff;
    display: inline-block;
    vertical-align: middle;
  }
</style>