<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="'mini'"
        @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="{ selection }">
          <div style="width: 100%">
            <el-row style="margin-top: -15px">
              <el-col :span="24">
                <el-col :span="4" style="width: 210px">
                  <search-brand
                    :value="parmValue.brandid"
                    :disabled="false"
                    :size="'mini'"
                    :isDetail="false"
                    :names="''"
                    :placeholder="'品牌'"
                    @searchChange="brandidsearchChange"
                  />
                </el-col>
                <el-col :span="6" style="width: 370px; padding: 0 0 0 10px">
                  <good-class
                    :value="parmValue.cat_id"
                    @handleChange="goods_class_change"
                    :disabled="false"
                    :size="searchSize"
                    :isDetail="false"
                    :placeholder="'分类'"
                  />
                </el-col>

                <el-col :span="3" class="fr" style="width: 66px">
                  <el-button
                    :size="'mini'"
                    type="primary"
                    style="float: right; margin-left: 5px"
                    @click="searchList"
                  >
                    刷新
                  </el-button>
                </el-col>
                <el-col :span="4" class="fr" style="width: 66px">
                  <el-button type="warning" class="fr" :size="'mini'" @click="restSearch">
                    重置
                  </el-button>
                </el-col>
              </el-col>
              <el-col :span="24" style="padding: 10px 0 0 0">
                <el-col :span="4" style="width: 450px">
                  <el-input
                    clearable
                    :size="'mini'"
                    v-model="sinput"
                    :maxlength="40"
                    placeholder="关键字"
                    @blur="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                    @clear="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                  >
                    <el-select
                      v-model="select"
                      style="width: 120px"
                      slot="prepend"
                      placeholder="关键字类型"
                    >
                      <el-option label="商品名称" value="1" />
                      <el-option label="成本商品编码" value="2" />
                      <el-option label="上线商品编码" value="4" />
                      <el-option label="平台商品编码" value="3" />
                    </el-select>
                  </el-input>
                </el-col>

                <el-col :span="3" style="width: 66px; float: right">
                  <el-button
                    :size="'mini'"
                    type="success"
                    style="float: right; margin-left: 5px"
                    @click="resultList(selection)"
                  >
                    保存
                  </el-button>
                </el-col>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #good_thumb_img="{ scope }">
          <div
            v-if="scope.row.good_thumb_img"
            style="width: 20px; height: 20px"
            class="hover"
            v-viewer
          >
            <img
              :src="scope.row.good_thumb_img"
              style="display: inline-block; width: 100%; height: 100%"
              alt=""
            />
          </div>
        </template>
        <template #model="{ scope }">
          <span v-for="(si, sii) in scope.row.specinfo" :key="si.spec_id">
            <span v-if="sii !== 0">--</span
            ><span>{{ si.spec_name }}[{{ si.spec_value }}]</span></span
          >
        </template>
        <template #is_stock="{ scope }">
          <span>{{ scope.row.is_stock + "" === "1" ? "库存品" : "非库存品" }} </span>
        </template>
      </ex-table>
    </el-card>
  </el-dialog>
</template>

<script>
import asyncRequest from "@/apis/components/search-good-online-modal";
import resToken from "@/mixins/resToken";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import columns from "./columns";
export default {
  name: "searchGoodOnlineModal",
  mixins: [resToken, mixinPage],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
  },
  props: ["showModel", "once", "sitem", "active"],

  /**
   * 属性集合
   * @param {Boolean}       showModel             : 是否弹出弹窗        必填
   * @param {Boolean}       once                  : 是否只选一条        非必填
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      select: "1",
      sinput: "",
      is_stock: [
        { id: "0", name: "非库存品" },
        { id: "1", name: "库存品" },
      ],
      selectLoading: false,
      searchName: "",
      supplierCode: [],
      loading: true,
      showModelThis: false,
      parmValue: {
        platform_code: "",
        companyNo: "",
        is_stock: "",
        exam_status: "",
        cat_id: [], // 商品属性code
        brandid: [],
        skucode: "",
        spucode: "", // 商品编码
        good_name: "", // 商品名称
        plat_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.restSearch();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },

  methods: {
    restSearch() {
      console.log(this.active);
      console.log(this.sitem);
      const { platform_code, company_id, is_stock } = this.sitem;

      this.parmValue = {
        platform_code: platform_code || "",
        companyNo: company_id || "", //业务企业编号
        is_stock: is_stock || "",
        cat_id: [], // 商品属性code
        brandid: [],
        exam_status: this.active ? "3" : "",
        skucode: "",
        spucode: "", // 商品编码
        good_name: "", // 商品名称
        plat_code: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.searchList();
    },
    async goods_class_change(e) {
      this.parmValue.cat_id = e;
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
    async brandidsearchChange(e) {
      const { id } = e;
      this.parmValue.brandid = id ? [id] : [];
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
    async platform_codesearchChange(e) {
      const { id, code, label } = e;
      this.parmValue.platform_code = id ? [id] : [];
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
    async company_idsearchChange(e) {
      const { id, code, label } = e;
      this.parmValue.company_id = code || "";
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
    async searchList() {
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      model.cat_id = model.cat_id.length > 0 ? model.cat_id[model.cat_id.length - 1] : "";
      model.brandid =
        model.brandid.length > 0 ? model.brandid[model.brandid.length - 1] : "";
      model.spucode = this.select === "2" ? this.sinput : ""; // 商品编码
      model.good_name = this.select === "1" ? this.sinput : ""; // 商品名称
      model.plat_code = this.select === "3" ? this.sinput : "";
      model.skucode = this.select === "4" ? this.sinput : "";
      // model.platform_code =
      //   model.platform_code.length > 0
      //     ? model.platform_code[model.platform_code.length - 1]
      //     : "";
      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.map((e) => {
          e.cat_name = "";
          if (e.cat_info && e.cat_info.length > 0) {
            e.cat_info.forEach((si, sii) => {
              e.cat_name += `${sii !== 0 ? "_" : ""}${si.name}`;
            });
          }
          return e;
        });

        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;
    },
    async remoteMethod(query) {
      this.selectLoading = true;
      if (query !== "") {
        this.options = [];
        let formValue = {
          page: 1,
          size: 100,
          name: query,
          code: "",
        };

        let res = await asyncRequest.supplierList(formValue);
        if (res && res.code === 0 && res.data) {
          const { list } = res.data;
          this.options = list;
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(res.message);
        }
      } else {
        this.options = [];
      }
      this.selectLoading = false;
    },
    async resultList(selection) {
      if (selection && selection.length > 1 && this.once) {
        this.$message.warning("只能选择一条商品信息！");
        return;
      }
      this.showModelThis = false;
      this.$emit("resultList", selection);
    },
  },
};
</script>

<style></style>
