<template>
  <div>
    <div class="upload_record" v-if="!isShowDetail">
      <!--   <h1>资料列表</h1> -->
      <div class="search HeadLeft">
        <div class="input">
          <img src="../../img/search.png" />
          <input
            type="text"
            name=""
            id=""
            placeholder="请输入要搜索的资料名"
            v-model="searchName"
          />
        </div>
        <button @click="searchByName">搜索</button>
      </div>
      <!--    <div class="searchBox">
        <div class="search_inside">
          <span><img src="" alt="" /></span>
          <input type="text" name="" id="" placeholder="请输入要搜索的资料名" />
          <button>搜索</button>
        </div>
      </div> -->
      <div class="sortBox">
        <div class="sort_inside">
          <span>是否下载</span>
          <select name="" id="isDownload" v-model="downloadSortVal">
            <option value="">不限</option>
            <option value="already">已下载</option>
            <option value="notyet">未下载</option>
          </select>
          <span>是否反馈</span>
          <select name="" id="isReturn" v-model="reportSortVal">
            <option value="">不限</option>
            <option value="already">已反馈</option>
            <option value="notyet">未反馈</option>
          </select>
          <span>排序</span>
          <select name="" id="timeSort" v-model="timeSortVal">
            <option value="">时间降序</option>
            <option value="asc">时间升序</option>
          </select>
        </div>
        <button @click="searchByCondition">确定</button>
      </div>
      <div class="tableD">
        <div class="table_template">
          <table class="table">
            <thead>
              <tr :style="tableHeadStyle">
                <td
                  v-for="(item, index) in tableHeaderD"
                  :key="index"
                  :style="tableHeadStyle"
                >
                  {{ item }}
                </td>
              </tr>
            </thead>
            <tbody>
              <tr
                v-for="(obj, index) in tableData"
                :key="index"
                :class="{ table_gray: !discolor && index % 2 === 0 }"
              >
                <!--  <td v-for="(item, i) in obj" :key="i" :style="trStyle"> -->
                <td>{{ obj.informationName || "北京博瑞" }}</td>
                <td>{{ obj.filePulishTime || "2021/02" }}</td>
                <td>{{ obj.accountScope === 1 ? "共通" : "部分" }}</td>
                <td>{{ obj.fileUploadDate || "2021/01/10 12:12" }}</td>
                <td>{{ obj.download > 0 ? "已下载" : "未下载" }}</td>
                <td>{{ obj.report === "1" ? "已反馈" : "未反馈" }}</td>

                <td v-if="operation" :style="trStyle" class="operationStyle">
                  <span
                    v-for="(operationObj, i) in operation"
                    :key="i"
                    @click="
                      operationObj.function(obj, index);
                      nowIndex = index;
                    "
                    >{{ operationObj.name }}
                  </span>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
      <div class="pageD">
        <TablePage
          :totalPage="totalPage"
          :currentPage="currentPage"
          @change_page="changePage"
          @jump_page="jumpPage"
        />
        <p>共{{ totalPage }}页，共{{ sum }}条数据</p>
      </div>
    </div>
    <div v-else>
      <Detail></Detail>
      <!-- <Detail
        :detailData="tableData"
        :nowIndex="nowIndex"
        :fileData="tableFileData"
      ></Detail> -->
    </div>
  </div>
</template>

<script>
import TablePage from "../../components/TablePage";
import Detail from "./components/InfoListItemDetail";
export default {
  props: {
    isManufacturer: {
      type: String,
      default: "distributor",
    },
    detailVisible: {
      type: Boolean,
      default: true,
    },
  },
  components: {
    TablePage,
    Detail,
  },
  data() {
    return {
      nowIndex: 0,
      searchName: "",
      timeSortVal: "",
      reportSortVal: "",
      downloadSortVal: "",
      //initialPage: true,
      tableHeaderD: [
        "资料名称",
        "素材时间",
        "经销商范围",
        "上传时间",
        "下载状态",
        "反馈状态",
        "操作",
      ],
      operation: [
        {
          name: "下载",
          function: (obj, i) => {
            console.log(obj, i);
            let config = {
              url: "/dealerDownload",
              data: {
                informationId: obj.id,
              },
            };
            this.downloadPost(config);
          },
        },
        {
          name: "查看详情>",
          function: (obj, i) => {
            let data = {
              informationId: obj.id,
            };
            this.showDetail(data, i);
            console.log(obj, i);
          },
        },
      ],
      // 表格配置
      sum: 10, // 一共有多少条数据
      pageSize: 20, // 每页展示的数据
      discolor: false, // flase是隔行变色
      currentPage: 1,
      tableData: [
        {
          informationName: "北京博瑞",
          filePulishTime: "2021/02",
          accountScope: "传播类型1",
          fileUploadDate: "",
          download: "已下载",
          report: "未反馈",
          asc: "false",
        },
      ],
      tableFileData: [],
      tableHeadStyle: {
        background: "#848484",
        height: "30px",
        color: "white",
      },
      trStyle: {
        minwidth: "150px",
        height: "30px",
        overflow: "hidden",
      },
      onlineUrl: "http://8.136.230.133:8080",
      userId: 254,
    };
  },
  computed: {
    // 表格总页数
    totalPage() {
      return Math.ceil(this.sum / this.pageSize);
    },
    isShowDetail() {
      return this.$route.query && this.$route.query.tag ? true : false;
    },
  },
  methods: {
    feedback: () => {
      alert("反馈");
    },
    download: function () {
      alert("下载");
    },
    // 获取某一页面的数据，展示在表格
    changePage: function (page) {
      this.currentPage = page;
      console.log(page);
    },
    // 点击上一页，下一页，首页，尾页
    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;
      }
      console.log(this.currentPage);
      this.getDataListPage(this.currentPage);
    },
    //下载文件
    downloadPost: function (config) {
      console.log("下载文件:", config);
      let url =
        this.onlineUrl +
        "/dealerDownload?informationId=" +
        config.data.informationId;
      window.open(url);
      /*   return new Promise((resolve, reject) => {
        this.$http({
          url: config.url,
          method: "get",
          params: config.data,
        })
          .then((res) => {
            console.log(res);
            resolve(res);
            let link = document.createElement("a");
            link.href = window.URL.createObjectURL(new Blob([res.data]));
            link.target = "_blank";
            //文件名和格式
            link.download = "文件模板.xlsx";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          })
          .catch((err) => {
            reject(err);
          });
      }); */
    },
    //查看详情
    showDetail: function (config, i) {
      console.log(config);
      /*  //获取附件数据
      this.$http({
        url: "/selectInformationFileList",
        method: "post",
        data: config,
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.tableFileData = res.data.data;
          }
        })
        .catch((err) => {
          console.log(err);
        }); */
      this.$router.push({
        query: {
          tag: "infoListDetail",
          informationId: config.informationId,
          nowIndex: i,
          agentId: this.userId,
          Page: this.currentPage,
          Rows: this.pageSize,
        },
      });
    },
    //获取资料列表
    getDataList: function () {
      this.$http({
        url: "/firmsUploadListByAgent",
        method: "post",
        data: {
          agentId: this.userId,
          Page: this.currentPage,
          Rows: this.pageSize,
        },
      })
        .then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            this.tableData = res.data.data;
            this.sum = res.data.count;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //分页 获取资料列表
    getDataListPage: function (nowpage) {
      this.$http({
        url: "/firmsUploadListByAgent",
        method: "post",
        data: {
          agentId: this.userId,
          Page: nowpage,
          Rows: this.pageSize,
        },
      })
        .then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            this.tableData = res.data.data;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //条件筛选 获取新的资料列表
    newGetDataList: function (config) {
      console.log("new Getdata1", config);
      this.$http({
        url: "/firmsUploadListByAgent",
        method: "post",
        data: config,
      })
        .then((res) => {
          console.log("new Getdata2", res);
          if (res.data.code === 200) {
            this.tableData = res.data.data;
            this.sum = res.data.count;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //搜索资料名
    searchByName: function () {
      console.log("insearchByName", this.searchName);
      let searchName = this.searchName;
      let data = {
        informationName: searchName,
      };
      this.newGetDataList(data);
    },

    searchByCondition: function () {
      console.log("insearchByCondition", this.searchName);
      let asc = this.timeSortVal === "asc" ? false : true;
      let report = "";
      if (this.reportSortVal === "already") {
        report = this.reportSortVal === "already" ? true : false;
      } else if (this.reportSortVal === "notyet") {
        report = this.reportSortVal === "notyet" ? false : true;
      }
      let download = "";
      if (this.downloadSortVal === "already") {
        download = this.downloadSortVal === "already" ? true : false;
      } else if (this.downloadSortVal === "notyet") {
        download = this.downloadSortVal === "notyet" ? false : true;
      }
      let data = {
        asc: asc,
        report: report,
        download: download,
        Page: 1,
        Rows: this.pageSize,
        agentId: this.userId,
      };
      this.newGetDataList(data);
    },
  },
  mounted() {
    let localUserId = localStorage.getItem("userId");
    if (localUserId) {
      console.log("经销商ID", localUserId);
      this.userId = localUserId;
    } else {
      console.log("经销商ID 254");
      this.userId = 254;
    }
    this.getDataList();
  },
};
</script>

<style scoped lang="less">
.searchBox {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  margin-left: 40px;
}

.sortBox {
  display: flex;
  justify-content: flex-end;
  height: 40px;
  align-items: center;
}
.sort_inside {
  display: flex;
  width: 400px;
  justify-content: space-between;
}
/* 查看详情样式 */
.table_template {
  text-align: center;
  .table {
    background-color: #fff;
    border-collapse: collapse;
    border: none;
    width: 100%;
    td {
      border: 1px solid #ccc;
    }
    span {
      &:hover {
        cursor: pointer;
      }
    }
  }
}
.table_gray {
  background-color: #eeeeee;
}
.operationStyle span {
  color: #0000ff;
}
.operationStyle {
  color: #0000ff;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
/* 搜索栏 */
.search {
  padding: 10px 15px 10px 10px;
  display: flex;
  align-items: center;
  .input {
    background-color: #fff;
    padding: 2px;
    display: flex;
    align-items: center;
    margin-left: 16px;
    border: 1px solid #ccc;
    img {
      width: 28px;
      height: 28px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
    input {
      background-color: #fff;
      border: 1px solid #ccc;
      margin-left: 4px;
      color: #555;
      font-size: 12px;
      height: 24px;
    }
  }
}
.HeadLeft {
  border: 1px solid #ccc;
}
.pageD {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 10px;
}
.pageD p {
  margin-left: 16px;
}
</style>
