<template>
  <div class="right-wrap">
    <topNav :navpath="navpath"></topNav>
    <div class="right-content">
      <div class="right-box">
        <div class="attention-wrap">
          <!-- 积分制度 -->
          <div class="point-cell point-form">
            <point-form :pointInfo="pointInfo" :projectName="projectName"></point-form>
          </div>
          <!-- 特殊积分获取策略 -->
          <div class="point-cell point-table" v-if="false">
            <div class="flex table-top">
              <div class="flex-1 flex-l">
                <p class="flex-title">特殊积分获取策略</p>
                <div class="flex-lable">设置除通用积分获取规则以外的特殊策略</div>
              </div>
              <div class="flex-1 flex-r"><el-button type="primary" @click="handleAdd('point')">新增策略</el-button></div>
            </div>
            <!-- table  border-->
            <div class="table-contain">
              <el-table
                :data="achieveTableData"
                style="width: 100%">
                <el-table-column
                  prop="strategyName"
                  label="策略名称">
                </el-table-column>
                <el-table-column
                  prop=""
                  label="线下渠道">
                  <template slot-scope="scope">
                    <span v-for="item in scope.row.channelList" v-if="item.channelType==3">
                        <span v-if="item.openStatus==0">关闭</span>
                        <span v-else>
                          <span v-if="item.channelRelationProType==1&&item.channelRelationStoreType==1">通用</span>
                          <span v-if="item.channelRelationProType!=1||item.channelRelationStoreType!=1">自定义配置</span>
                        </span>

                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop=""
                  label="微信商城">
                  <template slot-scope="scope">
                    <span v-for="item in scope.row.channelList" v-if="item.channelType==1">
                        <span v-if="item.openStatus==0">关闭</span>
                        <span v-else>
                          <span v-if="item.channelRelationProType==1">通用</span>
                          <span v-if="item.channelRelationProType!=1">自定义配置</span>
                        </span>

                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop=""
                  label="天猫商城">
                  <template slot-scope="scope">
                    <span v-for="item in scope.row.channelList" v-if="item.channelType==8">
                      <span v-if="item.openStatus==0">关闭</span>
                      <span v-else>
                        <span v-if="item.channelRelationProType==1">通用</span>
                        <span v-if="item.channelRelationProType!=1">自定义配置</span>
                      </span>
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop=""
                  label="适用对象">
                  <template slot-scope="scope">
                    {{scope.row.useCrowd == 1? '所有会员': '自定义人群'}}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="getRule"
                  label="获取规则">
                   <template slot-scope="scope">
                    <span v-if="scope.row.integralGetStatus == 0">无积分</span>
                    <span v-if="scope.row.integralGetStatus != 0">
                      {{scope.row.integralGet.fee+':'+scope.row.integralGet.integral}}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  label="操作">
                  <template slot-scope="scope">
                    <!-- <el-button type="text" size="small">编辑</el-button> -->
                     <router-link :to="{path:'/editPointSystem',query:{integralStrategyId: scope.row.integralStrategyId}}" class="edit-btn el-button--text">编辑</router-link>
                    <el-button @click="handleDelClick(scope.$index,scope.row,achieveTableData)" type="text" size="small">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
          <!-- 特殊积分抵现策略 -->
          <div class="point-cell point-table" v-if="false">
            <div class="flex table-top">
              <div class="flex-1 flex-l">
                <p class="flex-title">特殊积分抵现策略</p>
                <div class="flex-lable">设置消费时使用积分抵现的规则（仅用于微信商城结算）</div>
              </div>
              <div class="flex-1 flex-r"><el-button type="primary" @click= "handleAdd('integ')">新增策略</el-button></div>
            </div>
            <!-- table -->
            <div class="table-contain">
              <el-table
                :data="exchangeTableData"
                style="width: 100%">
                <el-table-column
                  prop="strategyName"
                  label="策略名称">
                </el-table-column>
                <el-table-column
                  prop="useCrowd"
                  label="适用对象">
                  <template slot-scope="scope">
                    {{scope.row.useCrowd == 1? '所有会员': '自定义人群'}}
                  </template>
                </el-table-column>
                <el-table-column
                  prop=""
                  label="适用商品">
                  <template slot-scope="scope">
                    <span v-for="item in scope.row.channelList" >
                      {{item.channelRelationProType == 1&&item.integralUseDiscountControl==1? "所有商品" : "自定义配置" }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop=""
                  label="折扣控制">
                  <template slot-scope="scope">
                    <span v-for="item in scope.row.channelList" >
                      {{item.integralUseDiscountControl==1? "不限制" : (item.integralUseDiscountControlLower+':'+item.integralUseDiscountControlUpper) }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="getRule"
                  label="抵现规则">
                  <template slot-scope="scope">
                    <span >
                      {{scope.row.integralUseStatus==0? "无积分" : (scope.row.integralUse.integral+':'+scope.row.integralUse.fee)}}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop=""
                  label="比例限制">
                  <template slot-scope="scope">
                    <span v-for="item in scope.row.channelList" >
                      {{item.useProportionalRestriction}}%
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  label="操作">
                  <template slot-scope="scope">
                    <!-- <el-button type="text" size="small">编辑</el-button> -->
                    <router-link :to="{path:'/editIntegralSytem',query:{integralStrategyId: scope.row.integralStrategyId}}" class="edit-btn el-button--text">编辑</router-link>
                    <el-button @click="handleDelClick(scope.$index,scope.row,exchangeTableData)" type="text" size="small">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
    </div>
    <vue-gic-footer></vue-gic-footer>
  </div>
</template>
<script>
import topNav from 'components/nav/nav'
import pointForm from 'components/memberShip/point-form'

import showMsg from '@/common/js/showmsg';
import errMsg  from '@/common/js/error';
import { getRequest, postRequest, postForm } from '@/api/api';

export default {
  name: "pointSystem",
  data() {
    return {
      projectName:  'gic-clique', // 当前项目名
      navpath: [{
          name: '首页',
          path: '/'
        },
        {
          name: '积分制度',
          path: ''
        }
      ],


      // form
      pointInfo: {},
      bodyHtml:'', //富文本 tinymce 值

      // table 获取
      achieveTableData: [
        // {
        //   strategyName: '积分获取test',
        //   channelList: {},
        //   useCrowd: '所有会员',
        //   getRule: '1:10',
        // }
      ],
      // table 抵现
      exchangeTableData: [
        // {
        //   strategyName: 'test',
        //   channelList: {},
        //   useCrowd: '所有会员',
        //   applyGoods: '所有商品',
        //   control: '不限制',
        //   getRule: '无积分',
        //   limitRate: '100.0%'
        // }
      ]

    }
  },
  created() {
    // this.selectReplayStyle()
  },
  methods: {
    // 新增
    handleAdd(flag){
      var that = this
      if (flag =='point') {
        this.$router.push('/addPointSystem')
      }else {
        this.$router.push('/addIntegralSytem')
      }
    },
    // 删除
    handleDelClick(index,row,ArrData) {
      var that = this
      console.log("del:",index,row,ArrData)
      var para = {
        strategyId: row.integralStrategyId,
        requestProject: that.projectName
      }
      that.$confirm('确认删除吗?', '提示', {
          //type: 'warning'
        }).then(() => {
          postRequest('/api-admin/delete-member-integral-strategy',para)
          .then((res) => {
            var resData = res.data
            if (resData.errorCode == 0) {
              that.$message({
                message: '删除成功',
                type: 'success'
              });
              ArrData.splice(index,1)
              return;
            }
            errMsg.errorMsg(resData)

          })
          .catch(function (error) {
            console.log(error);

            that.$message.error({
              duration: 1000,
              message: error.message
            })
          });

        }).catch(() => {
          console.log(error);
          that.$message.error({
              duration: 1000,
              message: error.message
            })
        });
    },

    // 获取积分制度
    getPointInfo() {
      var that = this
      var param = {
        requestProject: that.projectName
      }

      postRequest('/api-admin/get-member-integral-setting',param)
      .then((res) => {
        // console.log(res,res.data,res.data.errorCode)
        var resData = res.data
        if (resData.errorCode == 0) {
          if (!!resData.result.strategyList && !!resData.result.strategyList.length) {
            var arr = resData.result.strategyList;
            // 积分策略
            arr.forEach(function(ele,index) {
              ele.integralGet = JSON.parse(ele.integralGet);
              ele.integralUse = JSON.parse(ele.integralUse);
              if (ele.strategyType == 1) {
                that.achieveTableData.push(ele)
              }else {
                that.exchangeTableData.push(ele)
              }
            })
          }

          // form
          if (!!resData.result.memberIntegral) {
            that.pointInfo = that.handleData(resData.result.memberIntegral)
          }

          return;
        }
        // that.userFormVisible = false;
       errMsg.errorMsg(resData)

      })
      .catch(function (error) {
        console.log(error);
        // that.toLogin()
        // that.userFormVisible = false;
        that.$message.error({
          duration: 1000,
          message: error.message
        })
      });
    },
    // 处理表单中数据
    handleData(item) {
      var that = this ;
      // 获取规则需要除以 10

      item.integralGetStatus = String(item.integralGetStatus);
      item.integralUseStatus = String(item.integralUseStatus);
      item.integralGetStrategy = String(item.integralGetStrategy);
      item.integralGetDiscountControl = String(item.integralGetDiscountControl);
      item.integralUseDiscountControl = String(item.integralUseDiscountControl);
      item.integralGetDiscountControlUpper = String(item.integralGetDiscountControlUpper);
      item.integralGetDiscountControlLower = String(item.integralGetDiscountControlLower);
      item.integralUseDiscountControlUpper = String(item.integralUseDiscountControlUpper);
      item.integralUseDiscountControlLower = String(item.integralUseDiscountControlLower);

      item.integralGet = JSON.parse(item.integralGet);
      item.integralUse = JSON.parse(item.integralUse);
      item.integralCost = JSON.parse(item.integralCost);
      console.log(typeof(item.integralGet))
      item.integralGet.fee = Number(item.integralGet.fee/10).toFixed(1)
      item.integralGet.integral = Number(item.integralGet.integral/10).toFixed(1)
      return item;
    },

    changeRoute(route) {
      this.$router.push(route);
    }
  },
  components: {
    topNav,
    pointForm
  },
  mounted(){
    var that = this;
    that.getPointInfo()
  },
}

</script>
<style lang="less" scoped>

.attention-wrap {
  padding: 24px 32px;
  background: #fff;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  .item-label {
    font-size: 14px;
    color: #606266;
    margin-bottom: 30px;
    span {
      display: inline-block;
      width: 80px;
    }
  }
  .attention-content-wrap {}
}

.attention-wrap {

  .point-cell {
    &+.point-cell {
      margin-top: 60px;
    }
  }
  .flex {
    display: -webkit-flex;
    display: flex;

    &.table-top {
      border-bottom: 1px solid #e7e7eb;
      padding-bottom: 10px;
      position: relative;
    }

    .flex-1 {
      -webkit-flex: 1;
      -moz-flex: 1;
      -ms-flex: 1;
      -o-flex: 1;
      flex: 1;
    }

    .flex-title {
      font-size: 15px;
      font-weight: 700;
      padding: 5px 0;
    }

    .flex-lable {
      font-size: 13px;
      color: #828282;
    }

    .flex-l {
      text-align: left
    }

    .flex-r {
      text-align: right
    }
  }
}

</style>
