<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">
              <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-row></div
      ></template>

      <template #companyNo>
        <div>{{ getCompanyWithCode(currentCompany).code }}</div>
      </template>

      <template #companyName>
        <div>{{ getCompanyWithCode(currentCompany).name }}</div>
      </template>

      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '1' ? '' : '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
              : "****"
          }} -->
          {{displayPrivateField(isDisplayPrivateField('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
              : "****"
          }} -->
          {{displayPrivateField(isDisplayPrivateField('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
              : "****"
          }} -->
          {{displayPrivateField(isDisplayPrivateField('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
              : "****"
          }} -->
          {{displayPrivateField(isDisplayPrivateField('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
              : "****"
          }} -->
          {{displayPrivateField(isDisplayPrivateField('3'), scope.row.low_rate)}}
        </div>
      </template>

      <template #operation="{ scope }">
        <el-tooltip
          v-if="powers.some((i) => i == '007')"
          effect="dark"
          content="查看"
          placement="top"
        >
          <i class="el-icon-view tb-icon" @click="setRate(scope.row, true)"></i>
        </el-tooltip>

        <el-tooltip
          v-if="powers.some((i) => i == '005')"
          effect="dark"
          content="修改"
          placement="top"
        >
          <i class="el-icon-edit tb-icon" @click="setRate(scope.row)"></i>
        </el-tooltip>

        <!-- <el-tooltip
          v-if="powers.some((i) => i == '004') && scope.row.status + '' === '1'"
          effect="dark"
          content="禁用"
          placement="top"
        >
          <i
            class="el-icon-video-pause tb-icon"
            @click="changeStatus(scope.row.cat_id, scope.row.status)"
          ></i>
        </el-tooltip>

        <el-tooltip
          v-if="powers.some((i) => i == '004') && scope.row.status + '' !== '1'"
          effect="dark"
          placement="top"
        >
          <i
            class="el-icon-video-play tb-icon"
            @click="changeStatus(scope.row.cat_id, scope.row.status)"
          ></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"
      :is-detail="isDetail"
      :platform_id="parmValue.platform_id"
    />
  </div>
</template>
<script>
import addEditRate from "./addEditRate";
import asyncRequest from "@/apis/service/serviceParam/platformGrossProfit";
import { statusList } from "@/assets/js/statusList";
import roleLevel from "@/assets/js/roleLevel";
import mixinPage from "@/mixins/elPaginationHandle";
import companyHelper from "@/mixins/companyHelper";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
import privateField from "@/mixins/privateField";

export default {
  name: "othsearchSort",
  mixins: [mixinPage, resToken, companyHelper, privateField],
  components: {
    addEditRate,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const { btnList } = this.$store.getters;
      const tran = btnList.find((i) => i.menu_route == "platformGrossProfit") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      isDetail: true,
      private_field: [],
      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: "80px",
        },
        // {
        //   prop: "status",
        //   label: "状态",
        //   _slot_: "status",
        //   width: "70px",
        // },
        // {
        //   prop: "platform_name",
        //   label: "平台名称",
        //   minwidth: "190px",
        // },
        {
          prop: "companyNo",
          label: "公司编号",
          _slot_: "companyNo",
          width: "260px",
        },
        {
          prop: "companyName",
          label: "公司名称",
          _slot_: "companyName",
          width: "260px",
        },
        // {
        //   prop: "fund_code",
        //   label: "财务核算码",
        //   width: "150px",
        // },
        // {
        //   prop: "sale_rate",
        //   label: "成本→售价",
        //   _slot_: "sale_rate",
        //   width: "90px",
        // },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          width: "140px",
          _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.cat_id;
      this.parmValue.status = ""
      this.parmValue.cat_name = ""
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
      let model = {
        id: item.cat_id,
        titleName: item.cat_name,
      };
      this.titleList.push(model);
    },
    //设置毛利率
    setRate(row, isDetail = false) {
      this.isDetail = isDetail;
      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.parmValue.status = ""
        this.parmValue.cat_name = ""
        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 changeStatus(cat_id, status) {
      await this.$confirm(`确定要改为${status + "" === "1" ? "禁用" : "启用"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;

          const model = {
            cat_id,
            platform_id: this.parmValue.platform_id,
            status: String(status) ? (String(status) === "1" ? "0" : "1") : "1",
            companyNo: this.currentCompany,
          };

          const res = await asyncRequest.status(model);
          if (res && res.code === 0) {
            this.loading = false;

            this.$notify.success({
              title: "状态修改成功！",
              message: "",
            });

            await this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.loading = false;
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },

    // 刷新表格
    async searchList() {
      if (this.loading) return;

      if (!this.currentCompany) {
        this.$message.warning("请选择公司！");
        return;
      }

      if (this.parmValue.platform_id === "") {
        this.$message.warning("请选择平台！");
        return;
      }

      this.parmValue.companyNo = this.currentCompany;
      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;
      }
    }
  }
}
</style>
