<template>
  <div class="upload_links">
    <div class="header">
      <div class="search">
        <div class="input">
          <img src="../../img/search.png" />
          <input
            type="text"
            @focus="focusInput"
            v-model="inputValue"
            class="inputFocus"
            placeholder="DLR Code / 经销商"
          />
        </div>
        <div class="area">
          <p>所属区域</p>
          <select v-model="areaValue">
            <option value="">全区</option>
            <option v-for="(item, index) in areaList" :key="index">
              {{ item }}
            </option>
          </select>
        </div>
        <div class="time">
          <p>请选择时间段</p>
          <div class="date_pick">
            <Datepicker
              :language="zh"
              format="yyyy/MM/dd"
              placeholder=" 年 / 月 / 日"
              v-model="startTimeValue"
            ></Datepicker>
          </div>
          <span>至</span>
          <div class="date_pick">
            <Datepicker
              :language="zh"
              format="yyyy/MM/dd"
              placeholder=" 年 / 月 / 日"
              v-model="endTimeValue"
            ></Datepicker>
          </div>
        </div>
        <div class="current_button" @click="search">搜索</div>
      </div>
    </div>
    <div class="count">
      <p>针对论坛及其它平台链接上传</p>
      <div style="display: flex">
        <div><Count :sum="sum" /></div>
        <div class="current_button" @click="exportData">导出</div>
      </div>
    </div>
    <div class="tableBox">
      <Table :tableData="tableData" @go_detail="ulrJump"></Table>
    </div>
    <TablePage
      :currentPage="currentPage"
      :totalPage="totalPage"
      @change_page="changePage"
      @jump_page="jumpPage"
    ></TablePage>
  </div>
</template>

<script>
import TablePage from "../../components/TablePage";
import Count from "../../components/Count";
import Table from "./components/UploadLinksTable";
import { env_url, php_url } from "../../config/env";
import Datepicker from "vuejs-datepicker";
import { zh } from "vuejs-datepicker/dist/locale";
import axiosTest from "axios";
export default {
  components: {
    TablePage,
    Count,
    Table,
    Datepicker,
  },
  data() {
    return {
      zh: zh,
      startTimeValue: "",
      endTimeValue: "",
      inputValue: "",
      sum: 0,
      currentPage: 1,
      pageSize: 20,
      tableData: [],
      areaList: [],
      areaValue: "",
      startTime: "",
      endTime: "",
      onlineUrl: env_url, //'http://8.140.188.129:8080'线上 //http://8.136.230.133:8080 //测试,
      otherUrl: php_url,
    };
  },
  computed: {
    // 表格总页数
    totalPage() {
      return Math.ceil(this.sum / this.pageSize);
    },
  },
  methods: {
    // 当焦点在选择框时，清空选择框
    focusInput: function () {
      this.inputValue = "";
    },
    formatDateToDate: function (val) {
      if (val) {
        let date = new Date(val)
        let y = date.getFullYear()
        let m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        const time =  y + '-' + m + '-' + d;
        return time
      } else {
        return ""
      }
    },
    search: function () {
      this.startTime = this.formatDateToDate(this.startTimeValue);
      this.endTime = this.formatDateToDate(this.endTimeValue);
      let data = {
        queryParams: this.inputValue,
        localArea: this.areaValue,
        startTime: this.startTime,
        endTime: this.endTime,
        Page: this.currentPage,
        Rows: this.pageSize,
      };
      //  this.firmsLinkUpload(data);
      this.getPageTableDate(data);
    },
    changePage: function (page) {
      this.currentPage = page;
      let req = {
        queryParams: this.inputValue,
        localArea: this.areaValue,
        startTime: this.startTime,
        endTime: this.endTime,
        Page: this.currentPage,
        Rows: this.pageSize,
      };
      // this.firmsLinkUpload(req);
      this.getPageTableDate(req);
    },
    // 点击上一页，下一页，首页，尾页
    jumpPage: function (item) {
      switch (item) {
        case 1:
          this.currentPage = 1;
          break;
        case 2:
          this.currentPage = this.currentPage - 1;
          break;
        case 3:
          this.currentPage = this.currentPage + 1;
          break;
        case 4:
          this.currentPage = this.totalPage;
          break;
      }
      let req = {
        queryParams: this.inputValue,
        localArea: this.areaValue,
        startTime: this.startTime,
        endTime: this.endTime,
        Page: this.currentPage,
        Rows: this.pageSize,
      };
      // this.firmsLinkUpload(req);
      this.getPageTableDate(req);
    },
    ulrJump: function (url) {
      window.open(url);
    },
    exportData: function () {
      this.exportTem();
    },
    // 获取表格数据
    firmsLinkUpload: function (data = {}) {
      this.$http({
        method: "post",
        url: "/firmsLinkUpload",
        data: data,
      })
        .then((res) => {
          this.tableData = res.data.data;
          this.sum = res.data.count;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //php获取表格数据
    // 获取表格数据
    getPageTableDate: function (data = {}) {
      axiosTest({
        method: "post",
        url: this.otherUrl + "/lexus_php/api/report_list.php",
        data: data,
      })
        .then((res) => {
          this.tableData = res.data.data;
          this.sum = +res.data.count;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 获取 所有区域  接口
    getAreaList: function () {
      this.$http({
        method: "post",
        url: "/sys/agent/selectAgentAreaInfoList",
      })
        .then((res) => {
          if (res.data && res.data.code === 200) {
            this.areaList = res.data.data;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 导出   接口
    exportTem: function () {
      let url =
        this.otherUrl +
        "/lexus_php/web/report_export.php?queryParams=" +
        this.inputValue +
        "&localArea=" +
        this.areaValue +
        "&startTime=" +
        this.startTime +
        "&endTime=" +
        this.endTime;
      window.open(url);
    },
  },
  created() {
    let req = {
      Page: this.currentPage,
      Rows: this.pageSize,
    };
    //this.firmsLinkUpload(req);
    this.getPageTableDate(req);
    this.getAreaList();
  },
};
</script>

<style scoped lang="less">
.upload_links {
  .header {
    padding: 10px;
    border: 1px solid #ccc;
    .search {
      display: flex;
      align-items: center;
      .input {
        background-color: #fff;
        border: 1px solid #ccc;
        padding: 2px;
        display: flex;
        img {
          width: 22px;
          height: 22px;
          border: 1px solid #ccc;
        }
        input {
          background-color: #fff;
          border: 1px solid #ccc;
          margin-left: 3px;
          color: #555;
          font-size: 12px;
          height: 18px;
          margin-top: 2px;
        }
      }
    }
    .area {
      margin-left: 20px;
      display: flex;
      height: 28px;
      line-height: 28px;
      select {
        margin-left: 10px;
        height: 28px;
        width: 120px;
        font-size: 12px;
        color: #555;
        border: 1px solid #ccc;
        option {
          font-size: 12px;
          border: 1px solid #ccc;
        }
      }
    }
    .time {
      display: flex;
      margin-left: 20px;
      p {
        height: 28px;
        line-height: 28px;
        margin-right: 10px;
      }
      input {
        height: 26px;
        padding: 0;
        outline: none;
        border: 1px solid #ccc;
        margin-right: 10px;
      }
      span {
        height: 28px;
        line-height: 28px;
        margin-right: 10px;
      }
      .date_pick{
        margin-top: 5px;
      }
    }
  }
  .count {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    p {
      margin-top: 10px;
      font-weight: 600;
    }
  }
  .tableBox {
    width: 1030px;
    overflow: auto;
    text-align: center;
  }
}
.inputFocus {
  outline: none;
  border: 1px solid #ccc;
}
.inputFocus:focus {
  animation: shadowAni 200ms linear forwards;
}
@keyframes shadowAni {
  0% {
    border-color: #cccccc;
    box-shadow: inset 0px 0px 0 #ccc;
  }
  100% {
    border-color: #75b9f0;
    box-shadow: 0px 0px 5px #75b9f0;
  }
}
</style>
