<template>
  <el-dialog
    v-loading="loading"
    :title="'选择商品'"
    :center="true"
    align="left"
    top="8vh"
    width="1040px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    append-to-body
    @close="showModelThis = false"
  >
    <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="{ selection }">
          <div style="width: 100%">
            <el-row style="padding: 0 0 10px 0; margin: -5px 0 0 0">
              <el-col :span="6" style="width: 363px">
                <good-class
                  :value="parmValue.cat_id"
                  :disabled="false"
                  :size="searchSize"
                  :is-detail="false"
                  :placeholder="'分类'"
                  @handleChange="goods_class_change"
                />
              </el-col>
              <el-col :span="4" style="width: 228px; padding: 0 0 0 10px">
                <search-brand
                  :value="parmValue.brandid"
                  :disabled="false"
                  :size="'mini'"
                  :is-detail="false"
                  :names="''"
                  :placeholder="'品牌'"
                  @searchChange="brandidsearchChange"
                />
              </el-col>
              <el-col :span="4" style="width: 145px; padding: 0 0 0 10px">
                <el-select
                  v-model="parmValue.good_type"
                  filterable
                  clearable
                  :size="searchSize"
                  placeholder="是否定制"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in options1"
                    :key="'good_type' + item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </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 style="padding: 0 0 0 0">
              <el-col :span="4" style="width: 518px">
                <el-input
                  v-model="sinput"
                  :size="searchSize"
                  :maxlength="40"
                  clearable
                  placeholder="关键字"
                  @blur="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                  @clear="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                  ><el-select
                    slot="prepend"
                    v-model="select"
                    style="width: 135px"
                    placeholder="关键字类型"
                  >
                    <el-option label="商品编号" value="1" />
                    <el-option label="商品名称" value="2" />
                    <el-option label="业务公司名称" value="3" />
                    <el-option label="供应商名称" value="4" /> </el-select
                ></el-input>
              </el-col>
              <el-col :span="3" class="fr" style="width: 66px; padding: 0 0 0 10px">
                <el-button
                  type="success"
                  :size="searchSize"
                  @click="resultList(selection)"
                >
                  保存
                </el-button>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #good_thumb_img="{ scope }">
          <div
            v-if="scope.row.good_thumb_img"
            v-viewer
            style="width: 20px; height: 20px"
            class="hover"
          >
            <img
              :src="scope.row.good_thumb_img"
              style="display: inline-block; width: 100%; height: 100%"
              alt=""
            />
          </div>
        </template>
        <template #good_type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.good_type == '0' ? 'warning' : ''"
            v-text="
              (options1.find((item) => item.id == scope.row.good_type) || {}).name || '--'
            "
          />
        </template>
      </ex-table>
    </el-card>
  </el-dialog>
</template>

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

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

  methods: {
    // supplierChange(e) {
    //   if (e && e.length === 1) {
    //     this.parmValue.supplierNo = e[0];
    //   } else {
    //     this.parmValue.supplierNo = "";
    //   }
    //   this.parmValue.page = 1;
    //   this.searchList();
    // },
    restSearch() {
      this.sinput = "";
      this.select = "1";
      // this.supplierCode = [];
      this.parmValue = {
        type_code: "", // 是否定制
        supplierNo: "", // 供应商code
        spucode: "", // 商品编码
        cat_id: [],
        brandid: [],
        good_name: "", // 商品名称
        status: "1", //
        is_stock: "",
        company: "",
        supplier: "",
        isonline: "",
        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 searchList() {
      this.loading = true;
      this.parmValue.spucode = this.select === "1" ? this.sinput : "";
      this.parmValue.good_name = this.select === "2" ? this.sinput : "";
      this.parmValue.company = this.select === "3" ? this.sinput : "";
      this.parmValue.supplier = this.select === "4" ? this.sinput : "";
      this.parmValue.isonline = this.isonline || "";
      this.parmValue.is_stock = this.is_stock || "";
      const 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] : "";

      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.forEach((a) => {
          a.cat_name = "";
          const list = a.cat_info || [];
          list.forEach((b, i) => {
            a.cat_name += i == 0 ? b.name : "/" + b.name;
          });
        });
        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 = [];
        const formValue = {
          page: 1,
          size: 100,
          name: query,
          code: "",
        };

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