<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;margin-left:10px">
              <el-input placeholder="支付渠道名称" clearable size="mini" v-model="parmValue['channel_name']" @blur="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "  />
            </el-col>
            <el-col :span="4" style="width: 390px; padding: 0 0 0 10px">
              <el-input
                :size="searchSize"
                v-model="parmValue['apply_name']"
                :maxlength="40"
                clearable
                @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=" setRate({}, actionTypes.CREATE)">
                新建
              </el-button>
            </el-col>
          </el-row>
        </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 #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, actionTypes.PREVIEW)"></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,actionTypes.UPDATE)"></i>
        </el-tooltip>

        <el-tooltip
          v-if="powers.some((i) => i == '004') && scope.row.status + '' === '0'"
          effect="dark"
          content="启用"
          placement="top"
        >
          <i
            class="el-icon-video-play tb-icon"
            @click="changeStatus(scope.row.id, scope.row.status)"
          ></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.id, scope.row.status)"
          ></i>
        </el-tooltip>
      </template>
    </ex-table>
    
    <no-auth v-else />

    <!-- 弹窗 新增/修改 -->
    <add-edit-rate
      :id="modelRateId"
      :show-model="showRateModel"
      :sitem="sitemRate"
      @refresh="searchList"
      @cancel="showRateModel = false"
      :actionType="actionType"
    />
  </div>
</template>
<script>
import addEditRate from "./addEditRate";
import asyncRequest from "@/apis/service/serviceParam/paymentChannelManage";
import { statusList } from "@/assets/js/statusList";
import roleLevel from "@/assets/js/roleLevel";
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
import privateField from "@/mixins/privateField";
import { actionTypes } from "@/utils/actionType";

export default {
  name: "othsearchSort",
  mixins: [mixinPage, resToken, privateField],
  components: {
    addEditRate,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const { btnList } = this.$store.getters;
      const tran = btnList.find((i) => i.menu_route == "paymentChannelManage") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      actionTypes,
      actionType:'',
      isDetail: true,
      private_field: [],
      statusList: statusList,
      roleLevel: roleLevel,
      loading: false,
      sitem: {},
      showRateModel: false,
      modelRateId: "000",
      sitemRate: {},
      titleList: [],
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        status:"",
        channel_name:"",
        apply_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",
          minWidth: "60px",
        },
        {
          prop: "channel_name",
          label: "支付渠道名称",
          width: "230px",
        },
        {
          prop: "companyName",
          label: "业务公司名称",
          width: "230px",
        },
        {
          prop: "status",
          label: "当前状态",
          width: "230px",
          _slot_:'status'
        },
        {
          prop: "apply_name",
          label: "创建人",
          width: "230px",
        },
        {
          prop: "addtime",
          label: "创建时间",
          width: "260px",
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          width: "140px",
          _noset_: true,
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.parmValue.pid = "0";
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        status: "",
        channel_name:"",
        apply_name:""
      };
      this.searchList();
    },
    //设置毛利率
    setRate(row, actionType) {
      this.actionType = actionType

      if(this.actionTypes.UPDATE === actionType && row.status !== "0"){
        this.$message.warning('必须在禁用后才能修改!')
        return
      }


      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);
      }
      
      this.parmValue.cat_name = "";
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },

    async changeStatus(id, status) {
      await this.$confirm(`确定要改为${status + "" === "1" ? "禁用" : "启用"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;

          const model = {
            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;
      // }

      // 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>
