<template>
  <div>
    <el-card className="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="单号:">
              <el-input v-model="filterQuery.billNo" placeholder="单号"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item class="query-form-item" label="第三方单据号:">
              <el-input v-model="filterQuery.thirdBillNo" placeholder="第三方单据号"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item class="query-form-item" label="提交时间:">
              <el-date-picker
                v-model="filterQuery.submitTime"
                type="date"
                placeholder="请选择提交时间"
                :picker-options="pickerOptions"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item class="query-form-item" label="处理状态:">
              <el-select v-model="filterQuery.status" placeholder="请选择处理状态" style="width: 90%">
                <el-option label="全部" value=""></el-option>
                <el-option label="提交成功" value="2"></el-option>
                <el-option label="提交失败" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="单据日期:">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="onReset"
          >重置
          </el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit"
          >查询
          </el-button
          >
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%"
                highlight-current-row
                border>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="单据号" prop="billNo" show-overflow-tooltip></el-table-column>
        <el-table-column label="第三方单据号" prop="thirdBillNo" show-overflow-tooltip></el-table-column>
        <el-table-column label="单据类型" prop="billTypeName" width="220"></el-table-column>
        <el-table-column label="单据日期" prop="billDate" width="200"></el-table-column>
        <el-table-column label="提交时间" prop="submitTime" width="200"></el-table-column>
        <el-table-column label="提交状态" prop="exportStatus" width="100">
          <template slot-scope="scope">
            <el-tag :type="(scope.row.status ) | statusFilterType">{{
                statusMap[scope.row.status]
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="结果信息" prop="result" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="showResultInfo(scope.row)"
            >详情
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteLog(scope.row.id)"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>

    <el-dialog
      title="结果详情"
      width="60%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="submitResultVisible"
    >
      <span v-text="resultInfo" style="white-space:pre-line;" class="resultInfo"></span>
    </el-dialog>

  </div>
</template>

<script>
import {getUploadLogList, deleteUploadLog} from "@/api/thrsys/thrOrderUploadLog";

const formJson = {
  site_id: "",
  site_name: "",
  describe: "",
  ads: [],
};

export default {
  name: "uploadLog",
  data() {
    return {
      showSearch: true,
      filterQuery: {
        billNo: null,
        thirdBillNo: null,
        submitTime: null,
        status: null,
        fromType: 1,
        page: 1,
        limit: 20
      },
      loading: false,
      list: [],
      total: 0,
      statusMap: {
        0: "未提交",
        1: "正在提交",
        2: "提交成功",
        3: "提交失败"
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      submitResultVisible: false,
      resultInfo: null
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.filterQuery = {
        billNo: null,
        thirdBillNo: null,
        submitTime: null,
        status: null,
        fromType: 1,
        page: 1,
        limit: 20
      }
      this.onSubmit();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getUploadLogList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    showResultInfo(row) {
      this.resultInfo = row.result;
      this.submitResultVisible = true;
    },
    deleteLog(id) {
      this.$confirm('是否确认删除提交记录？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: id};
        deleteUploadLog(params).then((res) => {
          if (res.code === 20000) {
            this.$message.success("删除成功");
            this.getList();
          }
        }).catch((error) => {
          this.$message.error("删除失败");
        })
      }).catch(() => {
        this.$message.info("已取消删除");
      })
    },
  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        false: "success",
        true: "warning",
      };
      return statusMap[status];
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>

