<template>
  <div class="content-main">
    <el-form :inline="true" :model="searchForm" class="search-bm-boder">
      <el-row>
        <el-form-item label="企业名称:">
          <el-input
            clearable
            v-model="searchForm.cjdwmc"
            placeholder="请输入企业名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业分级:">
          <!-- <el-input clearable v-model="searchForm.qyfj"></el-input> -->
          <el-select
            clearable
            v-model="qyfj"
            @change="handleQYFJ"
            placeholder="请选择企业分级"
          >
            <el-option
              v-for="(item, index) in grades"
              :key="index"
              :label="item.grade"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <div class="search-button">
            <span class="search-line"></span>
            <el-button
              type="primary"
              @click="doSearch"
              size="small"
              class="iconfont iconchaxun"
              >查询</el-button
            >
            <el-button
              type="primary"
              @click="
                searchForm = {};
                qyfj = null;
                doSearch();
              "
              size="small"
              class="iconfont iconzhongzhi"
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row>
      <el-table
        :data="tableData"
        stripe
        v-loading="listLoading"
        border
        fit
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="77"
        ></el-table-column>
        <el-table-column
          prop="cjdwmc"
          label="企业名称"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="hxdf"
          label="总得分"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="qyfj"
          label="企业分级"
          align="center"
        ></el-table-column>
        <!-- <el-table-column prop="nsdj" label="纳税等级" align="center"></el-table-column> -->
        <el-table-column
          prop="cjrq"
          label="最近一次成交日期"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                @click="
                  $router.push({
                    path: 'detail',
                    query: { cjdwbm: scope.row.cjdwbm },
                  })
                "
                class="operate_show_btn iconfont iconchakan2"
                size="small"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageForm.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageForm.total"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import { list } from "@/api/transaction/creditAssess/creditquery/index";
import { getconfig } from "@/api/transaction/creditAssess/creditEvaluationRules/index";
export default {
  data() {
    return {
      qyfj: "",
      searchForm: {
        cjdwmc: null,
        hxdfStart: null,
        hxdfEnd: null,
        // qyfj: null,
        operaterType: 1,
      },
      tableData: [],
      listLoading: false,
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      grades: [],
      values: [],
    };
  },
  created() {
    this.initOpt();
  },
  methods: {
    handleQYFJ(val) {
      if (val == 0) {
        this.searchForm.hxdfStart = this.values[val];
        this.searchForm.hxdfEnd = null;
      } else if (val == this.values.length) {
        this.searchForm.hxdfStart = null;
        this.searchForm.hxdfEnd = this.values[val - 1];
      } else {
        this.searchForm.hxdfStart = this.values[val - 1];
        this.searchForm.hxdfEnd = this.values[val];
      }
    },
    initOpt() {
      getconfig(1045).then((res) => {
        if (res.infoCode != 200) return;
        if (res.data.length) {
          let d = res.data[0];
          try {
            this.grades = JSON.parse(d.configNameEn);
            let values = JSON.parse(d.configName);
            this.values = values.map((d) => d.v);
          } catch (err) {
          } finally {
            this.getList("start");
          }
        }
      });
    },
    doSearch() {
      this.pageForm.pageNum = 1;
      this.getList();
    },
    getList(type) {
      // this.searchForm.qyfj = this.qyfj;
      if (type != "start") {
        //非初始化调用：查询、分页
        sessionStorage.setItem("searchData", JSON.stringify(this.searchForm));
        sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
      } else {
        //初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
        if (JSON.parse(sessionStorage.getItem("searchData"))) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          this.searchForm = JSON.parse(sessionStorage.getItem("searchData"));
          this.qyfj = this.searchForm.qyfj;
          delete this.searchForm.qyfj;
          this.pageForm = JSON.parse(sessionStorage.getItem("pageData"));
        } else {
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(this.searchForm));
          sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
        }
      }
      this.listLoading = true;
      let { pageNum, pageSize } = this.pageForm;
      list({ ...this.searchForm, pageNum, pageSize })
        .then((res) => {
          this.listLoading = false;
          if (res.infoCode === 200) {
            let list = res.data.items;
            let end = this.values.length;
            list.forEach((d) => {
              let n = d.hxdf;
              let values = [...this.values];
              values.push(n);
              values = values.sort();
              values = values.sort(function (a, b) {
                if (a > b) {
                  return 1;
                } else if (a < b) {
                  return -1;
                } else {
                  return 0;
                }
              });
              console.log(values, "sort");
              let i = values.indexOf(n);
              let j = values.lastIndexOf(n);
              console.log(i, j, n, values);
              if (i == 0) {
                d.qyfj = this.grades[i].grade;
              }
              if (i != j) {
                d.qyfj = this.grades[i].grade;
                console.log(d.qyfj, "d.qyfj");
              } else {
                d.qyfj = this.grades[j].grade;
                console.log(d.qyfj, "d.qyfj");
              }
              // console.log(d.qyfj, "d.qyfj");
            });
            this.tableData = [...list];
            console.log(this.tableData, "this.tableData");
            this.pageForm.total = res.data.total;
          }
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
    handleSizeChange(val) {
      this.pageForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val;
      this.getList();
    },
  },
};
</script>
