<template>
  <div class="mall-section-container">
    <div class="mall-content-section">
      <div class="mall-content-title">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '' }"><a href="/report/#/memberSummary">首页</a></el-breadcrumb-item>
          <el-breadcrumb-item>积分商城</el-breadcrumb-item>
          <el-breadcrumb-item>设置</el-breadcrumb-item>
          <el-breadcrumb-item>配送设置</el-breadcrumb-item>
        </el-breadcrumb>
        <h3>
          <span>配送设置</span>
        </h3>
      </div>
      <div class="section">
        <div style="border-bottom:1px solid #dcdfe6;padding:24px;">
          <div class="header-top" style="display:inline-block;margin-right:30px;padding:12px 0px">
            <span style="width:80px;">门店自提 </span>
            <el-switch v-model="pickStatus" @change="changeStatus"> </el-switch>
          </div>
          <div class="header-top" v-show="pickStatus" style="display:inline-block;background:#F0F2F5;padding:12px 20px">
            <span
              >会员自助核销
              <el-tooltip effect="dark" content="如开启会员自助核销，则会员可在到门店后，在小程序端输入导购代码进行提货核销；但不建议此方式！！推荐导购直接使用好办APP（2.0版本）进行扫码核销。" placement="top-start">
                <i class="iconfont icon-xinxixianshi"></i>
              </el-tooltip>
            </span>
            <el-switch v-model="cancelStatus" @change="changeCancelStatus" :disabled="!pickStatus"> </el-switch>
          </div>
        </div>

        <div v-show="pickStatus" style="padding:24px;">
          <div class="operate-top">
            <div class="fl">
              <div class="search-item">
                <el-input v-model="searchValue" placeholder="请输入自提点名称进行搜索" prefix-icon="el-icon-search" clearable class="w-280" @keyup.enter.native="getSearchList" @clear="getSearchList"> </el-input>
              </div>
            </div>
            <div class="fr">
              <el-button plain @click="exportData"><i class="iconfont icon-icon_yunxiazai" style="margin-right:5px"></i>导出自提点</el-button>
              <el-button plain @click="setPicked">自提设置</el-button>
              <el-button type="primary" @click="addStore('-1')">新增自提点</el-button>
            </div>
          </div>
          <div v-loading="loading">
            <el-table ref="multipleTable" :data="tableDate" style="width: 100%" @selection-change="handleSelectAll">
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column prop="pickUpPointName" label="自提点名称" min-width="200px"> </el-table-column>
              <el-table-column prop="pickUpPointAddress" label="自提点地址" min-width="200px"> </el-table-column>
              <el-table-column prop="pickUpPointPhone" label="联系电话"> </el-table-column>
              <el-table-column prop="canChooseTime" label="自提时间可选">
                <template slot-scope="scope">
                  <span>{{ scope.row.canChooseTime === 1 ? '是' : '否' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <div>
                    <el-button type="text" @click="goAdd(scope.row)">编辑</el-button>
                    <el-popover placement="top" width="160" v-model="scope.row.visible2" style="margin-left:8px">
                      <p style="line-height:1.5;padding: 10px 10px 20px;">确定删除该自提点吗？</p>
                      <div style="text-align: right; margin: 0">
                        <el-button size="mini" type="text" @click="scope.row.visible2 = false">取消</el-button>
                        <el-button type="primary" size="mini" @click="deletePoint(scope.row, scope.$index)">确定</el-button>
                      </div>
                      <el-button slot="reference" type="text">删除</el-button>
                    </el-popover>
                  </div>
                </template>
              </el-table-column>
              <template slot="empty">
                <div class="no-data-wrap">
                  <div class="no-data-icon">
                    <img src="../../../static/img/no-data_icon.png" />
                  </div>
                  <p>暂无数据</p>
                </div>
              </template>
            </el-table>
            <div class="pagination-conteiner" v-show="tableDate.length > 0">
              <div class="pagination">
                <dm-pagination v-if="total != 0" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next" :total="total"> </dm-pagination>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 自提点设置 -->
    <pickedAuto :pickedForm="pickedForm" @getList="getList"></pickedAuto>
    <!-- 导出自提点 -->
    <vue-gic-export-excel :dataArr="tableDate" :dialogVisible.sync="dialogVisible" :type="2" :excelUrl="excelUrl" :params="params" :projectName="projectName"></vue-gic-export-excel>
  </div>
</template>

<script>
import qs from 'qs';
import request from '../../service/request.js';
// import getInputVal from '../../../utils/common.js';
// import stockModal from '../../../components/getSkuList';
import pickedAuto from './pickedAuto';
import { alertInfo } from '../../utils/user.js';
export default {
  data() {
    return {
      searchValue: '',
      currentPage: 1,
      pageSizes: [20, 40, 60, 80],
      pageSize: 20,
      total: 2,
      pickStatus: true, //自提状态
      cancelStatus: false, //核销状态
      loading: false,
      selectList: [], //复选框选中的数据
      tableDate: [],
      pickedForm: {
        dialogVisible: false,
        selectIdList: [], //复选框选中的数据的id
        list: [{ timeRange: [] }] //初始数据
      },
      enableAccessControl: false,
      // 导出
      dialogVisible: false,
      excelUrl: '/api-integral-mall/pick-up-points-excel',
      params: { search: '', dataType: 2, requestProject: 'integral-mall' },
      projectName: 'integral-mall'
    };
  },
  created() {
    this.getAccessControl();
    // export const alertInfo = () => {
    //   Notification({
    //     title: '温馨提示',
    //     type: 'warning',
    //     message: '在此页面进行修改、删除等操作将更改本商户下所有的配置 请谨慎进行'
    //   });
    // }
    this.enableAccessControl = localStorage.getItem('enableAccessControl') == 'false' ? false : true;
    if (this.enableAccessControl) {
      alertInfo();
    }
    this.getStatus();
  },
  methods: {
    getAccessControl() {
      let params = {
        requestProject: 'integral-mall'
      };
      request.get('/api-auth/get-login-user-info', { params }).then(res => {
        if (res.data.errorCode === 0) {
          if (res.data.result.enableAccessControl) {
            this.enableAccessControl = true;
          } else {
            this.enableAccessControl = false;
          }
          window.localStorage.setItem('enableAccessControl', this.enableAccessControl);
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    getSearchList() {
      this.currentPage = 1;
      this.getList();
    },
    //分页设置每页的数量
    handleSizeChange(size) {
      this.pageSize = size;
      this.getList();
    },
    // 分页设置跳转到第几页
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getList();
    },
    //复选
    handleSelectAll(val) {
      this.selectList = val;
    },
    // 自提设置
    setPicked() {
      if (this.selectList.length < 1) {
        this.$message.error('请先选择自提点');
        return false;
      } else {
        this.pickedForm.selectIdList = [];
        for (let item of this.selectList) {
          this.pickedForm.selectIdList.push(item.integralMallPickUpPointId);
        }
        let currentTime = new Date(new Date().valueOf());
        let currentTime1 = new Date(new Date(new Date().getTime() + 1000 * 60).valueOf());
        // this.pickedModal.timeRangeList[0].timeRange = [currentTime, currentTime1];
        this.pickedForm.list[0].timeRange = [currentTime, currentTime1];
        this.pickedForm.dialogVisible = true;
      }
    },
    //获取自提点状态
    getStatus() {
      request.get('/api-integral-mall/pick-up-point-switch-status').then(res => {
        if (res.data.errorCode === 0) {
          if (res.data.result.status === 0) {
            this.pickStatus = false;
          } else if (res.data.result.status === 1) {
            this.pickStatus = true;
          }
          if (res.data.result.memberVerification === 0) {
            this.cancelStatus = false;
          } else if (res.data.result.memberVerification === 1) {
            this.cancelStatus = true;
          }
          this.getList();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    // 修改自提点状态
    changeStatus() {
      let params = {
        status: this.pickStatus ? 1 : 0
      };
      request.get('/api-integral-mall/set-pick-up-point-switch-status', { params }).then(res => {
        if (res.data.errorCode === 0) {
          this.$message.success('设置成功');
          this.getStatus();
          this.getList();
        } else {
          this.pickStatus = !this.pickStatus;
          this.$message.error(res.data.message);
        }
      });
    },
    //开启关闭会员核销
    changeCancelStatus() {
      let params = {
        status: this.cancelStatus ? 1 : 0
      };
      request.get('/api-integral-mall/set-pick-up-point-member-verification', { params }).then(res => {
        if (res.data.errorCode === 0) {
          this.$message.success('设置成功');
          // this.getList();
        } else {
          this.cancelStatus = !this.cancelStatus;
          this.$message.error(res.data.message);
        }
      });
    },
    //删除自提点
    deletePoint(item, index) {
      let params = {
        integralMallPickUpPointId: item.integralMallPickUpPointId
      };
      request.get('/api-integral-mall/delete-pick-up-point', { params }).then(res => {
        if (res.data.errorCode === 0) {
          this.tableDate.splice(index, 1);
          if (this.tableDate.length === 0) {
            if (this.currentPage > 1) {
              this.currentPage = this.currentPage - 1;
            }
          }
          this.getList();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    getList() {
      let params = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchValue
      };
      this.loading = true;
      request.post('/api-integral-mall/page-pick-up-points', qs.stringify(params)).then(res => {
        if (res.data.errorCode === 0) {
          if (res.data.result.result) {
            this.tableDate = res.data.result.result;
            this.total = res.data.result.totalCount;
          } else {
            this.tableDate = [];
            this.total = 0;
          }
          this.loading = false;
        } else {
          this.$message.error(res.data.message);
          this.loading = false;
        }
      });
    },

    // 新增
    addStore() {
      this.$router.push({ name: 'editDeliver', query: { integralMallPickUpPointId: '-1' } });
    },
    // 编辑
    goAdd(item) {
      this.$router.push({ name: 'editDeliver', query: { integralMallPickUpPointId: item.integralMallPickUpPointId } });
    },
    // 导出数据
    exportData() {
      let params = {
        search: this.searchValue
      };
      request
        .post('/api-integral-mall/pick-up-points-excel', qs.stringify(params))
        .then(res => {
          if (res.data.errorCode === 0) {
            setTimeout(() => {
              window.open(window.location.origin + '/mall/#/reportList', '_self');
            }, 1000);
          } else {
            this.$message.error(res.data.message);
            return false;
          }
        })
        .catch(error => {
          this.$message.error({
            duration: 1000,
            message: error.message
          });
          return false;
        });
      // this.dialogVisible = true;
      // this.params.search = this.searchValue;
    }
  },
  components: {
    pickedAuto
  }
};
</script>
<style scoped>
.section {
  background: #fff;
  margin: 24px;
  /* padding: 24px; */
}
.header-top {
  /* margin: 15px 0 22px 0; */
}
.header-top span {
  width: 130px;
  display: inline-block;
}
</style>
