<!--
  我的顾客应用：
  <app-detail ></app-detail>
 -->
<template>
  <div class="app-detail-wrap">
    <el-tabs v-model="activeName" @tab-click="handleClick" v-if="tabType == 1">
      <el-tab-pane label="会员搜索设置" name="first">
        <div class="app-detail-pane border-box p-45">
          <el-checkbox v-model="myCustomData.searchSwitch">会员搜索设置</el-checkbox>
          <div class="m-t-46">
            <el-button type="primary" @click="submit('searchFlag')">保 存</el-button>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-tabs v-model="activeName" @tab-click="handleClick" v-if="tabType == 2">
      <el-tab-pane label="在线沟通设置" name="first">
        <div class="app-detail-pane border-box p-45">
          <el-checkbox v-model="myCustomData.onlineChat">在线沟通设置</el-checkbox>
          <div class="m-t-46">
            <el-button type="primary" @click="submit('chatFlag')">保 存</el-button>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
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 '';
      }
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      activeName: 'first',
      myCustomData: {
        searchFlag: false,
        chatFlag: false
      }
    };
  },
  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: {
    handleClick(tab, event) {
      console.log(tab, event);
    },

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

    /**
     * 保存-API
     */
    seData(type) {
      const that = this;
      let para = {
        brandId: that.brandId
      };
      para[type] = !!that.myCustomData.searchFlag ? 1 : 0;
      let url = '/haoban-app-member-web/customer/save-online-chat-setting';
      if (type === 'searchFLag') {
        url = '/haoban-app-member-web/customer/find-customer-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 && !!resData.result.list) {
              that.myCustomData.searchFlag = !!resData.result.searchFlag ? 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 && !!resData.result.list) {
              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;
      console.log(newData);
      that.getChatSet(newData);
      that.getCustomerSet(newData);
    }
  }
  /*  mounted() {
    const that = this;
  },
  components: {} */
};
</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: 54px;
      line-height: 54px;

      &::after {
        height: 1px;
      }

      .el-tabs__nav-scroll {
        padding-left: 20px;
      }
    }

    .el-tab-pane {
      padding: 1px;
    }
  }
}
</style>
