<template>
  <div>
    <div class="warp">
      <h2>加好友配置</h2>
      <span class="echoData" v-show="friendSettingType">{{ friendSettingTypeOption[friendSettingType] }}</span>
      <el-button type="text" style="margin-left: 20px;" @click="openDialog">去配置</el-button>
    </div>
    <div class="daily-set-wrap">
      <div class="daily-set-content border-box">
        <div class="admin-tip-body">
          <div role="alert" class="el-alert el-alert--info">
            <i class="el-alert__icon el-icon-info"></i>
            <div class="el-alert__content">
              <p class="el-alert__title">
                1. 因企业微信接口限制，若在企微管理端为相关人员配置欢迎语，好办设置欢迎语不会生效；<br />
                2. 若使用好办欢迎语，请关闭其他企业自建应用/第三方应用的欢迎语配置； <br />
                3. 在此处所自定义配置的欢迎语基础上，系统将默认包含“带参会员小程序卡片”，以便门店员工添加客户好友后，引导客户进入带参会员小程序页面授权微信信息，进而使得系统将企业客户与GIC后台某客户对应起来；<br />
                4. 如果导购没有绑定导购code，没有主门店信息，则无法发送好办欢迎语；
              </p>
            </div>
          </div>
        </div>
        <div class="flex flex-space-between m-b-20 m-t-20">
          <el-input placeholder="请输入标题" maxlength="50" v-model="title" class="w-264" style="width: 264px;" @keyup.native="value => toInput(value, searchInput)" clearable @clear="clearSearch"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input>
          <el-button type="primary" @click="changeRoute">新建欢迎语</el-button>
        </div>
        <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" :style="{ width: '100%' }">
          <el-table-column prop="title" label="标题" width="220" show-overflow-tooltip>
            <template slot-scope="{ row }">{{ row.title }}</template>
          </el-table-column>
          <el-table-column prop="welcomeContent" label="内容" show-overflow-tooltip>
            <template slot-scope="{ row }">{{ row.welcomeContent }}</template>
          </el-table-column>
          <el-table-column label="附件">
            <template slot-scope="{ row }">
              <el-popover placement="top-start" trigger="hover">
                <div class="flexBox" v-for="item in row.welcomeMediaList" :key="item.welcomeMediaId">
                  <svg-icon :iconname="iconType[item.mediaType]" size="16" style="margin-right: 13px;"> </svg-icon>
                  <span class="popverText">{{ item.mediaTitle }}</span>
                </div>
                <div slot="reference" class="flexBox" v-if="row.welcomeMediaList && row.welcomeMediaList.length">
                  <svg-icon :iconname="iconType[row.welcomeMediaList[0].mediaType]" size="40" style="margin-right: 10px;"> </svg-icon>
                  <span class="wrapText"> {{ row.welcomeMediaList[0].mediaTitle }}</span>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="更新时间" width="120" show-overflow-tooltip>
            <template slot-scope="{ row }">{{ row.updateTime }}</template>
          </el-table-column>
          <el-table-column prop="operatorName" label="操作人" width="120" show-overflow-tooltip> </el-table-column>
          <el-table-column prop="suitDepartmentName" label="适用范围" width="120" show-overflow-tooltip>
            <template slot-scope="{ row }">{{ row.suitDepartmentName.join(',') }}</template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="{ row }">
              <el-button type="text" @click="changeRoute(row)">编辑</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog title="加好友配置" :visible.sync="dialogVisible" width="590px" :before-close="handleClose">
      <span class="tips">好办欢迎语支持文字、图片、视频、文件、链接及小程序</span>
      <el-form label-width="100px" style="margin-top:20px">
        <el-form-item label="加好友配置">
          <div v-for="(val, key) in friendSettingTypeOption" :key="key">
            <el-radio :label="Number(key)" v-model="type">{{ val }}</el-radio>
            <el-tag size="small" v-show="key == 1">推荐使用</el-tag>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="saveSetting">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { postJsonRequest, getRequest } from '@/api/api';
import { _debounce } from '@/common/js/public';
export default {
  props: {},
  data() {
    return {
      friendSettingTypeOption: {
        1: '发送【好办欢迎语】+【带导购参数的会员小程序卡片】',
        2: '发送【好办欢迎语】+【带导购参数的公众号二维码】',
        3: '发送【好办欢迎语】',
        4: '不发送【好办欢迎语】，用企微欢迎语'
      },
      title: '',
      wxEnterpriseId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',
      friendSettingType: '',
      type: 1,
      friendSettingId: '',
      btnLoading: false,
      dialogVisible: false,
      iconType: {
        1: 'iconwenjianleixingtupian',
        2: 'iconwenjianleixingshipin',
        3: 'iconwenjianleixing-wenjian',
        4: 'iconwenjianleixing-lianjie'
      },
      tableData: []
    };
  },
  mounted() {
    const that = this;
    that.$emit('showTab', 4);
    document.documentElement.style.backgroundColor = '#f0f2f5';
    that.getData();
    this.getSetting();
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  methods: {
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      this.getData();
    }, 500),
    /**
     * 搜索标签清空
     */
    clearSearch() {
      this.getData();
    },
    openDialog() {
      this.dialogVisible = true;
    },
    getSetting() {
      getRequest('/haoban-manage3-web/welcome/get/friend-setting', { wxEnterpriseId: this.wxEnterpriseId }).then(res => {
        let resData = res.data;
        if (resData.code == 0 && resData.result) {
          const { friendSettingId, friendSettingType } = resData.result;
          this.friendSettingId = friendSettingId;
          this.type = friendSettingType;
          this.friendSettingType = friendSettingType;
        }
      });
    },
    saveSetting() {
      let data = {
        friendSettingId: this.friendSettingId,
        wxEnterpriseId: this.wxEnterpriseId,
        friendSettingType: this.type
      };
      this.btnLoading = true;
      postJsonRequest('/haoban-manage3-web/welcome/save/friend-setting', data)
        .then(res => {
          console.log(res);
          let resData = res.data;
          if (resData.code == 0 && resData.result) {
            this.$message.success(resData.message);
            this.dialogVisible = false;
            this.getSetting();
          }
        })
        .finally(() => {
          this.btnLoading = false;
        });
    },
    /**
     * 跳转
     */
    changeRoute(row) {
      this.$router.push({ path: '/salutatorySet', query: { id: row.welcomeId, type: this.friendSettingType } });
    },

    /**
     * 获取数据
     */
    getData() {
      postJsonRequest('/haoban-manage3-web/welcome/list', { title: this.title, wxEnterpriseId: this.wxEnterpriseId })
        .then(res => {
          let resData = res.data;
          if (resData.code == 0 && resData.result) {
            this.tableData = resData.result;
            this.tableData.forEach(item => {
              item.suitDepartmentName = [];
              if (item.suitDepartmentList && item.suitDepartmentList.length) {
                item.suitDepartmentList.forEach(ele => {
                  item.suitDepartmentName.push(ele.departmentName);
                });
              }
            });
          }
        })
        .catch(error => {
          this.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {},

  components: {}
};
</script>
<style type="text/scss" lang="scss" scoped>
.warp {
  background: #fff;
  margin-bottom: 10px;
  padding: 20px;
  font-size: 14px;
  h2 {
    margin-bottom: 12px;
    font-weight: 500;
  }
  .echoData {
    background: #f5f7fa;
    border-radius: 2px;
    line-height: 22px;
    color: #606266;
    padding: 5px 13px;
  }
}

.el-alert--info {
  align-items: flex-start;
  width: 700px;
  padding-top: 5px;
  font-size: 14px;
  color: #606266;
  background: rgba(240, 245, 255, 1);
  border-radius: 2px;
  border: 1px solid rgba(133, 165, 255, 1);
}
.el-icon-info {
  width: 12px;
  font-size: 12px;
  color: #2f54eb;
  margin-top: 5px;
}
.el-alert__content {
  padding-left: 9px;
  .el-alert__title {
    font-size: 13px;
    font-weight: 400;
    color: #303133;
    line-height: 22px;
  }
}
.daily-set-wrap {
  height: 100%;
  background-color: #fff;
  .daily-set-content {
    padding: 20px;
    .table-condition-left {
      font-size: 0;
    }
  }
}
.el-radio {
  margin-right: 0;
}
.el-radio + .el-radio {
  margin-left: 0;
}
.flexBox {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .wrapText {
    width: 90%;
    text-align: left;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .popverText {
    font-size: 12px;
    width: 90%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.flexBox + .flexBox {
  margin-top: 10px;
}
</style>
