<script>
import { TableColumns } from "./column";
import { initDicts, initColumns } from "@/utils/init";
import { FIRSTDIRECT, ADD } from "@/api/business/purchase/task";
export default {
  name: "FirstDirectPurchaseDrawer",
  dicts: initDicts(TableColumns),
  components: {},
  data() {
    return {
      size: "mini",
      title: "首次协议直采",
      width: "100%",
      column: 3,
      visible: false,
      loading: false,
      tableColumns: initColumns(TableColumns),
      data: [],
    };
  },
  computed: {},
  watch: {},
  methods: {
    //
    async open(prop) {
      this.visible = await this.fetchItem(prop);
    },
    //
    hide() {
      this.visible = false;
    },
    //
    async fetchItem(prop) {
      try {
        // try
        this.loading = true;
        const { code, data } = await FIRSTDIRECT(prop);
        if (code === 200) {
          this.data = data;
          return true;
        } else {
          return false;
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
    //
    async submit(prop) {
      const params = prop
        .map((item) => ({
          ...item,
          orderPriceVos: item.orderPriceVos.filter(
            (citem) => citem.purchaseQuantity
          ),
        }))
        .filter((item) => item.orderPriceVos.length);
      try {
        // try
        const { msg, code } = await ADD(params);
        if (code === 200) {
          this.hide();
          this.$emit("success");
          this.$notify.success({ title: msg, duration: 3000 });
        }
      } catch (err) {
        // catch
      } finally {
        // finally
      }
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>

<template>
  <el-drawer
    :size="width"
    :title="title"
    :show-close="false"
    :visible.sync="visible"
  >
    <template slot="title">
      <span>{{ title }}</span>
      <span>
        <el-button
          :size="size"
          circle
          icon="el-icon-close"
          @click="visible = false"
        ></el-button>
        <el-button
          :size="size"
          circle
          icon="el-icon-check"
          @click="submit(data)"
        >
        </el-button>
      </span>
    </template>
    <div v-for="(item, index) in data" :key="index" style="margin: 10px">
      <el-descriptions :size="size" :column="column" border>
        <template #title>
          <span style="margin-right: 10px">{{ item.materialName }}</span>
          <span style="color: tomato">{{ item.puQty }}</span>
          <span> {{ item.puUnitName }}</span>
        </template>
        <el-descriptions-item label="需求组织">
          {{ item.demandOrgName }}
        </el-descriptions-item>
        <el-descriptions-item label="采购组织">
          {{ item.puOrgName }}
        </el-descriptions-item>
        <el-descriptions-item label="需求时间">
          {{ item.demandDate }}
        </el-descriptions-item>
      </el-descriptions>
      <el-table
        v-loading="loading"
        :size="size"
        :data="item.orderPriceVos"
        style="width: 100%"
      >
        <el-table-column
          v-for="(cItem, cIndex) in tableColumns"
          :key="cIndex"
          :prop="cItem.key"
          :label="cItem.title"
          :fixed="cItem.fixed"
          :width="cItem.width || 180"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-input-number
              v-if="cItem.inputType === 'InputNumber'"
              v-model="scope.row[cItem.key]"
              :size="size"
              :min="cItem.min(item)"
              :max="cItem.max(item)"
              :placeholder="cItem.placeholder"
              :controls-position="cItem.controlsPosition"
              style="width: 90%"
            ></el-input-number>
            <el-date-picker
              v-else-if="cItem.inputType === 'DatePicker'"
              v-model="scope.row[cItem.key]"
              :size="size"
              :type="cItem.type"
              :placeholder="cItem.placeholder"
              :value-format="cItem.valueFormat"
              :picker-options="cItem.pickerOptions"
              style="width: 90%"
            ></el-date-picker>
            <el-input
              v-else-if="cItem.inputType === 'Textarea'"
              v-model="scope.row[cItem.key]"
              :size="size"
              autosize
              type="textarea"
              style="width: 90%"
            ></el-input>
            <dict-tag
              v-else-if="cItem.inputType === 'Select'"
              :size="size"
              :value="scope.row[cItem.key]"
              :options="dict.type[cItem.referName]"
            />
            <span v-else>{{ scope.row[cItem.key] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-drawer>
</template>

<style scoped></style>
