<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: 220px">
                  <el-input
                    :size="'mini'"
                    v-model="parmValue.buy_num"
                    :maxlength="40"
                    disabled
                    placeholder="起订量"
                    ><template slot="prepend">起订量:</template></el-input
                  >
                </el-col>
                <el-col :span="4" style="width: 460px; padding: 0 0 0 10px">
                  <el-input
                    :size="'mini'"
                    v-model="company"
                    :maxlength="40"
                    clearable
                    disabled
                    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: 220px">
                  <el-input
                    :size="'mini'"
                    v-model="parmValue.price"
                    :maxlength="40"
                    disabled
                    placeholder="预算单价"
                    ><template slot="prepend">售 价 ≤</template></el-input
                  >
                </el-col>
                <el-col :span="4" style="width: 250px; padding: 0 0 0 10px">
                  <el-input
                    :size="'mini'"
                    v-model="platform_name"
                    :maxlength="40"
                    clearable
                    disabled
                    placeholder="所属平台"
                  />
                </el-col>
                <el-col :span="4" style="width: 280px; padding: 0 0 0 10px">
                  <good-class
                    :value="parmValue.cat_id"
                    @handleChange="goods_class_change"
                    :disabled="sitem.good_type === '1'"
                    :size="'mini'"
                    :isDetail="sitem.good_type === '1'"
                    :placeholder="'分类'"
                  />
                </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>
      </ex-table>
    </el-card>
  </el-dialog>
</template>

<script>
import asyncRequest from "@/apis/service/sellOut/project";

import resToken from "@/mixins/resToken";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import columns from "./columns";
export default {
  name: "searchGoodOnlineTableModal",
  mixins: [resToken, mixinPage],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
  },
  props: ["showModel", "sitem"],

  /**
   * 属性集合
   * @param {Boolean}       showModel             : 是否弹出弹窗        必填
   * @param {Boolean}       once                  : 是否只选一条        非必填
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      options: [
        { id: "0", name: "非库存品" },
        { id: "1", name: "库存品" },
      ],
      company: "",
      platform_name: "",
      selectLoading: false,
      searchName: "",
      loading: true,
      showModelThis: false,
      parmValue: {
        buy_num: "", // 商品购买数量
        price: "", // 单价
        cat_id: [], // 分类id
        companyNo: "", // 业务公司id
        platform_code: "", //平台编号id
        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.sitem);
      const {
        platform_id,
        companyNo,
        num,
        cat_id,
        budget_price,
        company,
        platform_name,
        cat_info,
      } = this.sitem;
      console.log(this.sitem);
      this.company = company;
      this.platform_name = platform_name;
      let list = [];
      if (cat_info && cat_info.length > 0) {
        cat_info.forEach((s) => {
          list.push(s.id);
        });
      }
      this.parmValue = {
        buy_num: num, // 商品购买数量
        price: budget_price, // 单价
        cat_id: list, // 分类id
        companyNo: companyNo, // 业务公司id
        platform_code: platform_id, //平台编号id
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.searchList();
    },
    async searchList() {
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      const { cat_id } = model;
      model.cat_id =
        cat_id && cat_id.length > 0 ? cat_id[cat_id.length - 1] : "";
      const res = await asyncRequest.get_good_online_list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.map((v) => {
          v.cat_name = "";
          if (v.cat_info && v.cat_info.length > 0) {
            v.cat_info.forEach((x, xi) => {
              v.cat_name += `${xi !== 0 ? "_" : ""}${x.name}`;
            });
            return v
          }
          this.setNum(v.sale_price);

          return v;
        });
        //
        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 goods_class_change(e) {
      this.parmValue.cat_id = e;
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
    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.loading = true;
      let model = {
        pgNo: this.sitem.pgNo,
        spuCode: [],
        source: "1",
      };
      selection.forEach((a) => {
        model.spuCode.push(a.skuCode);
      });
      let { code, data, message } = await asyncRequest.project_online_good_add(
        model
      );
      this.loading = false;
      if (code === 0) {
      this.$notify.success({
          title: "添加成功！",
          message: "",
        });
        this.showModelThis = false;
        this.$emit("submitRes", this.sitem.pgNo_index);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
  },
};
</script>

<style>
</style>
