<template>
  <section class="dm-store">
    <el-select v-model="storeType" placeholder="请选择" class="w200 inline-block mr5">
      <el-option v-for="(v,i) in leftList" :key="i" :label="v.label" :value="v.value"></el-option>
    </el-select>
    <option-type v-if="storeType === 1" :uuid="newUuid"></option-type>
    <option-tags v-if="storeType === 2" :uuid="newUuid"></option-tags>
    <option-area v-if="storeType === 3" :uuid="newUuid"></option-area>
    <option-group v-if="storeType === 4" :uuid="newUuid"></option-group>
    <option-part v-if="storeType === 5" :uuid="newUuid"></option-part>
  </section>
</template>

<script>
import qs from 'qs';
import uuidv1 from 'uuid/v1';
import { baseUrl } from './config';
import optionType from './options/option-type';
import optionArea from './options/option-area';
import optionTags from './options/option-tags';
import optionGroup from './options/option-group';
import optionPart from './options/option-part';
const leftList = [{label:'所有门店',value:0},
        {label:'门店类型',value:1},
        {label:'门店标签',value:2},
        {label:'门店区域',value:3},
        {label:'门店分组',value:4},
        {label:'部分门店',value:5}
      ];
export default {
  name:'vue-gic-store-new',
  props:{
    uuid:{
      type:String,
      default:''
    },
    options: {
      type:Array,
      default() {
        return [0,1,2,3,4,5]
      }
    },
    isAdd:{
      type:Boolean,
      default:true
    }
  },
  components:{
    'option-type':optionType,
    'option-area':optionArea,
    'option-tags':optionTags,
    'option-group':optionGroup,
    'option-part':optionPart
  },
  watch:{
    storeType(val) {
      if (!val) {
        this.saveInit();
      }
    },
    uuid(val) {
      // 编辑或者详情
      if (val && !this.isAdd) {
        this.init();
      }
    }
  },
  data() {
    return {
      storeType:0, // 一级选择值
      leftList:[],
      newUuid:uuidv1().replace(/-/g,'')  // 生成uuid
    }
  },
  created() {
    if (this.options instanceof Array) {
      leftList.map( v => {
        if (this.options.indexOf(v.value) >= 0) {
          this.leftList.push(v);
        }
      })
    }
    this.init();
  },
  methods:{
    // 获取配置项
    init() {
      this.storeType = 0;
      this.newUuid = this.uuid || this.newUuid;
      if (!this.uuid) {
        this.$emit('update:uuid',this.newUuid);
        this.saveInit();
      } else {
        let params = {
          key:this.newUuid,
          currentPage:1,
          pageSize:20
        }
        this.axios.post(baseUrl + '/api-plug/get-store-widget-config?requestProject=gic-web',qs.stringify(params)).then(res => {
          this.comList = res.data.result.config.result || [];
          this.storeType = res.data.result.selectType || 0;
        }).catch(err => {
          console.log(err)
        })
      }
    },
    // 保存
    saveInit() {
      let params = {
        selectType:0,
        key:this.newUuid,
        isAll:0,
        value:'',
        removeItems:'',
      };
      this.axios.post(baseUrl + '/api-plug/save-store-widget?requestProject=gic-web',qs.stringify(params)).then(res => {

      }).catch(err => {
        console.log(err)
      })
    },
   // 判断门店是否保存
    isStoreSave() {
      return new Promise((resolve,reject) => {
        let params = {
          key:this.newUuid,
          selectType:this.storeType
        }
        this.axios.post(baseUrl + '/api-plug/is-empty?requestProject=gic-web',qs.stringify(params)).then(res => {
          console.log(res);
          if (res.data.errorCode === 0) {
            resolve(res.data.result);
          } else {
            reject(new Error('系统错误'));
          }
        }).catch(err => {
          reject(new Error('系统错误'));
        })
      })
    },

   
  }
}
</script>

<style lang="scss">
@import url('./index.css');
</style>
