<template>
  <el-dialog
    center
    title="选择组合商品库存添加记录"
    :visible="_visible"
    :close-on-click-modal="false"
    :append-to-body="true"
    width="1024px"
    top="5vh"
    class="child-product"
    @close="handleClose"
  >
    <ex-table
      :table="table"
      v-loading="loading"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @selection="selectionChange"
      @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>
            <el-col :span="24">
              <el-col :span="6" style="width: 355px;">
                <period-date-picker
                  :create_start="parmValue.create_start"
                  :create_end="parmValue.create_end"
                  :type="1"
                  :width="'165px'"
                  :size="searchSize"
                  placeholder="创建"
                  @timeReturned="timeReturned($event)"
                />
              </el-col>

              <el-col :span="3" style="width: 66px; float: right">
                <el-button
                  type="primary"
                  :size="searchSize"
                  @click="searchList"
                  style="float: right; margin-left: 5px"
                >刷新</el-button>
              </el-col>

              <el-col :span="4" style="width: 66px; float: right">
                <el-button type="warning" class="fr" :size="searchSize" @click="restSearch">重置</el-button>
              </el-col>
            </el-col>
          </el-row>
          <el-row style="margin-top: 10px">
            <el-col :span="6" style="width: 400px">
              <el-input
                v-model="value"
                :size="searchSize"
                placeholder="关键字"
                clearable
                :maxlength="40"
                @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
              >
                <template #prepend>
                  <el-select
                    v-model="parmValue.key"
                    style="width:100px"
                    @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                  >
                    <el-option label="商品名称" value="spuCode" />
                    <el-option label="商品编码" value="good_name" />
                  </el-select>
                </template>

                <el-button
                  slot="appcreate_end"
                  icon="el-icon-search"
                  @click="
                        pageInfo.curr = 1;
                        parmValue.page = 1;
                        searchList();
                      "
                ></el-button>
              </el-input>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '0' ? 'warning' : ''"
          v-text="
              (statusOptions.find((item) => item.id == scope.row.status) || {})
                .label || '--'
            "
        ></el-tag>
      </template>

      <template #order_source="{ scope }">
        <el-tag
          :size="tablebtnSize"
          v-text="
              (
              xs_order_source_options.find(
                  (item) => item.id == scope.row.order_source
                ) || {}
              ).label || '--'
            "
        ></el-tag>
      </template>

      <template #catinfo="{ scope }">{{scope.row.catInfo.map(({name}) => name).join("-")}}</template>

      <template #specInfo="{ scope }">
        <span
          v-for="(si, sii) in scope.row.specInfo"
          :key="sii + si.spec_value_id"
        >{{ sii === 0 ? "" : "-" }}{{ si.spec_name }}[{{ si.spec_value }}]</span>
      </template>

      <template #order_type="{ scope }">
        <el-tag
          :size="tablebtnSize"
          v-text="
              (
              is_stock.find((item) => item.id == scope.row.is_stock) || {}
              ).name || '--'
            "
        ></el-tag>
      </template>
    </ex-table>

    <div style="display:flex;justify-content:flex-end">
      <el-button size="mini" type="primary" @click="onSave">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { has_account_list, isStockOptions } from "@/assets/js/statusList";
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/combinedAdd";

export default {
  name: "ProductListModal",
  mixins: [mixinPage, resToken],
  props: ["visible"],
  data() {
    return {
      options1: [
        { id: "0", name: "否" },
        { id: "1", name: "是" }
      ],
      options4: [
        { id: "0", name: "非库存品" },
        { id: "1", name: "库存品" }
      ],
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: []
      },
      loading: false,
      selected: [],
      size: "mini",
      searchSize: "mini",
      tablebtnSize: "mini",
      sinput: "",
      select: "1",
      parmValue: {
        key: "spuCode",
        value: "",
        status: "",
        create_start: "",
        create_end: "",
        page: 1, // 页码
        size: 15 // 每页显示条数
      },
      has_account_list,
      isStockOptions,
      pageInfo: {
        size: 10,
        curr: 1,
        total: 0
      },
      isonlineoptions: [
        { id: "0", name: "未上线" },
        { id: "1", name: "已上线" }
      ],
      statusOptions: [
        { id: "1", label: "待审核" },
        { id: "2", label: "审核通过" },
        { id: "3", label: "审核驳回" }
      ],
      columns: [
        { type: "selection", fixed: "left", _noset_: true },
        {
          prop: "stockCode",
          label: "库存申请编码",
          fixed: "left",
          minWidth: "152px"
        },
        {
          prop: "spuCode",
          label: "商品编码",
          minWidth: "125px"
        },
        {
          label: "商品分类",
          minWidth: "125px",
          _slot_: "catinfo"
        },
        {
          prop: "good_name",
          label: "商品名称",
          minWidth: "125px"
        },
        {
          prop: "specInfo",
          label: "商品规格",
          minWidth: "125px",
          _slot_: "specInfo"
        },
        {
          prop: "status",
          _slot_: "status",
          label: "状态",
          width: "100px"
        },
        {
          prop: "apply_name",
          label: "申请人",
          width: "90px"
        },
        {
          prop: "createtime",
          label: "创建时间",
          width: "155px"
        }
      ],
      tableData: []
    };
  },
  computed: {
    _visible: {
      get() {
        return this.visible;
      },
      set(nV) {
        this.$emit("update:visible", nV);
      }
    }
  },
  watch: {
    visible(v) {
      if (!v) return;
      this.searchList();
    }
  },
  methods: {
    selectionChange(evt) {
      const { list } = evt;
      this.selected = list;
    },
    restSearch() {
      this.select = "2";
      this.sinput = "";
      this.supplierNo = [];
      this.brandid = [];
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        start: "",
        end: "",
        status: "",
        good_name: "",
        company_name: "", // 创建人部门
        spucode: "",
        cat_id: [],
        brandid: "",
        good_type: "",
        isonline: "",
        companyNo: "",
        supplierNo: ""
      };
      this.searchList();
    },
    handleClose() {
      this.selected = [];
      this._visible = false;
    },
    proportionChange(proportion) {
      this.ruleForm.proportion = proportion;
    },
    onSave() {
      if (this.selected.length === 0 || this.selected.length > 1) {
        const message =
          this.selected.length === 0
            ? "请选择组合商品库存添加记录"
            : "只能选择一条组合商品库存记录";
        this.$message.warning(message);
        return;
      }

      const { id } = this.selected[0];
      this.$emit("selected", id);
      this._visible = false;
    },
    async searchList() {
      if (
        (this.parmValue.start !== "" && this.parmValue.end === "") ||
        (this.parmValue.start === "" && this.parmValue.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      const { key, value, ...rest } = this.parmValue;

      const res = await asyncRequest.list({
        needRela: true,
        [key]: value,
        flag: "1",
        ...rest,
        status: "2"
      });

      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;
    }
  }
};
</script>

<style lang="scss" scoped>
.child-product {
  /deep/ .el-pagination {
    float: left !important;
  }
}
</style>
