<template>
  <div>
    <detail-page
      v-show="!detialShow"
      :tableColumns="columns"
      :tableColumnsDetail="columnsDetail"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :tableAutoLoad="false"
      :btnOptions="btnOptions"
      :items="items"
      :ajaxId="ajaxId"
      @print="print"
      @checkOk="checkOk"
      @changeClick="changeClick"
      @selectChange="selectChange"
      @selectChange1="selectChange1"
      ref="detial"
    >
    </detail-page>
  </div>
</template>

<script>
import DetailPage from "../components/commonTableNews";
import { moveAudit, moveCancel } from "../api/api";
import API from "../api/default";
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from "@/views/kzyWMS/SearchModal";
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.detial.$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.detial.$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: "moveHeaderId",
      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: "moveHeaderId", title: "单据编号", key: "moveHeaderId", width: 150 },
  { dataIndex: "moveDate", title: "单据日期", key: "moveDate", width: 150 },
  { dataIndex: "cargoOwnerName", title: "货主", key: "cargoOwnerName", width: 200 },
  { dataIndex: "moveOutBldgName", title: "移出建筑", key: "moveOutBldgName", width: 100 },
  { dataIndex: "moveInBldgName", title: "移入建筑", key: "moveInBldgName", width: 100 },
  { dataIndex: "moveBillType", title: "来源类型", key: "moveBillType", width: 80 },
];
const columnsDetail = [
  { dataIndex: "itemCode", title: "商品编码", key: "itemCode", width: 150 },
  { dataIndex: "itemName", title: "商品名称", key: "itemName", width: 200 },
  { dataIndex: "outLocatorCode", title: "移出货位编码", key: "outLocatorCode", width: 180 },
  { dataIndex: "outWarehouseAreaCategory", title: "移出库别", key: "outWarehouseAreaCategory", width: 100 },
  { dataIndex: "inLocatorCode", title: "移入货位编码", key: "inLocatorCode", width: 180 },
  { dataIndex: "inWarehouseAreaCategory", title: "移入库别", key: "inWarehouseAreaCategory", width: 100 },
  { dataIndex: "pieceQuantity", title: "移动数量", key: "pieceQuantity", width: 100 },
  { dataIndex: "uom", title: "单位", key: "uom", width: 80 },
  { dataIndex: "caseConversionRate", title: "包装数量", key: "caseConversionRate", width: 100 },
  { dataIndex: "manufacturerLotNum", title: "批号", key: "manufacturerLotNum", width: 150 },
  { dataIndex: "reason", title: "移库原因", key: "reason", width: 150 },
  // { dataIndex: 'code14', title: '源容器', key: 'code14', type: 'edit' },
  // { dataIndex: 'code15', title: '源货格', key: 'code15', type: 'edit' },
  // { dataIndex: 'code16', title: '目标容器', key: 'code16', type: 'edit' },
  // { dataIndex: 'code17', title: '目标货格', key: 'code17', type: 'edit' },
  { dataIndex: "batchProductionDateShow", title: "生产日期", key: "batchProductionDate", width: 100 },
  { dataIndex: "batchExpiredDateShow", title: "有效期至", key: "batchExpiredDateShow", width: 100 },
  // { dataIndex: 'shelfLifeDays', title: '保质天数', key: 'shelfLifeDays', width: 80 },
  // { dataIndex: 'warehouseName', title: '仓库名称', key: 'warehouseName', width: 150 },
  { dataIndex: "warehouseWorkerId", title: "库工编码", key: "warehouseWorkerId", width: 150 },
  { dataIndex: "warehouseWorkerName", title: "库工名称", key: "warehouseWorkerName", width: 80 },
  { dataIndex: "comments", title: "备注", key: "comments", width: 150 },
];
import { toPrintServer } from "@/views/kzyWMS/utils/printServer.js";
import { upcloud_base_wh_operate } from "@/config/serviceSettings";

export default {
  components: { DetailPage },
  data() {
    return {
      items: items(this),
      columns,
      columnsDetail,
      title: "打印",
      tableRowKey: "moveHeaderId",
      selectedRowKeys: [],
      selectedRows: [],
      selectedRows1: [],
      selectedRowKeys1: [],
      detialRecord: {},
      addStatus: false,
      detialShow: false,
      ajaxId: "", // 移库类型 1 移库 2移位
      tableRemoteUrl: {
        url: upcloud_base_wh_operate + "/move/v1/head-list",
        method: "get",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        params: {
          subBillType: "1",
          status: "0",
          statusList: "W01,W20,W21",
        },
      },
      btnOptions: [
        {
          name: "打印",
          icon: "",
          action: "print",
        },
      ],
    };
  },
  created() {
    console.log();
    this.getAuthBycargoOwner();
  },
  mounted() {
    this.$refs.detial.$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.detial.$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 = {};
      // moveAudit(params).then(res => {
      //   if (res.success) {
      //     this.$message.success('审核成功')
      //     this.$refs.detial.$refs.table.reload()
      //   } else {
      //     this.$exception(res)
      //   }
      // })
    },
    successBtn() {
      this.addStatus = false;
      this.$refs.detial.$refs.table.reload();
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      console.log(selectedRowKeys, selectedRows, "selectedRowKeys, selectedRows");
      this.selectedRows = selectedRows;
      this.selectedRowKeys = selectedRowKeys;
    },
    selectChange1(selectedRowKeys, selectedRows) {
      this.selectedRows1 = selectedRows;
      this.selectedRowKeys1 = selectedRowKeys;
    },
    //打印
    print() {
      console.log(this.$store.getters.userInfo);
      console.log(this.$refs.detial.$refs.forms.form.getFieldsValue());
      if (this.selectedRows1.length < 1) {
        this.$message.error("请至少勾选一条子表数据");
        return;
      }
      const params = [
        {
          head: {
            enterpriseName: this.$store.getters.userInfo.enterpriseName,
            workerNo: this.$store.getters.userInfo.employeeNo,
            printUser: this.$store.getters.userInfo.employeeName,
            moveInBldgName: this.selectedRows[0].moveInBldgName,
            moveOutBldgName: this.selectedRows[0].moveOutBldgName,
            moveHeaderId: this.selectedRows[0].moveHeaderId,
            excuteUser: this.selectedRows[0].excuteUser,
            moveDate: this.selectedRows[0].moveDate,
            deptName: "",
          },
          table: this.selectedRows1,
        },
      ];
      toPrintServer(params, this.$store.getters.warehouse.warehouseId, "YK_KP").then((res) => {
        if (!res.success) {
          this.$exception(res);
          return;
        }
        this.$message.success(res.message);
      });
    },
  },
};
</script>

<style></style>
