<template>
  <div>
    <p-spin :spinning="loadding">
      <detailPage
        v-show="!detialShow"
        :tableColumns="columns"
        :title="title"
        :tableRemoteUrl="tableRemoteUrl"
        :tableRowKey="tableRowKey"
        :btnOptions="btnOptions"
        :items="items"
        :ajaxId="ajaxId"
        :tableAutoLoad="false"
        @checkOk="checkOk"
        @changeClick="changeClick"
        @selectChange="selectChange"
        ref="detail"
      />
    </p-spin>
  </div>
</template>

<script>
import detailPage from "../components/commonTable";
import { moveCheck } from "../api/api";
import API from "../api/default";
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from "@/views/kzyWMS/SearchModal";
import { sorter } from "../../utils/sort";
import { upcloud_base_wh_operate } from "@/config/serviceSettings";
import { getCargoOwner } from "../../utils/index";

const items = function (that) {
  let grid = {
    labelCol: { span: 8 },
    span: 8,
  };
  return [
    ...genWarehouseAuthSearchModal(1, true, false, null, null, grid),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, grid),
    {
      type: "select",
      name: "moveOutBldg",
      label: "移出建筑",
      required: true,
      defaultValueIndex: 0,
      destroyOptions: true,
      value: "",
      autoLoad: true,
      remoteUrl: API.wmsWhBldgPage,
      replaceFields: {
        label: "bldgName",
        value: "bldgId",
      },
      remoteBefore(res, remoteParams) {
        // console.log(arguments, 'warehouseIdwarehouseId')
        that.$nextTick(() => {
          let values = that.$refs.detail.$refs.forms.form.getFieldsValue();
          if (remoteParams.params) {
            remoteParams.params.warehouseId = values.warehouseId;
          } else {
            remoteParams.params = { warehouseId: values.warehouseId };
          }
        });
      },
      on: {
        ok(event, value, rowData, form) {
          console.log(rowData, "rowDatarowData");
        },
      },
    },
    {
      type: "select",
      name: "moveInBldg",
      label: "移入建筑",
      required: true,
      defaultValueIndex: 0,
      destroyOptions: true,
      value: "",
      autoLoad: true,
      remoteUrl: API.wmsWhBldgPage,
      replaceFields: {
        label: "bldgName",
        value: "bldgId",
      },
      remoteBefore(res, remoteParams) {
        // console.log(arguments, 'warehouseIdwarehouseId')
        that.$nextTick(() => {
          let values = that.$refs.detail.$refs.forms.form.getFieldsValue();
          if (remoteParams.params) {
            remoteParams.params.warehouseId = values.warehouseId;
          } else {
            remoteParams.params = { warehouseId: values.warehouseId };
          }
        });
      },
      on: {
        ok(event, value, rowData, form) {
          console.log(rowData, "rowDatarowData");
        },
      },
    },
    {
      type: "input",
      name: "autoBillNum",
      label: "自动单号",
    },
    // {
    //   type: 'input',
    //   name: 'warehouseName2',
    //   label: '手动单号'
    // },
    {
      type: "row",
      label: "单据日期",
      name: "ff",
      list: [
        {
          type: "date",
          name: "start",
          style: { paddingRight: "5px" },
          format: "YYYY-MM-DD",
          placeholder: "请输入开始日期",
          value: "",
        },
        {
          style: { paddingLeft: "5px" },
          type: "date",
          name: "end",
          placeholder: "请输入结束日期",
          format: "YYYY-MM-DD",
          value: "",
        },
      ],
    },
    {
      type: "select",
      name: "moveBillType",
      label: "来源类型",
      value: "",
      defaultValueIndex: 0,
      options: [
        // { label: '全部', value: '' },
        { label: "WMS", value: "WMS" },
        { label: "GSP", value: "GSP" },
        { label: "ERP", value: "ERP" },
        { label: "SC", value: "SC" },
        { label: "CHECK", value: "CHECK" },
      ],
    },
  ];
};
const columns = [
  { dataIndex: "autoBillNum", title: "自动单号", width: 150 },
  { dataIndex: "cargoOwnerName", title: "货主", width: 150 },
  { dataIndex: "moveOutBldgName", title: "移出建筑", width: 80 },
  { dataIndex: "moveInBldgName", title: "移入建筑", width: 80 },
  { dataIndex: "moveBillType", title: "来源类型", width: 80 },
  { dataIndex: "moveDate", title: "单据日期", width: 150 },
  { dataIndex: "itemCode", title: "商品编码", width: 150 },
  { dataIndex: "itemName", title: "商品名称", width: 100 },
  { dataIndex: "outLocatorCode", title: "移出货位编码", width: 180 },
  { dataIndex: "outWarehouseAreaCategory", title: "移出库别", width: 100 },
  { dataIndex: "inLocatorCode", title: "移入货位编码", width: 180 },
  { dataIndex: "inContainerCode", title: "入库容器号", width: 180 },
  { dataIndex: "inWarehouseAreaCategory", title: "移入库别", width: 100 },
  { dataIndex: "pieceQuantity", title: "移动数量", width: 100 },
  { dataIndex: "uom", title: "单位", width: 80 },
  { dataIndex: "caseConversionRate", title: "包装数量", width: 100 },
  { dataIndex: "manufacturerLotNum", title: "批号", width: 150 },
  { dataIndex: "reason", title: "移库原因", width: 150 },
  // { dataIndex: 'code14', title: '源容器', type: 'edit' },
  // { dataIndex: 'code15', title: '源货格', type: 'edit' },
  // { dataIndex: 'code16', title: '目标容器', type: 'edit' },
  // { dataIndex: 'code17', title: '目标货格', type: 'edit' },
  { dataIndex: "batchProductionDateShow", title: "生产日期", width: 100 },
  { dataIndex: "batchExpiredDateShow", title: "有效期至", width: 100 },
  { dataIndex: "shelfLifeDays", title: "保质天数", width: 80 },
  { dataIndex: "warehouseName", title: "仓库名称", width: 150 },
  { dataIndex: "warehouseWorkerId", title: "库工编码", width: 150 },
  { dataIndex: "warehouseWorkerName", title: "库工名称", width: 80 },
  { dataIndex: "comments", title: "备注", width: 150 },
];

export default {
  components: { detailPage },
  data() {
    return {
      loadding: false,
      items: items(this),
      columns: null,
      title: "移库下架",
      tableRowKey: "moveLineId",
      selectedRowKeys: [],
      selectedRows: [],
      detialRecord: {},
      addStatus: false,
      detialShow: false,
      ajaxId: "", // 移库类型 1 移库 2移位
      tableRemoteUrl: {
        url: upcloud_base_wh_operate + "/move/v1/list",
        method: "get",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        params: {
          subBillType: "1",
          statusList: "E01,W23", // 下架
        },
      },
      btnOptions: [
        {
          name: "下架",
          icon: "",
          action: "checkOk",
        },
      ],
    };
  },
  created() {
    console.log();
    this.columns = sorter(columns);
    this.getAuthBycargoOwner();
  },
  mounted() {
    this.$refs.detail.$refs.forms.form.setFieldsValue({
      warehouseId: this.$store.state.user.warehouse.warehouseId,
      warehouseName: this.$store.state.user.warehouse.warehouseName,
      start: this.getDay(-7),
      end: this.getDay(0),
    });
  },
  methods: {
    async getAuthBycargoOwner() {
      await getCargoOwner().then((res) => {
        if (res.success) {
          this.$nextTick(() => {
            this.$refs.detail.$refs.forms.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerName: res.data.list[0].companyName,
              cargoOwnerCode: res.data.list[0].companyCode,
            });
          });
        }
        if (!res.success) {
          this.$exception(res);
          return;
        }
      });
    },
    getDay(day) {
      let today = new Date();
      var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
      today.setTime(targetday_milliseconds); //注意，这行是关键代码
      var tYear = today.getFullYear();
      var tMonth = today.getMonth();
      var tDate = today.getDate();
      tMonth = this.doHandleMonth(tMonth + 1);
      tDate = this.doHandleMonth(tDate);
      return tYear + "-" + tMonth + "-" + tDate;
    },
    doHandleMonth(month) {
      var m = month;
      if (month.toString().length == 1) {
        m = "0" + month;
      }
      return m;
    },
    changeClick(record) {
      // 编辑
      this.detialShow = true;
      this.detialRecord = record;
      console.log(record);
    },
    requestUrl(val) {
      this.detialShow = val;
    },
    cancelBtn(val) {
      this.addStatus = false;
    },
    checkOk() {
      // 审核
      if (this.selectedRowKeys < 1) {
        return this.$message.warn("请选择商品！");
      }
      let params = {
        lineIds: this.selectedRowKeys,
        moveFlag: "0", // 下架
      };
      this.loadding = true;
      moveCheck(params).then((res) => {
        this.loadding = false;
        if (res.success) {
          this.$message.success(res.message);
          this.selectedRowKeys=[]
          this.$refs.detail.$refs.table.reload();
        } else {
          this.loadding = false;
          this.$exception(res);
        }
      });
    },
    successBtn() {
      this.addStatus = false;
      this.$refs.detail.$refs.table.reload();
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      console.log(selectedRowKeys, selectedRows, "selectedRowKeys, selectedRows");
      this.selectedRows = selectedRows;
      this.selectedRowKeys = selectedRowKeys;
    },
  },
};
</script>

<style></style>
