<template>
  <el-dialog
    :title="'支持品牌列表'"
    :center="true"
    align="left"
    top="8vh"
    width="1040px"
    @close="showModelThis = false"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    append-to-body
  >
    <el-card style="margin-top: -20px" class="modal-form-style">
      <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="margin: -10px 0 10px 0; width: 100%" class="tr">
            <div v-if="modelSitem" class="fl" style="height:28px;line-height:28px">
              <span>供应商：</span>{{ modelSitem.name }}
            </div>
            <el-button
              type="primary"
              style="margin: 0 0 0 10px"
              :size="searchSize"
              @click="searchList"
            >
              刷新
            </el-button>

            <el-button
              type="warning"
              style="margin: 0 0 0 10px"
              :size="searchSize"
              @click="restSearch"
            >
              重置
            </el-button>
            <el-button
              type="success"
              style="margin: 0 0 0 10px"
              :size="searchSize"
              @click="openCostEdit(-1, 'add')"
            >
              添加
            </el-button>
          </div>
        </template>
        <template #brand_book="{ scope }">
          <div
            v-if="scope.row.brand_book"
            style="width: 20px; height: 20px"
            class="hover"
            v-viewer
          >
            <img
              :src="scope.row.brand_book"
              style="display: inline-block; width: 100%; height: 100%"
              alt=""
            />
          </div>
        </template>
        <template #long="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.long == '1' ? 'warning' : ''"
            v-text="
              (options1.find((item) => item.id == scope.row.long) || {}).name ||
              '--'
            "
          ></el-tag>
        </template>
        <template #longTime="{ scope }">
          {{ scope.row.starttime }}~{{ scope.row.endtime }}
        </template>
        <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.status == '0' ? 'warning' : ''"
            v-text="
              (options2.find((item) => item.id == scope.row.status) || {})
                .name || '--'
            "
          ></el-tag>
        </template>
        <template #operation="{ scope }">
          <el-tooltip effect="dark" content="查看" placement="top">
            <i
              class="el-icon-view tb-icon"
              @click="openCostEdit(scope.row.id, 'view')"
            ></i>
          </el-tooltip>
          <el-tooltip effect="dark" content="修改" placement="top">
            <i
              class="el-icon-edit tb-icon"
              @click="openCostEdit(scope.row.id, 'edit')"
            ></i>
          </el-tooltip>
          <el-tooltip
            v-if="
              powers.some((item) => item == '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>
          <el-tooltip
            v-if="
              powers.some((item) => item == '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 effect="dark" content="删除" placement="top">
            <i
              class="el-icon-delete tb-icon"
              @click="deleteItem(scope.row.id)"
            ></i>
          </el-tooltip>
        </template>
      </ex-table>
      <add-edit
        :id="modelId"
        :type="modelType"
        :sitem="modelSitem"
        :showModel="costshowModel"
        :code="code"
        @cancel="costshowModel = false"
        @refresh="(costshowModel = false), searchList()"
      />
    </el-card>
  </el-dialog>
</template>

<script>
import asyncRequest from "@/apis/service/serviceParam/supplier";
import resToken from "@/mixins/resToken";
import mixinPage from "@/mixins/elPaginationHandle";
import addEdit from "../addEdit";
import { mapGetters } from "vuex";
import columns from "./columns";
export default {
  name: "searchGoodModal",
  mixins: [resToken, mixinPage],
  components: {
    addEdit,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "supplier"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  props: ["showModel", "sitem"],

  /**
   * 属性集合
   * @param {Boolean}       showModel             : 是否弹出弹窗        必填
   * @param {Boolean}       once                  : 是否只选一条        非必填
   */
  /**
   * 事件集合
   * @resultList             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      options1: [
        { id: "0", name: "长期" },
        { id: "1", name: "非长期" },
      ],
      options2: [
        { id: "0", name: "禁用" },
        { id: "1", name: "启用" },
      ],
      costshowModel: false,
      modelSitem: null,
      modelId: "",
      modelType: "",
      selectLoading: false,
      loading: true,
      showModelThis: false,
      parmValue: {
        gyscode: "", // 供应商code
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      tableData: [],
      passwordModel: false,
      passwordModelId: 0,
      isPasswordDetail: false,
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        // _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initd();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },

  methods: {
    async initd() {
      this.modelSitem = JSON.parse(JSON.stringify(this.sitem));
      await this.restSearch();
    },
    async restSearch() {
      this.parmValue = {
        gyscode: this.code, // 供应商code
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      await this.searchList();
    },
    async searchList() {
      this.loading = true;
      this.parmValue.gyscode = this.modelSitem.code;
      const res = await asyncRequest.klist(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;
    },
    //新建/编辑/查看
    openCostEdit(id, type) {
      this.modelId = id;
      this.modelType = type;
      this.costshowModel = true;
    },
    /**
     * 启用/禁用
     * @param {String} id id
     * @param {String} status 0-禁用 1-启用
     */
    async changeStatus(id, status) {
      await this.$confirm(`确定要改为${status === "1" ? "禁用" : "启用"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          const model = {
            id: id,
            status: status === "1" ? "0" : "1",
          };
          const res = await asyncRequest.kstatus(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 deleteItem(id) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            id: id,
          };
          const res = await asyncRequest.kdelete(model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: "删除成功",
              message: "",
            });
            this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
  },
};
</script>

<style>
</style>