<script>
import { MODEIFYBUYER } from "@/api/business/purchase/task";
export default {
  name: "ModifyBuyerDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  components: {
    ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
  },
  data() {
    return {
      title: "转 派",
      visible: false,
      loading: false,
      params: { id: "", buyer: "", buyerName: "" },
    };
  },
  computed: {
    disabled: {
      get() {
        return this.selectData.length !== 1;
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    open(prop) {
      this.visible = true;
      this.params.id = prop.id;
    },
    //
    hide() {
      this.visible = false;
      this.params.id = "";
      this.params.buyer = "";
      this.params.buyerName = "";
    },
    //
    async submit(prop) {
      try {
        // try
        this.loading = true;
        const { id, buyer, buyerName } = prop;
        const { msg, code } = await MODEIFYBUYER([
          {
            buyer,
            buyerName,
            id: Number(id),
          },
        ]);
        if (code === 200) {
          this.hide();
          this.$emit("success");
          this.$notify.success({ title: msg });
        }
      } catch (err) {
        // catch
      } finally {
        // finally
        this.loading = false;
      }
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>

<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="open(selectData[0])"
  >
    {{ title }}
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="25%"
      append-to-body
      @close="hide"
    >
      <div slot="footer">
        <el-button
          :size="$attrs.size"
          :loading="loading"
          @click="visible = false"
          >取 消</el-button
        >
        <el-button
          type="primary"
          :size="$attrs.size"
          :loading="loading"
          @click="submit(params)"
          >确 认</el-button
        >
      </div>
      <el-alert
        title="转派后，采购任务将会从您的采购任务清单中删除，转移到转派目标人员的已受理采购任务清单中，您确定要转派吗？"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 10px"
      >
      </el-alert>
      <el-form
        :size="$attrs.size"
        :model="params"
        label-width="0px"
        label-position="right"
        style="padding: 0"
      >
        <el-form-item prop="" label="">
          <el-popover-select-v2
            v-model="params.buyerName"
            :source.sync="params"
            :data-mapping="{ buyer: 'code', buyerName: 'name' }"
            valueKey="name"
            referName="CONTACTS_PARAM"
          >
          </el-popover-select-v2>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-button>
</template>

<style scoped></style>
