<template>
  <div class="mall-section-container">
    <div class="mall-content-section">
      <div class="mall-content-title">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '' }">
            <a href="/report/#/memberSummary">首页</a>
          </el-breadcrumb-item>
          <el-breadcrumb-item>积分商城</el-breadcrumb-item>
          <el-breadcrumb-item>商品</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/ruleList' }">
            上架规则
          </el-breadcrumb-item>
          <el-breadcrumb-item>{{ breadcrumbTitle }}</el-breadcrumb-item>
        </el-breadcrumb>
        <h3>
          <span>{{ breadcrumbTitle }}</span>
        </h3>
      </div>
      <div class="section">
        <el-form
          ref="ruleForm"
          :rules="rules"
          :model="ruleForm"
          label-width="160px"
          size="small"
        >
          <el-form-item label="上架规则名称" prop="ruleTitle">
            <el-input
              maxlength="12"
              show-word-limit
              placeholder="请输入上架规则名称"
              v-model="ruleForm.ruleTitle"
              clearable
              @clear="ruleForm.ruleTitle=''"
              style="width:340px"
            />
          </el-form-item>
          <el-form-item label="上架规则名称备注" prop="ruleExplanation">
            <el-input
              maxlength="30"
              show-word-limit
              placeholder="请输入上架规则说明"
              v-model="ruleForm.ruleExplanation"
              style="width:340px"
            />
          </el-form-item>
          <el-form-item label="适用人群" prop="memberType">
            <el-radio v-model="ruleForm.memberType" :label="1">
              全部会员
            </el-radio>
            <el-radio v-model="ruleForm.memberType" :label="3">
              会员筛选
            </el-radio>
          </el-form-item>
          <el-form-item v-show="ruleForm.memberType === 3">
            <vue-gic-people
              v-bind="storeParams"
              :projectName="projectName"
              :triggerReset="true"
              :useId="useId"
              :hasSearchData="hasSearchData"
              :sceneValue="sceneValue"
              ref="peopleFilter"
              @findFilter="findFilter"
              @getBackData="getBackData"
              @editHide="editHide"
              @editShow="editShow"
              @hideBtn="hideBtn"
            />
            <div class="gic-people--button" v-show="toggleTag">
              <el-button size="small" type="primary" @click="getData">
                确 定
              </el-button>
              <el-button size="small" @click="cancelFilter">
                取 消
              </el-button>
            </div>
          </el-form-item>
          <div>
            <el-form-item style="margin-top:50px">
              <el-button type="primary" size="small" @click="submit()">
                确认
              </el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../../service/request';
export default {
  data () {
    const activityTimeVal = (rule, value, callback) => {
      callback(new Error('请填写'));
    };
    return {
      breadcrumbTitle: '新建上架规则',
      fixedWidth: document.documentElement.clientWidth - 200,
      ruleForm: {
        memberType: 1,
      },
      rules: {
        ruleTitle: [
          { required: true, message: '请填写', trigger: 'blur' },
        ],
        activityTime: [
          { required: true, message: '请填写', trigger: 'blur', validator: activityTimeVal },
        ],
      },
      projectName: 'integral-mall',
      useId: '',
      hasSearchData: '',
      sceneValue: 'member', // 场景值
      toggleTag: true,
      isEdit: this.$route.meta.type === 'edit',
      isAdd: this.$route.meta.type === 'add',
      isCopy: this.$route.meta.type === 'copy',
    };
  },
  computed: {
    storeParams () {
      return !this.isAdd ? { creatorId: this.ruleForm.creatorId } : {};
    },
  },
  mounted () {
    console.log(this.$route.params.id);
    if (!this.isAdd) {
      this.integralMallProRuleId = this.$route.params.id;
      this.getDatail(this.$route.params.id);
    }
    const obj = {
      edit: '编辑上架规则',
      add: '新建上架规则',
      copy: '复制上架规则',
    };
    this.breadcrumbTitle = obj[this.$route.meta.type];
  },
  methods: {
    /** 人群筛选器 */
    cancelFilter () { // 取消
      this.$refs.peopleFilter.cancelSet();
    },
    getData () {
      this.$refs.peopleFilter.confirmSet();
    },
    // 获取需要回显的数据, 供保存时候使用
    getBackData (val) {
      console.log(val);
      this.ruleForm.searchJson = val;
    },
    // 显示编辑,保存按钮隐藏,确认按钮显示 (子组件会调用)
    editShow () {
      console.log('hideBtn');
      this.toggleTag = true;
    },
    // 显示保存按钮,隐藏确认按钮显示  (子组件会调用)
    editHide () {
      console.log('hideBtn');
      console.log(this.useId);
      this.toggleTag = false;
    },
    // 子组件触发父组件事件,返回过滤条件数据
    findFilter (value) {
      console.log(value);
      this.ruleForm.filterJson = value;
    },

    // 隐藏保存按钮和确认按钮  (子组件会调用)
    // 传个参数给父组件 来查数据
    hideBtn () {
      console.log('hideBtn');
      this.toggleTag = false;
    },
    getDatail (integralMallProRuleId) {
      request.get('/api-integral-mall/get-rule', { params: { integralMallProRuleId } }).then(res => {
        if (res.data.result) {
          this.ruleForm = res.data.result;
          if (this.ruleForm.memberType == 3) {
            this.hasSearchData = this.ruleForm.filterJson;
            this.useId = this.ruleForm.searchId;
          }
        }
      });
    },
    submit () {
      if (this.ruleForm.memberType === 3) {
        if (this.toggleTag && !this.ruleForm.filterJson) {
          this.$message.error('请选中人群筛选器的数据');
          return false;
        }
      }
      this.$refs.ruleForm.validate(val => {
        if (val) {
          let data = {};
          if (this.isCopy) {
            const { ruleTitle, ruleExplanation, filterJson, memberType, searchJson } = this.ruleForm;
            data = { ruleTitle, ruleExplanation, memberType, filterJson, searchJson };
          } else {
            data = this.ruleForm;
          }
          if (data.memberType == 1) {
            delete data.filterJson;
            delete data.searchJson;
          }
          request.post('/api-integral-mall/add-update-rule', data).then(res => {
            this.$router.go(-1);
          });
        }
      });
    },
  },

};
</script>

<style scoped>
.gic-people--button{
  background: #f2f3f4;
    padding: 0 0 20px 124px;
}
</style>
