<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-06-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-26 10:01:31
 -->
<template>
  <div class="app-detail-wrap">
    <div class="app-detail-pane border-box p-20">
      <div class="flex flex-space-between">
        <div>
          <el-input placeholder="搜索门店名称或代码" maxlength="50" prefix-icon="el-icon-search" v-model="searchInput" class="w-369" @keyup.native="value => toInput(value, searchInput)" clearable @clear="clearInput"> </el-input>
        </div>
        <div>
          <el-button :disabled="disableSet" type="primary" @click="toSetStore">新增门店</el-button>
          <el-button type="danger" @click="multDel">批量移除</el-button>
        </div>
      </div>
      <div class="m-t-20">
        <el-table class="no-empty-icon-table" ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column class="" label="门店名称" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.storeName || '--' }}</template>
          </el-table-column>
          <el-table-column class="" prop="" label="code" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.storeCode || '--' }}</template>
          </el-table-column>
          <el-table-column class="" prop="" label="门店分组" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.storeGroupName || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="" label="操作" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" @click="delRow(scope.$index, scope.row)">移除</el-button>
            </template>
          </el-table-column>
          <template slot="empty">
            <div><span>暂无门店分组，</span><el-button :disabled="disableSet" type="text" @click="toSetStore()">去设置</el-button></div>
          </template>
        </el-table>
        <div class="block common-wrap__page text-right" v-if="tableData.length != 0">
          <dm-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </dm-pagination>
        </div>
        <div v-if="tableData.length == 0" class="text-center table-set-line"><span class="font-14 color-606266">暂无门店分组，</span><el-button :disabled="disableSet" type="text" @click="toSetStore()">去设置</el-button></div>
      </div>
    </div>
    <set-visiable :setStoreShow="setStoreShow" :brandId="brandId" @closeStore="closeStore" @submitStore="submitStore"></set-visiable>
  </div>
</template>
<script>
import setVisiable from '@/components/app/customer/set-visiable.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';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      wxEnterpriseRelatedId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',
      editPerson: localStorage.getItem('userInfos') && JSON.parse(localStorage.getItem('userInfos')).staffDTO ? JSON.parse(localStorage.getItem('userInfos')).staffDTO.staffName : '',
      searchInput: '',
      tableData: [],
      activeId: '1',
      loading: false,
      multipleSelection: [],

      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      setStoreShow: false,
      disableSet: false
    };
  },
  methods: {
    /**
     * 门店范围设置
     */
    toSetStore() {
      const that = this;
      that.setStoreShow = true;
    },
    closeStore() {
      const that = this;
      that.setStoreShow = false;
    },
    submitStore() {
      const that = this;
      that.setStoreShow = false;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    }, 500),
    // 搜索清除
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     * 多选分组
     */
    handleSelectionChange(val) {
      const that = this;
      that.multipleSelection = val;
    },
    /**
     * 删除
     */
    delRow(index, row) {
      const that = this;
      that
        .$confirm(`确认要删除吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postDel(index, row);
        })
        .catch(() => {});
    },
    postDel(index, row) {
      const that = this;
      let para = {
        storeTemplateRelatedIds: row.storeTemplateRelatedId,
        enterpriseId: that.activeBrand // 品牌 id
      };
      postRequest('/haoban-app-customer-web/del-template-store', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.currentPage = 1;
            that.getTableList();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 批量删除
     */
    multDel() {
      const that = this;
      if (!that.multipleSelection.length) {
        that.$message.error({
          duration: 1000,
          message: '请选择删除项'
        });
        return false;
      }
      that.postMultDel();
    },
    postMultDel() {
      const that = this;
      let para = {
        enterpriseId: that.activeBrand, // 品牌 id
        storeTemplateRelatedIds: that.multipleSelection.map(ele => ele.storeTemplateRelatedId).join(',') || '' // 门店分组,数组
      };
      postRequest('/haoban-app-customer-web/del-template-store', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.currentPage = 1;
            that.getTableList();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 分页---页码变化
     *  @param {Number} val
     */
    handleSizeChange(val) {
      const that = this;
      that.currentPage = 1;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     *  @param {Number} val
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList();
    },

    /**
     * 获取全品牌搜索设置数据
     */
    getTableList(brandId) {
      const that = this;
      let para = {
        search: that.searchInput,
        templateId: that.$route.query.templateId,
        enterpriseId: that.brandId,
        pageNum: that.currentPage,
        pageSize: that.pageSize
        // wxEnterpriseRelatedId: that.wxEnterpriseRelatedId
      };
      postRequest('/haoban-app-customer-web/get-template-store-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.result || [];
            that.total = resData.result.totalCount;
            that.disableSet = false;
            if (that.$route.query.brandId != that.brandId) {
              that.tableData = [];
              that.total = 0;
              that.disableSet = true;
            }
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      if (newData) {
        that.getTableList(that.brandId);
      }
    }
  },
  mounted() {
    const that = this;
    that.$emit('showTab', '12');
    sessionStorage.setItem('memberGroupSet', 'second');
    if (that.brandId) {
      that.getTableList(that.brandId);
    }
  },
  components: { setVisiable }
};
</script>
<style lang="scss" scoped>
.w-500 {
  width: 500px;
}

.w-195 {
  width: 195px;
}

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

.p-20 {
  padding: 20px;
}

.p-l-38 {
  padding-left: 38px;
}

.p-l-199 {
  padding-left: 199px;
}

.app-detail-wrap {
  height: 100%;
  background: #fff;
  .el-tabs {
    background: #fff;
    >>> .el-tabs__nav-wrap {
      /* height: 48px;
      line-height: 48px; */
      &::after {
        height: 1px;
      }
      /* .el-tabs__nav-scroll {
        padding-left: 20px;
      } */
    }
  }
  .condition-tip {
    width: 740px;
    .el-alert--info {
      background: #e6f7ff;
      border: 1px solid rgba(145, 213, 255, 1);
      .el-alert__icon {
        font-size: 12px;
      }
    }
  }
}
.table-set-line {
  height: 43px;
  line-height: 43px;
  border-bottom: 1px solid #dcdfe6;
}
</style>
