<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-26 10:01:07
 -->

<!--
  我的客户应用：
  <app-detail ></app-detail>
 -->
<template>
  <div class=" border-box p-lbr-20">
    <div class="flex flex-space-between">
      <div></div>
      <div>
        <span class="font-12 color-909399">最多新建20个模板</span>
        <el-button v-if="$getButtonLimit($buttonCode.addGroupTemp)" :limit-code="$buttonCode.addGroupTemp" :disabled="$store.state.wxEnterpriseType || templateList.length >= 20" class="m-l-20" type="primary" @click="addTemplate">新建模版</el-button>
      </div>
    </div>
    <div class="m-t-20">
      <el-table class="select-table" ref="tempTable" v-loading="loading" :data="templateList" tooltip-effect="dark" :style="{ width: '100%' }">
        <el-table-column label="模板名称" show-overflow-tooltip min-width="150">
          <template slot-scope="scope">{{ scope.row.templateName }}</template>
        </el-table-column>
        <el-table-column label="模板id" show-overflow-tooltip min-width="150">
          <template slot-scope="scope">{{ scope.row.templateId }}</template>
        </el-table-column>
        <el-table-column label="创建人" prop="creatorName" show-overflow-tooltip min-width="90"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="80">
          <template slot-scope="scope">
            <div class="line-18">{{ scope.row.createTime | timeStampToYmd }}</div>
            <div class="line-18">{{ scope.row.createTime | timeStampToHms }}</div>
          </template>
        </el-table-column>
        <el-table-column label="编辑人" show-overflow-tooltip min-width="90">
          <template slot-scope="scope">{{ scope.row.editPerson }}</template>
        </el-table-column>
        <el-table-column prop="" label="最近编辑时间" show-overflow-tooltip min-width="80">
          <template slot-scope="scope">
            <div class="line-18">{{ scope.row.editTime | timeStampToYmd }}</div>
            <div class="line-18">{{ scope.row.editTime | timeStampToHms }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" v-if="$getButtonLimit($buttonCode.editGroupTemp) || $getButtonLimit($buttonCode.setStoreArea) || $getButtonLimit($buttonCode.delGroupTemp)" min-width="150">
          <template slot-scope="scope">
            <el-button v-if="$getButtonLimit($buttonCode.editGroupTemp)" :limit-code="$buttonCode.editGroupTemp" @click="toEdit(scope.$index, scope.row)" type="text" :disabled="$store.state.wxEnterpriseType">编辑</el-button>
            <el-button v-if="$getButtonLimit($buttonCode.setStoreArea)" :limit-code="$buttonCode.setStoreArea" @click="toStore(scope.$index, scope.row)" type="text" :disabled="$store.state.wxEnterpriseType">门店范围设置</el-button>
            <el-button v-if="$getButtonLimit($buttonCode.delGroupTemp)" :limit-code="$buttonCode.delGroupTemp" @click="toDel(scope.$index, scope.row)" type="text" :disabled="$store.state.wxEnterpriseType">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
import { postRequest } from '@/api/api';
export default {
  name: 'app-detail',
  props: {
    brandId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      wxEnterpriseRelatedId: sessionStorage.getItem('userInfos') ? JSON.parse(sessionStorage.getItem('userInfos')).wxEnterpriseId : '',
      loading: false,
      // 模板
      templateList: []
    };
  },
  methods: {
    /**
     * 删除
     */
    toDel(index, row) {
      const that = this;
      that
        .$confirm('是否要删除选中的模板?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.delTempList(row.templateId);
        })
        .catch(() => {});
    },
    /**
     * 删除卡券
     */
    delTempList(templateId) {
      const that = this;
      let para = {
        templateId: templateId
      };
      postRequest('/haoban-app-customer-web/del-group-template', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.getTableList();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 编辑
     */
    toEdit(index, row) {
      const that = this;
      that.$router.push(`/editGroupTemplate?templateId=${row.templateId}`);
    },
    /**
     * 门店范围设置
     */
    toStore(index, row) {
      const that = this;
      that.$router.push(`/storeRangeSet?templateId=${row.templateId}&brandId=${that.brandId}`);
    },
    /**
     * 新增模板
     */
    addTemplate() {
      const that = this;
      that.$router.push('/addGroupTemplate');
    },
    /**
     * 获取模板
     */
    getTableList() {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId
      };
      postRequest('/haoban-app-customer-web/get-template-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.templateList = resData.result || [];
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  mounted() {
    const that = this;
    that.getTableList();
  }
};
</script>
<style lang="scss" scoped>
.p-lbr-20 {
  padding: 0 20px 20px 20px;
}

.m-l-20 {
  margin-left: 20px;
}
</style>
