<!--
  我的顾客应用：
  <app-detail ></app-detail>
 -->
<template>
  <div class="app-detail-wrap">
    <div v-if="tabType == 1">
      <!-- <common-detail-top :topMenuData="topMenuData" :activeId="activeId"></common-detail-top> -->
      <div class="common-detail-tab">
        <el-tabs v-model="activeName" class="daily-set-tab" @tab-click="handleTabClick">
          <el-tab-pane label="会员搜索设置" name="first"></el-tab-pane>
          <el-tab-pane label="会员展示设置" name="second"></el-tab-pane>
        </el-tabs>
      </div>
      <div v-if="activeName == 'first'" class="app-detail-pane border-box p-45">
        <el-checkbox v-model="myCustomData.searchFlag">允许全品牌搜索</el-checkbox>
        <div class="m-t-46">
          <el-button type="primary" @click="submit('searchFlag')">保 存</el-button>
        </div>
      </div>
      <div v-if="activeName == 'second'" class="app-detail-pane border-box p-45">
        <div class="m-b-20"><el-checkbox v-model="myCustomData.mainStoreFlag">展示服务门店会员</el-checkbox></div>
        <div class="m-b-20"><el-checkbox v-model="myCustomData.minorStoreFlag">展示协管门店会员</el-checkbox></div>
        <div class="m-b-20"><el-checkbox v-model="myCustomData.openCardStoreFlag">展示开卡门店会员</el-checkbox></div>
        <div><el-checkbox v-model="myCustomData.clerkSeeOtherFlag">允许导购查看其他导购会员</el-checkbox></div>
        <div class="m-t-46">
          <el-button type="primary" @click="submit('showFlag')">保 存</el-button>
        </div>
      </div>
    </div>
    <div v-if="tabType == 2">
      <common-detail-top :topMenuData="topMenuData" :activeId="activeId"></common-detail-top>
      <div class="app-detail-pane border-box p-45">
        <el-checkbox v-model="myCustomData.chatFlag">在线沟通设置</el-checkbox>
        <div class="m-t-46">
          <el-button type="primary" @click="submit('chatFlag')">保 存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import commonDetailTop from '@/components/app/common-detail-top.vue';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
import { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
export default {
  name: 'app-detail',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    tabType: {
      type: String,
      default() {
        return '1';
      }
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      activeName: 'first',
      myCustomData: {
        searchFlag: false,
        chatFlag: false,
        mainStoreFlag: false, // 服务门店显示，1显示0不现实
        minorStoreFlag: false, // 协管门店显示，1显示0不显示
        openCardStoreFlag: false, // 开卡门店显示，1显示0不显示
        clerkSeeOtherFlag: false // 导购查看其他导购，1显示0不显示
      },
      topMenuData: [
        {
          id: '1',
          name: '会员搜索设置'
        }
      ],
      activeId: '1'
    };
  },
  beforeMount() {
    let that = this;
    let host = window.location.origin;
    if (host.indexOf('localhost') != -1) {
      that.baseUrl = 'http://www.gicdev.com';
    } else {
      that.baseUrl = host;
    }
  },
  methods: {
    /* eslint-disable */
    handleTabClick(tab, event) {
      const that = this;
      if (tab.name == 'first') {
        that.getCustomerSet(that.brandId);
      }else {
        that.getShowSet(that.brandId);
      }
    },

    /**
     * 保存
     */
    submit: _debounce(function(type) {
      const that = this;
      that.setData(type);
    }, 500),

    /**
     * 保存-API
     */
    setData(type) {
      const that = this;
      let para = {
        brandId: that.brandId,
        enterpriseId: JSON.parse(localStorage.getItem('userInfo')).enterpriseId
      };
      let url;
      if (type != 'showFlag') {
        para[type] = !!that.myCustomData[type] ? 1 : 0;
        url = '/haoban-app-member-web/customer/save-online-chat-setting';
        if (type === 'searchFlag') {
          url = '/haoban-app-member-web/customer/save-customer-setting';
        }
      }else {
        para['mainStoreFlag'] = !!that.myCustomData['mainStoreFlag'] ? 1 : 0;
        para['minorStoreFlag'] = !!that.myCustomData['minorStoreFlag'] ? 1 : 0;
        para['openCardStoreFlag'] = !!that.myCustomData['openCardStoreFlag'] ? 1 : 0;
        para['clerkSeeOtherFlag'] = !!that.myCustomData['clerkSeeOtherFlag'] ? 1 : 0;
        url = '/haoban-app-member-web/customer/save-member-show-setting';
      }

      postRequest(url, para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('保存成功', 'success');
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 获取全品牌搜索设置数据
     */
    getCustomerSet(brandId) {
      const that = this;
      let para = {
        brandId: brandId
      };
      postRequest('/haoban-app-member-web/customer/find-customer-setting', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (!!resData.result) {
              that.myCustomData.searchFlag = !!resData.result.searchFlag ? true : false;
            }
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取会员展示设置数据
     */
    getShowSet(brandId) {
      const that = this;
      let para = {
        brandId: brandId
      };
      postRequest('/haoban-app-member-web/customer/find-member-show-setting', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (!!resData.result) {
              that.myCustomData.mainStoreFlag = !!resData.result.mainStoreFlag ? true : false;
              that.myCustomData.minorStoreFlag = !!resData.result.minorStoreFlag ? true : false;
              that.myCustomData.openCardStoreFlag = !!resData.result.openCardStoreFlag ? true : false;
              that.myCustomData.clerkSeeOtherFlag = !!resData.result.clerkSeeOtherFlag ? true : false;
            }
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取在线沟通设置数据
     */
    getChatSet(brandId) {
      const that = this;
      let para = {
        brandId: brandId
      };
      postRequest('/haoban-app-member-web/customer/find-online-chat-setting', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (!!resData.result) {
              that.myCustomData.chatFlag = !!resData.result.chatFlag ? true : false;
            }
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      that.getChatSet(newData);
      that.getCustomerSet(newData);
      that.getShowSet(newData);
    },
    tabType: function(newData, oldData) {
      const that = this;
      if (newData == 1) {
        that.topMenuData = [
          {
            id: '1',
            name: '会员搜索设置'
          }
        ]
        that.activeId = 1;
      }else {
        that.topMenuData = [
          {
            id: '2',
            name: '在线沟通设置'
          }
        ]
        that.activeId = '2';
      }
    }
  },
  /*  mounted() {
    const that = this;
  },*/
  components: {
    commonDetailTop
  }
};
</script>
<style lang="less" scoped>
.w-500 {
  width: 500px;
}

.color-1890ff {
  color: #1890ff;
}

.app-detail-wrap {
  .el-tabs {
    background: #fff;
    /deep/ .el-tabs__nav-wrap {
      /* height: 48px;
      line-height: 48px; */
      &::after {
        height: 1px;
      }
      .el-tabs__nav-scroll {
        padding-left: 20px;
      }
    }
  }
}
</style>
