<template>
  <div class="divBox">
    <div class="selCard">
      <el-form
        size="small"
        :model="tableFrom"
        ref="searchForm"
        label-width="85px"
        inline
      >
        <el-form-item label="时间选择：" prop="date">
          <el-date-picker
            v-model="timeVal"
            value-format="yyyy/MM/dd"
            format="yyyy/MM/dd"
            type="daterange"
            placement="bottom-end"
            placeholder="自定义时间"
            :picker-options="pickerOptions"
            style="width: 280px"
            @change="onchangeTime"
          />
        </el-form-item>
        <el-form-item label="操作端：">
          <el-select
            v-model="tableFrom.type"
            placeholder="请选择"
            class="selWidth mr10"
            clearable
            filterable
            @change="getList(1)"
          >
            <el-option label="平台端" value="1" />
            <el-option label="商户端" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品id:" prop="nickname">
          <el-input
            v-model="tableFrom.id"
            @keyup.enter.native="getList(1)"
            placeholder="请输入id"
            class="selWidth"
            clearable
          />
        </el-form-item>
        <el-form-item label="操作人:" prop="nickname">
          <el-input
            v-model="tableFrom.operator_nickname"
            @keyup.enter.native="getList(1)"
            placeholder="请输入操作人"
            class="selWidth"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)"
            >搜索</el-button
          >
          <el-button size="small" @click="searchReset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="mt14">
      <el-button class="mb20" type="primary" size="small" @click="exports"
        >导出列表</el-button
      >
      <el-table
        border
        ref="productRecords"
        :data="tableData.data"
        row-key="operate_log_id"
        size="small"
        v-loading="listLoading"
        class="ones"
      >
        <el-table-column
          align="center"
          label="序号"
          min-width="60"
          prop="operate_log_id"
        />
        <el-table-column align="center" label="操作记录" min-width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.category_name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作端" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.type }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作角色" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.operator_role_nickname }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作人" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.operator_nickname }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作时间"
          min-width="100"
          prop="create_time"
        />
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { operateRecordList, exportOperate } from "@/api/product";
import timeOptions from "@/utils/timeOptions";
import createWorkBook from "@/utils/newToExcel.js";
export default {
  name: "ProductComment",
  data() {
    return {
      pickerOptions: timeOptions,
      tableData: {
        data: [],
        total: 0,
      },
      timeVal: [],
      listLoading: true,
      tableFrom: {
        page: 1,
        limit: 20,
        type: "",
        date: "",
      },
    };
  },
  mounted() {
    this.getList(1);
  },
  methods: {
    async exports() {
      let excelData = JSON.parse(JSON.stringify(this.tableFrom)),
        data = [];
      excelData.page = 1;
      excelData.limit = 100
      let pageCount = 1;
      let lebData = {};
      for (let i = 0; i < pageCount; i++) {
        lebData = await this.downData(excelData);
        pageCount = Math.ceil(lebData.count / excelData.limit);
        if (lebData.export.length) {
          data = data.concat(lebData.export);
          excelData.page++;
        }
      }
      createWorkBook(
        lebData.header,
        lebData.title,
        data,
        lebData.foot,
        lebData.filename
      );
      return;
    },
    /**订单列表 */
    downData(excelData) {
      return new Promise((resolve, reject) => {
        let id = this.tableFrom.id;
        exportOperate(id ? id : "0", excelData).then((res) => {
          return resolve(res.data);
        });
      });
    },
    /**重置 */
    searchReset() {
      this.timeVal = [];
      this.tableFrom.date = "";
      this.$refs.searchForm.resetFields();
      this.getList(1);
    },
    // 选择时间
    selectChange(tab) {
      this.tableFrom.date = tab;
      this.timeVal = [];
      // this.tableFrom.page = 1;
      this.getList(1);
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      // this.tableFrom.date = this.timeVal ? this.timeVal.join("-") : "";
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      // this.tableFrom.page = 1;
      this.getList(1);
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      let id = this.tableFrom.id;
      operateRecordList(id ? id : "0", this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-rate {
  margin-top: 9px;
}
</style>
