<template>
  <el-dialog title="选择仓号" :visible.sync="dialogVisible" width="900px">
    <el-form
      :inline="true"
      :model="searchForm"
      ref="searchForm"
      class="search-bm-boder"
    >
      <el-row>
        <el-form-item label="仓号:" prop="cfmc">
          <el-input
            clearable
            v-model="searchForm.cfmc"
            style="width: 170px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-button
          type="primary"
          @click="doSearch"
          size="small"
          class="iconfont iconchaxun"
          >查询</el-button
        >
      </el-row>
    </el-form>
    <el-table
      border
      stripe
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      height="500"
      @selection-change="handleSelectionChange"
    >
      <!-- :summary-method="getSummaries"
      show-summary -->
      <el-table-column align="center" type="selection" width="70">
      </el-table-column>
      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="type == 'wtsThree'"
        align="center"
        prop="sscfmc"
        label="仓号"
      ></el-table-column>
      <el-table-column
        v-if="type != 'wtsThree'"
        align="center"
        prop="cfmc"
        label="仓号"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="fcsl"
        label="数量"
        v-if="type == 'wtsThree'"
      >
        <template slot-scope="scope">
          {{
            String(scope.row.fcsl).indexOf(".") > -1
              ? Number(scope.row.fcsl).toFixed(3)
              : scope.row.fcsl
          }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="type == 'wtsThree'"
        align="center"
        prop="lsnd"
        label="生产年限"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="lspzmc"
        label="品种"
        v-if="type == 'wtsThree'"
      >
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        class="iconfont iconqueren"
        @click="getSelectList"
        >确 定</el-button
      >
      <el-button
        type="primary"
        class="iconfont iconquxiao"
        @click="dialogVisible = false"
        >取 消</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["type"],
  data() {
    return {
      flag: false,
      searchForm: { cfmc: "" },
      tableData: [],
      dialogVisible: false,
      cfListBefore: [],
      cfList: [],
      lcdwIndex: "",
      lcdwItem: {},
      lcdwIndex1: "",
      goodsList: [],
    };
  },
  methods: {
    doSearch() {
      this.$emit(
        "seeChList",
        this.lcdwItem,
        this.lcdwIndex,
        this.searchForm.cfmc,
        this.flag ? this.goodsList : "",
        this.flag ? this.lcdwIndex1 : "",
        this.flag ? this.flag : ""
      );
    },
    handleSelectionChange(val) {
      this.cfList = [];
      console.log(val, "val");
      val.map((item) => {
        if (this.type == "wtsThree") {
          this.cfList.push({
            ccdd: item.sscfmc,
            cfbm: item.sscfbh,
            pzbm: item.lspzbm,
            pzmc: item.lspzmc,
            scnd: item.lsnd,
          });
        } else {
          this.cfList.push({ ccdd: item.cfmc, cfbm: item.cfbh });
        }
      });
    },
    getSelectList() {
      console.log(this.lcdwIndex1, "this.lcdwIndex1");
      this.$emit(
        "getxzCfList",
        this.cfList,
        this.lcdwIndex,
        this.flag ? this.lcdwIndex1 : "",
        this.flag ? this.flag : ""
      );
    },
    toggleSelection(rows) {
      console.log(rows, "2");
      this.tableData.map((item) => {
        rows.map((item1) => {
          if (item.cfbh == item1.cfbm) {
            // arr.push(item);
            // console.log(this.$refs.multipleTable);
            this.$nextTick(() => {
              this.$refs.multipleTable.toggleRowSelection(item);
            });
          }
        });
      });
      // if (arr) {
      //   arr.forEach((row) => {

      //   });
      // } else {
      //   this.$refs.multipleTable.clearSelection();
      // }
    },
    getSummaries(param) {
      const { columns } = param;
      const data = this.cfList;
      // console.log(columns, data);
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index == 4) {
          sums[index] = "";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));

        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] =
            String(sums[index]).indexOf(".") > -1
              ? parseFloat(sums[index]).toFixed(3)
              : sums[index];
        } else {
          sums[index] = "";
        }
      });
      return sums;
    },
  },
};
</script>
<style lang="sass" scoped>
</style>
