<template>
  <div>
    <!-- 运费录入 -->
    <p-card title="运费录入">
      <span slot="extra">
        <p-button type="primary" style="margin-right: 10px" @click="print">物流标签补打</p-button>
        <p-button type="primary" style="margin-right: 10px" @click="save">保存</p-button>
      </span>
      <p-grid-form ref="form" :span="8" :items="config.items" @submit="hanlderSubmit" />
      <div class="waring">请谨慎填写,保存后不能修改!</div>
      <p-table-sheet
        ref="tableSheet"
        row-key="id"
        bordered
        :columns="columns"
        :rowSelectType="2"
        :dataSource="data"
        :scroll="{ x: true }"
      >
      </p-table-sheet>
    </p-card>
  </div>
</template>

<script>
//todo 运费录入接口未联调
import config from "./config.js";
import pick from "lodash.pick";
import { queryFreightEnter, saveFreightEnter } from "./api";
import { diffOption } from "@/views/kzyWMS/utils/index.js";
export default {
  data() {
    return {
      columns: [],
      config: config(this),
      data: [],
      type: true,
      remoteParams: {},
      visible: false,
      confirmLoading: false,
    };
  },
  mounted() {
    this.getinfo();
    console.log(this.$store.state.user.warehouse);
  },
  created() {
    diffOption(this.config.columns).then((res) => {
      this.columns = res;
    });
  },
  methods: {
    async save() {
      //   console.log(this.data);

      let data = await this.$refs.tableSheet.getDataSource(true, true);
      console.log(data);

      if (data.length == 0) {
        this.$message.warning("请先选择数据!");
        return;
      }
      try {
        let p = data.map((i) => {
          return { ...i, ...this.$store.state.user.warehouse };
        });
        let res = await saveFreightEnter(p);
        if (res.success) {
          this.data = [];
          this.$message.success(res.message || "保存成功!");
          //   this.remoteParams = { ...this.remoteParams };
        } else {
          this.$exception(res);
        }
      } catch (error) {
        this.$exception(error);
      }
    },
    print() {},
    hanlderSubmit(e, form) {
      e.preventDefault();
      form.validateFields((error, values) => {
        if (!error) {
          console.log(error, values, "valuesvaluesvaluesvaluesvalues");
          //   this.remoteParams = { ...this.remoteParams, ...values };
          this.getinfo();
        }
      });
    },
    async getinfo() {
      this.$refs.form.form.validateFields(async (error, values) => {
        if (!error) {
          console.log(values, "valuesvaluesvaluesvaluesvalues");

          try {
            let res = await queryFreightEnter({ ...values, ...this.$store.state.user.warehouse });
            console.log(res);
            if (res.success) {
              this.data = res.data;
              //   this.$message.success(res.message);
              //   this.remoteParams = { ...this.remoteParams };
            } else {
              this.$exception(res);
            }
          } catch (error) {
            this.$exception(error);
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.waring {
  padding: 15px;
  text-align: center;
  font-size: 28px;
  color: red;
  line-height: 50px;
}
</style>
