<template>
  <div class="searchSort pagePadding">
    <div
      v-if=" powers.some((i) => i == '001')"
    >
      <ex-table
        :table="table"
        v-loading="loading"
        :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 10px 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: 160px">
                <el-select
                  v-model="parmValue.status"
                  filterable
                  clearable
                  :size="searchSize"
                  placeholder="分类状态"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in statusList"
                    :key="'status' + item.code"
                    :label="item.name"
                    :value="item.code"
                  />
                </el-select>
              </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-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>
              <el-col :span="4" style="width: 373px">
                <el-input
                  :size="searchSize"
                  v-model="parmValue.cat_name"
                  :maxlength="40"
                  @blur="
                    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.creater"
                  :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"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                  placeholder="创建人部门"
                ></el-input>
              </el-col>
            </el-row></div
        ></template>
        <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.status == '0' ? 'warning' : ''"
            v-text="
              (statusOptions.find((item) => item.id == scope.row.status) || {})
                .label || '--'
            "
          ></el-tag>
        </template>
        <template #operation="{ scope }">
          <el-tooltip
            v-if="powers.some((i) => i == '061')"
            effect="dark"
            content="查看毛利率"
            placement="top"
          >
            <i class="el-icon-view tb-icon" @click="openModal(scope.row)"></i>
          </el-tooltip>
        </template>
      </ex-table>
      <!-- 弹窗 新增/修改 -->
      <add-edit
        :id="modelId"
        :show-model="showModel"
        :sitem="sitem"
        @refresh="searchList"
        @cancel="showModel = false"
      />
    </div>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/goodStore/searchSort";
import { statusList } from "@/assets/js/statusList";
import roleLevel from "@/assets/js/roleLevel";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import addEdit from "./addEdit";
export default {
  name: "searchSort",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "searchSort"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      statusList: statusList,
      roleLevel: roleLevel,
      loading: true,
      showModel: false,
      modelId: "000",
      sitem: {},
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        start: "",
        end: "",
        status: "",
        creater: "",
        company_name: "", //创建人部门
        cat_name: "",
      },
      // 状态
      statusOptions: [
        { id: "0", label: "禁用" },
        { id: "1", label: "启用" },
      ],
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        // {
        //   prop: "id",
        //   label: "ID",
        //   width: "80px",
        // },
        {
          prop: "cat_name",
          label: "分类名称",
          width: "140px",
        },
        {
          prop: "status",
          label: "状态",
          _slot_: "status",
          width: "70px",
        },
        {
          prop: "level",
          label: "分类等级",
          width: "70px",
        },
        {
          prop: "fund_code",
          label: "财务核算码",
          width: "130px",
        },
        {
          prop: "specs",
          label: "关联规格",
          width: "130px",
        },
        {
          prop: "cat_desc",
          label: "售后说明",
        },
        {
          prop: "company_name",
          label: "创建人部门",
          minWidth: "150px",
        },
        {
          prop: "creater",
          label: "创建人",
          width: "70px",
        },

        {
          prop: "addtime",
          label: "创建时间",
          sortable: true,
          width: "145px",
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          width: "50px",
          _noset_: true,
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        start: "",
        end: "",
        status: "",
        company_name: "", //创建人部门
        creater: "",
        cat_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();
      }
    },
    // 新建/编辑/详情
    openModal(row) {
      this.sitem = row;
      this.modelId = row.id;
      this.showModel = true;
    },

    // 刷新表格
    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.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.forEach((e) => {
          if (e.im && e.im.length > 0) {
            e.specs = "";
            e.im.forEach((a, i) => {
              e.specs += i === 0 ? a.spec_name : `,${a.spec_name}`;
            });
          } else {
            e.specs = "";
          }
        });
        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>
