<template>
  <div class="priceRecord">
    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row style="padding: 0 0 0 80px">
            <el-col :span="6" style="width: 303px">
              <period-date-picker
                :type="1"
                :width="'135px'"
                :size="searchSize"
                :start="parmValue.start"
                :end="parmValue.end"
                @timeReturned="handleTime"
              />
            </el-col>
            <el-col :span="4" style="width: 200px">
              <search-metal-kind
                :value="parmValue.type"
                :disabled="false"
                :isDetail="false"
                :size="searchSize"
                :placeholder="'金属类型'"
                @searchChange="noble_metalsearchChange"
              />
            </el-col>

            <el-col
              :span="3"
              class="fr"
              style="width: 66px; padding: 0 0 0 10px"
            >
              <el-button type="warning" :size="searchSize" @click="restSearch">
                重置
              </el-button>
            </el-col>
          </el-row>
          <el-row style="padding: 10px 0 0 0px">
            <el-col :span="4" style="width: 170px; padding: 0 0 0 0px">
              <el-input
                :size="searchSize"
                v-model="parmValue.action_name"
                :maxlength="40"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="创建人"
              ></el-input>
            </el-col>
            <el-col :span="4" style="width: 170px; padding: 0 0 0 10px">
              <el-input
                :size="searchSize"
                v-model="parmValue.company_name"
                :maxlength="40"
                @blur="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="创建人部门"
              ></el-input>
            </el-col>
            <el-col
              :span="3"
              class="fr"
              style="width: 66px; padding: 0 0 0 10px"
            >
              <el-button type="primary" :size="searchSize" @click="searchList">
                刷新
              </el-button>
            </el-col>
          </el-row>
        </div></template
      >
    </ex-table>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/goodStore/goldPrice";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
export default {
  name: "priceRecord",
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
  },
  data() {
    return {
      loading: true,
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        start: "",
        end: "",
        company_name: "", //创建人部门
        type: "",
        action_name: "",
      },

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "id",
          label: "ID",
          width: "70",
        },
        {
          prop: "type_cn",
          label: "金属种类",
        },
        {
          prop: "price",
          label: "当前金价(元/g)",
        },
        {
          prop: "company_name",
          label: "创建人部门",
          minWidth: "150px",
        },
        {
          prop: "action_name",
          label: "创建人",
        },
        {
          prop: "addtime",
          label: "创建时间",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        company_name: "", //创建人部门
        size: 15, // 每页显示条数
        start: "",
        end: "",
        type: "",
        action_name: "",
      };
      this.searchList();
    },

    // 时间
    async handleTime(e) {
      this.parmValue.start = e.startTime || "";
      this.parmValue.end = e.endTime || "";
      if (
        (this.parmValue.start !== "" && this.parmValue.end !== "") ||
        (this.parmValue.start === "" && this.parmValue.end === "")
      ) {
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    //贵金属种类选择
    async noble_metalsearchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.parmValue.type = id;
      } else {
        this.parmValue.type = "";
      }
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },

    // 刷新表格
    async searchList() {
      if (
        (this.parmValue.start !== "" && this.parmValue.end === "") ||
        (this.parmValue.start === "" && this.parmValue.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      const res = await asyncRequest.ecordList(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
