<!-- 采购需求处理-详情 -->
<script>
import useColumns from "./columns";
import {
  getSummaryDetail,
  shutDownSummary,
  editSummaryMx,
  reloadBatch,
} from "@/api/purchase/DemandSummary.js";
export default {
  name: "DemandSummaryAdd",
  props: {
    dict: {
      type: Object,
    },
    data: {
      type: Object,
    },
  },
  components: {
    CloseLineBtn: () => import("../closeLine/index.vue"),
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
    ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
  },
  data() {
    const { TableColumns } = useColumns();
    return {
      width: "100%",
      title: "详 情",
      isEdit: false,
      visible: false,
      loading: false,
      params: [],
      TableColumns,
      selectData: [],
    };
  },
  computed: {},
  methods: {
    onHide() {
      this.visible = false;
      this.params = [];
      this.selectData = [];
      this.$emit("refresh");
    },
    setVisible(prop) {
      this.visible = prop;
    },
    beforeOpen() {
      this.fetchItem();
    },
    async fetchItem() {
      try {
        this.loading = true;
        let { code, msg, data } = await getSummaryDetail({
          ...this.$props.data,
        });
        if (code == 200) {
          this.params = data;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    useSelect(prop) {
      this.selectData = prop;
    },
    // 编辑
    useEdit() {
      this.isEdit = true;
    },
    // 取消
    useCancel() {
      this.isEdit = false;
      this.fetchItem();
    },
    // 保存
    async useSave() {
      try {
        const params = [...this.params];
        console.log(params, "params");
        let { code, msg } = await editSummaryMx(params);
        if (code == 200) {
          this.$modal.notifySuccess(msg);
          this.isEdit = false;
          this.fetchItem();
        }
      } catch (error) {}
    },
    // 重取批量
    refetchBatch() {
      this.$modal
        .confirm("是否更新明细的最小包装量,最小订货量,最小批量?")
        .then(async () => {
          let ids = this.params.map((item) => item.demandItemId);
          let { code, msg } = await reloadBatch(ids);
          if (code == 200) {
            this.$modal.notifySuccess(msg);
            this.fetchItem();
          }
        })
        .catch(() => {});
    },
    // 补单标识
    changeReplenishment(prop, row) {
      if (prop === "N") {
        row.additionalSupplier = null;
        row.additionalSupplierName = null;
      }
    },
  },
  created() {},
};
</script>

<template>
  <el-drawer
    v-loading="loading"
    v-bind="$attrs"
    v-on="$listeners"
    :size="width"
    :show-close="false"
    :visible.sync="visible"
    destroy-on-close
    @close="onHide"
    @open="beforeOpen"
  >
    <div
      slot="title"
      style="display: flex; justify-content: space-between; align-items: center"
    >
      <h3>{{ title }}</h3>
      <el-button
        v-if="!isEdit"
        :size="$attrs.size"
        :loading="loading"
        @click="onHide"
      >
        返 回
      </el-button>
    </div>

    <el-row class="my-4" style="text-align: right; padding: 0 20px">
      <el-col v-if="isEdit">
        <el-button-group>
          <el-button :size="$attrs.size" @click="useCancel">取 消</el-button>
          <el-button type="primary" :size="$attrs.size" @click="useSave"
            >保 存</el-button
          >
        </el-button-group>
      </el-col>

      <el-col v-else>
        <el-button-group>
          <el-button type="primary" :size="$attrs.size" @click="useEdit"
            >编 辑</el-button
          >
        </el-button-group>

        <el-button-group>
          <close-line-btn
            :size="$attrs.size"
            :select-data="selectData"
            @refresh="fetchItem"
          ></close-line-btn>
        </el-button-group>

        <el-button-group>
          <el-button :size="$attrs.size" @click="refetchBatch"
            >重取批量</el-button
          >
        </el-button-group>
      </el-col>
    </el-row>

    <el-super-ux-table
      v-model="params"
      :size="$attrs.size"
      :dict="dict"
      :columns="TableColumns"
      index
      checkbox
      convenitentOperation
      storage-key="DemandSummaryDetailsSuperTable"
      @row-select="useSelect"
      style="padding: 0 20px 20px"
    >
      <!-- 紧急标识-->
      <template slot="isUrgency" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="!isEdit || scope.row.status !== '1'"
        >
          <el-option
            v-for="item in dict.type[scope.attr.dictName]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </component>
      </template>
      <!-- 补单标识-->
      <template slot="isReplenishment" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="!isEdit || scope.row.status !== '1'"
          @change="(val) => changeReplenishment(val, scope.row)"
        >
          <el-option
            v-for="item in dict.type[scope.attr.dictName]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </component>
      </template>
      <!-- 收货仓库-->
      <template slot="deliveryWarehouseName" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="!isEdit || scope.row.status !== '1'"
        >
        </component>
      </template>
      <!-- 收货货位-->
      <template slot="deliveryAllocationName" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="!isEdit || scope.row.status !== '1'"
        >
        </component>
      </template>
      <!-- 补单供应商-->
      <template slot="additionalSupplierName" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="
            !isEdit ||
            scope.row.status !== '1' ||
            (isEdit && scope.row.isReplenishment === 'N')
          "
        >
        </component>
      </template>
      <!-- 采购组织-->
      <template slot="orgName" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="!isEdit || scope.row.status !== '1'"
        >
        </component>
      </template>
    </el-super-ux-table>
  </el-drawer>
</template>
<style scoped>
.el-button-group + .el-button-group {
  margin: 0 0 0 8px;
}
>>> .el-drawer__header {
  margin-bottom: 0;
  padding: 5px 20px;
}
</style>
