<template>
  <div class="sort pagePadding">
    <ex-table
      v-loading="loading"
      v-if=" powers.some((i) => i == '001')"
      :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 10px 90px">
            <el-col :span="3" style="width: 748px">
              <div class="my-breadcrumb clear">
                <div
                  class="my-breadcrumb-item"
                  v-for="(item, index) in titleList"
                  :key="item + index"
                  @click="changeBreadcrumb(index)"
                >
                  <span class="label">{{ item.titleName }}</span
                  ><span class="separator" v-if="index + 1 !== titleList.length">/</span>
                </div>
              </div>
            </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: 140px">
              <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="4" style="width: 390px; padding: 0 0 0 10px">
              <search-terrace
                :value="parmValue.platform_id"
                :disabled="false"
                :size="'mini'"
                :isDetail="false"
                :is_show="'0'"
                :placeholder="'平台名称'"
                @searchChange="platform_codeChange"
              />
            </el-col>
            <el-col :span="4" style="width: 390px; padding: 0 0 0 10px">
              <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.company_name"
                :maxlength="40"
                @blur="
                  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 #order_rate="{ scope }">
        <div>
          {{
            private_field &&
            private_field.length > 0 &&
            private_field.some((item) => item == "3")
              ? scope.row.order_rate
              : "****"
          }}
        </div>
      </template>
      <template #sale_rate="{ scope }">
        <div>
          {{
            private_field &&
            private_field.length > 0 &&
            private_field.some((item) => item == "3")
              ? scope.row.sale_rate
              : "****"
          }}
        </div>
      </template>
      <template #rate="{ scope }">
        <div>
          {{
            private_field &&
            private_field.length > 0 &&
            private_field.some((item) => item == "3")
              ? scope.row.rate
              : "****"
          }}
        </div>
      </template>
      <template #money_rate="{ scope }">
        <div>
          {{
            private_field &&
            private_field.length > 0 &&
            private_field.some((item) => item == "3")
              ? scope.row.money_rate
              : "****"
          }}
        </div>
      </template>
      <template #low_rate="{ scope }">
        <div>
          {{
            private_field &&
            private_field.length > 0 &&
            private_field.some((item) => item == "3")
              ? scope.row.low_rate
              : "****"
          }}
        </div>
      </template>

      <template #operation="{ scope }">
        <el-tooltip
          v-if="powers.some((i) => i == '005')"
          effect="dark"
          content="修改毛利率"
          placement="top"
        >
          <i class="el-icon-s-check tb-icon" @click="setRate(scope.row)"></i>
        </el-tooltip>
        <el-tooltip
          v-if="parseInt(scope.row.level) < 3"
          effect="dark"
          content="下级分类"
          placement="top"
        >
          <i class="el-icon-arrow-right tb-icon" @click="openChildren(scope.row)"></i>
        </el-tooltip>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>

    <!-- 弹窗 新增/修改 -->
    <add-edit-rate
      :id="modelRateId"
      :show-model="showRateModel"
      :sitem="sitemRate"
      :titleList="titleList"
      @refresh="searchList"
      @cancel="showRateModel = false"
    />
  </div>
</template>
<script>
import addEditRate from "./addEditRate";
import asyncRequest from "@/apis/service/youzan/othsearchSort";
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";
export default {
  name: "othsearchSort",
  mixins: [mixinPage, resToken],
  components: {
    addEditRate,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size", "private_field"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find((i) => i.menu_route == "othsearchSort") ||
        {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      statusList: statusList,
      roleLevel: roleLevel,
      loading: false,
      sitem: {},
      showRateModel: false,
      modelRateId: "000",
      sitemRate: {},
      titleList: [],
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        status: "",
        platform_id: "",
        cat_name: "",
        pid: "0",
      },
      // 状态
      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",
        //   "min-width": "100px",
        // },
        {
          prop: "cat_name",
          label: "分类名称",
          minwidth: "130px",
        },
        {
          prop: "level",
          label: "等级",
          width: "50px",
        },
        {
          prop: "status",
          label: "状态",
          _slot_: "status",
          width: "70px",
        },
        {
          prop: "platform_name",
          label: "平台名称",
          minwidth: "190px",
        },

        {
          prop: "fund_code",
          label: "财务核算码",
          width: "150px",
        },

        // {
        //   prop: "order_rate",
        //   label: "预算→成本",
        //   _slot_: "order_rate",
        //   width: "90px",
        // },
        {
          prop: "sale_rate",
          label: "成本→售价",
          _slot_: "sale_rate",
          width: "90px",
        },
        // {
        //   prop: "rate",
        //   label: "主管利率",
        //   _slot_: "rate",
        //   width: "90px",
        // },
        // {
        //   prop: "money_rate",
        //   label: "财务利率",
        //   _slot_: "money_rate",
        //   width: "90px",
        // },
        // {
        //   prop: "low_rate",
        //   label: "项目毛利",
        //   _slot_: "low_rate",
        //   width: "90px",
        // },
        {
          prop: "creater",
          label: "创建人",
          width: "70px",
        },
        {
          prop: "addtime",
          label: "创建时间",
          width: "145px",
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          width: "110px",
          _noset_: true,
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.initTitleList();
    this.parmValue.pid = "0";
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        status: "",
        cat_name: "",
        platform_id: "",
        pid: "0",
      };
      this.initTitleList();
      this.searchList();
    },
    async platform_codeChange(e) {
      const { id } = e;
      this.parmValue.platform_id = id || "";
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
    initTitleList() {
      this.titleList = [];
      let model = { id: "0", titleName: "全部一级分类" };
      this.titleList.push(model);
    },
    //查看下一级分类
    async openChildren(item) {
      this.parmValue.pid = item.id;
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
      let model = {
        id: item.id,
        titleName: item.cat_name,
      };
      this.titleList.push(model);
    },
    //设置毛利率
    setRate(row) {
      this.modelRateId = row.id;
      this.sitemRate = row;
      this.showRateModel = true;
    },
    async changeBreadcrumb(index) {
      if (index + 1 === this.titleList.length) {
        return;
      } else {
        this.parmValue.pid = this.titleList[index].id;
        this.titleList = this.titleList.slice(0, index + 1);
      }
      // if (this.titleList.length === 1) {
      //   this.titleList = [];
      // }
      this.parmValue.cat_name = "";
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },

    // 刷新表格
    async searchList() {
      if (this.loading) return;
      if (this.parmValue.platform_id === "") {
        this.$message.warning("请选择平台！");
        return;
      }
      this.loading = true;
      const { code, data } = await asyncRequest.list(this.parmValue);
      if (code === 0) {
        const { list, count } = data ?? {};
        this.tableData = list ?? [];
        this.pageInfo.total = count ?? 0;
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.sort {
  .my-breadcrumb {
    .my-breadcrumb-item {
      float: left;
      span {
        height: 32px;
        line-height: 32px;
      }
      .label {
        padding: 0 5px 0 0;
        color: 010101;
      }
      .separator {
        color: #787878;
        font-size: 13px;
        padding: 0 5px 0 0;
      }
      &:last-child {
        .label {
          color: #787878;
        }
      }
      &:hover {
        cursor: pointer;
      }
      &:last-child {
        cursor: default;
      }
      &:only-child {
        cursor: pointer;
      }
      // &.chover {
      //   cursor: pointer;
      // }
    }
  }
}
</style>
