<template>
  <p-spin :spinning="spinning">
    <!-- :ajaxId="ajaxId" -->
    <searchTable
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :items="items"
      :tableAutoLoad="false"
      @save="save"
      @selectChange="selectChange"
      ref="detial"
    />
    <p-modal title="解锁结论" :visible="modalVisible" :width="480" @ok="handleOk" @cancel="handleCancel">
      <p-form-model :model="modalForm" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
        <p-form-model-item label="解锁结论">
          <p-select v-model="modalForm.iqcstatus" :options="conclusionOptions" />
        </p-form-model-item>
      </p-form-model>
    </p-modal>
  </p-spin>
</template>

<script>
import searchTable from "../components/searchTable";
import { unlock } from "../api/api";
import API from "../api/default";
import { sorter } from "../../utils/sort";
import { remote } from "../api/remote";
import { diffOption } from "@/views/kzyWMS/utils/index.js";
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from "../../SearchModal";
import { SERVICE_DATA_API_BASE, upcloud_base_inventory } from "@/config/serviceSettings";
import { getCargoOwner } from "../../utils/index";

let warehouseId = "";
let bldgId = "";
let warehouseRoomId = "";
let warehouseAreaId = "";
let warehouseLocatorName = "";
let warehouseLocatorId = "";
const items = function (that) {
  return [
    ...genWarehouseAuthSearchModal(
      1,
      true,
      false,
      null,
      function (modalRowData, form) {
        console.log(modalRowData);
        if (modalRowData) {
          warehouseId = modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId;
          form.setFieldsValue({
            warehouseId: modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId,
            warehouseLocatorId: "",
            warehouseLocatorName: "",
          });
        } else {
          form.setFieldsValue({
            warehouseLocatorId: "",
            warehouseLocatorName: "",
            warehouseId: "",
          });
          warehouseId = "";
          warehouseLocatorName = "";
          warehouseLocatorId = "";
        }
      },
      { span: 8 }
    ),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, {
      span: 8,
    }),
    // { type: "input", name: "lockStatus", label: "lockStatus", value: 2, hidden: true },
    { type: "input", name: "itemId", label: "商品Id", hidden: true },
    {
      name: "itemName",
      label: "商品名称",
      type: "search",
      targetValueKey: "itemName",
      targetClick: true,
      modalOpenChange: (open) => {
        let values = that.$refs.detial.$refs.forms.form.getFieldsValue();
        if (!values.warehouseId) {
          // 仓库
          that.$message.warn("请选择仓库");
          return false;
        } else if (!values.cargoOwnerId) {
          // 货主
          that.$message.warn("请选择货主");
          return false;
        } else {
          return true;
        }
      },
      toolBar: {
        items: [
          {
            type: "input",
            name: "itemName",
            label: "商品名称",
            span: 8,
            value: "",
          },
          {
            type: "input",
            name: "itemCode",
            label: "商品编码",
            span: 8,
            value: "",
          },
        ],
      },
      tableConfig: {
        rowKey: "itemId",
        remoteUrl: (res) => {
          return {
            ...API.commonUrl,
          };
        },
        remoteParams: {
          cargoOwnerId: that.cargoOwnerId,
          warehouseId: that.warehouseId,
        },
        remoteBefore: (res, remoteParams, form, val1, val2) => {
          console.log(arguments, "33333333");
          let values = that.$refs.detial.$refs.forms.form.getFieldsValue();
          remoteParams.data = {
            ...remoteParams.data,
            num: "item-storage",
            params: {
              cargoOwnerId: values.cargoOwnerId,
              warehouseId: values.warehouseId,
              itemCode: remoteParams.params.itemCode ? remoteParams.params.itemCode : "",
              itemName: remoteParams.params.itemName ? remoteParams.params.itemName : "",
            },
          };
          console.log(remoteParams, "remoteParamsremoteParamsremoteParams");
          return remoteParams;
        },
        columns: [
          {
            dataIndex: "itemName",
            title: "商品名称",
            key: "itemName",
          },
          {
            dataIndex: "itemCode",
            title: "商品编码",
            key: "itemCode",
          },
          {
            dataIndex: "itemId",
            title: "商品id",
            key: "itemId",
          },
        ],
      },
      on: {
        ok(event, value, rowData, form, name, index, tableRowData) {
          console.log(arguments, "6666666666");
          form.setFieldsValue({
            itemId: rowData[0].itemId,
          });
        },
        change(event, value, rowData, form) {
          console.log(arguments, "8888888");
          if (!value) {
            form.setFieldsValue({
              itemId: "",
            });
          }
        },
      },
    },
    { type: "input", name: "warehouseLocatorId", label: "货位ID", hidden: true },
    {
      type: "search",
      name: "warehouseLocatorName",
      label: "货位",
      targetValueKey: "warehouseLocatorName",
      destroyOnClose: true,
      targetClick: true,
      span: 8,
      value: "",
      toolBar: {
        items: [
          {
            type: "input",
            name: "warehouseLocatorName",
            label: "货位名称",
            span: 8,
            value: "",
          },
          {
            type: "input",
            name: "warehouseLocatorCode",
            label: "货位编码",
            span: 8,
            value: "",
          },
        ],
      },
      modalOpenChange: (open) => {
        let values = that.$refs.detial.$refs.forms.form.getFieldsValue();
        if (!values.warehouseId) {
          // 仓库
          that.$message.warn("请选择仓库");
          return false;
        } else {
          return true;
        }
      },
      tableConfig: {
        rowKey: "warehouseLocatorId",
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: "warehouseLocatorName", title: "货位名称", key: "warehouseLocatorName" },
          { dataIndex: "warehouseLocatorCode", title: "货位编码", key: "warehouseLocatorCode" },
          { dataIndex: "warehouseLocatorId", title: "货位ID", key: "warehouseLocatorId" },
        ],
        remoteBefore(res, remoteParams) {
          let values = that.$refs.detial.$refs.forms.form.getFieldsValue();
          if (remoteParams.params) {
            remoteParams.params.warehouseId = values.warehouseId;
          } else {
            remoteParams.params = { warehouseId: values.warehouseId };
          }

          return remoteParams;
        },
      },
      remoteUrl: API.locatorWHPage,
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            warehouseLocatorId: rowData[0].warehouseLocatorId,
          });
        },
        change(event, value, options, form) {
          if (!value) {
            form.setFieldsValue({ warehouseLocatorId: "" });
          }
        },
      },
    },
    { type: "input", name: "manufacturerLotNum", label: "批号" },
  ];
};
const columns = function (self) {
  return [
    { dataIndex: "cargoOwnerName", title: "货主名称", key: "cargoOwnerName" },
    { dataIndex: "itemCode", title: "商品编号", key: "itemCode" },
    { dataIndex: "itemName", title: "商品名称", key: "itemName" },
    { dataIndex: "manufacturerLotNum", title: "批号", key: "manufacturerLotNum" },
    { dataIndex: "locatorCode", title: "货位编码", key: "locatorCode" },
    { dataIndex: "containerCode", title: "容器号", key: "containerCode" },
    { dataIndex: "availableQuantity", title: "可用库存数量", key: "availableQuantity" },
    {
      dataIndex: "lockType",
      title: "锁定类型",
      key: "lockType",
      //   customRender(text, record, index) {
      //     text == 1 ? ((text = "自动锁定") ? text == 2 : (text = "手动锁定")) : "";
      //     return text;
      //   },
    },
    {
      dataIndex: "lockReason",
      title: "锁定原因",
      key: "lockReason",
      //   ...remote("WMS_INV_STOCK_LOCK_REASON"),
    },
    // { dataIndex: "oddCaseQuantity", title: "可锁定件数", key: "oddCaseQuantity" },
    // { dataIndex: "oddPieceQuantity", title: "可锁零散数", key: "oddPieceQuantity" },
    { dataIndex: "batchProductionDate", title: "生产日期", key: "batchProductionDate" },
    { dataIndex: "batchExpiredDate", title: "有效期至", key: "batchExpiredDate" },
    { dataIndex: "batchNum", title: "批次", key: "batchNum" },
  ];
};

export default {
  components: { searchTable },
  data() {
    return {
      spinning:false,
      items: items(this),
      columns: columns(this),
      title: "库存解锁",
      tableRowKey: "id",
      selectedRowKeys: [],
      ajaxId: "581848808753860608",
      tableRemoteUrl: {
        url: upcloud_base_inventory + "/stock-lot/v1/lockData",
        method: "get",
        headers: {
          "Content-Type": "application/JSON",
        },
      },
      btnOptions: [
        {
          name: "解锁",
          icon: "",
          action: "save",
        },
      ],
      // 弹窗
      modalVisible: false,
      modalForm: {
        char1: "",
      },
      conclusionOptions: [
        { label: "合格解锁", value: "1" },
        { label: "转GSP质量复检", value: "2" },
      ],
    };
  },
  created() {
    console.log();
    diffOption(columns(this)).then((res) => {
      this.columns = sorter(res);
    });
    this.getAuthBycargoOwner();
  },
  mounted() {
    // warehouseId = this.$store.state.user.warehouse.warehouseId
    this.$refs.detial.$refs.forms.form.setFieldsValue({
      warehouseId: this.$store.state.user.warehouse.warehouseId,
      warehouseName: this.$store.state.user.warehouse.warehouseName,
    });
  },
  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;
        }
      });
    },
    save() {
      // 解锁
      if (this.selectedRowKeys.length === 0) return this.$message.warn("请选择解锁商品!");
      this.modalVisible = true;
    },
    lock() {
      const { iqcstatus } = this.modalForm;
      console.log(iqcstatus);
      if (!iqcstatus) return this.$message.warn("请选择解锁结论");
      let params = this.selectedRowKeys.map((i) => {
        console.log(i);
        return {
          lockRecordId: i,
          unlockType: iqcstatus,
        };
      });
      console.log(params);
      //   return;
      this.spinning = true
      unlock(params).then((res) => {
        this.spinning = false
        if (res.success) {
          this.$refs.detial.$refs.table.reload();
          this.$message.success(res.message);
        } else {
          this.$exception(res);
        }
      });
    },
    handleOk() {
      this.lock();
      this.modalVisible = false;
    },
    handleCancel() {
      this.modalVisible = false;
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRowKeys = selectedRowKeys;
    },
  },
};
</script>

<style></style>
