<script>
import { TableColumns } from "../column";
import { ADD } from "@/api/business/purchase/apply";
import { initDicts, initRules, initParams } from "@/utils/init";

export default {
  name: "AddDialog",
  dicts: initDicts(TableColumns),
  components: {},
  data() {
    return {
      size: "mini",
      visible: false,
      loading: false,
      columns: TableColumns,
      rules: initRules(TableColumns),
      params: initParams(TableColumns),
    };
  },
  computed: {},
  watch: {},
  methods: {
    //
    open(prop) {
      this.visible = true;
      this.fetchItem(prop);
    },
    //
    hide() {
      this.visible = false;
      this.params = initParams(this.columns);
    },
    //
    async fetchItem(prop) {
      try {
        this.loading = true;
        this.params = prop;
      } catch (err) {
        //
      } finally {
        this.loading = false;
      }
    },
    //
    submit(prop) {
      this.$refs[prop].validate(async (valid) => {
        if (valid) {
          try {
            this.params.id = undefined;
            const { code, msg } = await ADD({
              ...this.params,
            });
            if (code === 200) {
              this.hide();
              this.$emit("success");
              
            } else {
              this.$notify.warning({ title: msg });
            }
          } catch (err) {
            //
          } finally {
            //
          }
        } else {
          return false;
        }
      });
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-dialog :visible.sync="visible" title="新增" fullscreen @close="hide">
    <el-form
      ref="ruleForm"
      v-loading="loading"
      :size="size"
      :rules="rules"
      :model="params"
      label-width="auto"
      label-position="right"
    >
      <el-row :gutter="20" style="display: flex; flex-wrap: wrap">
        <el-col
          v-for="(column, index) in columns"
          :key="index"
          :span="column.span || 8"
        >
          <el-form-item :prop="column.key" :label="column.title">
            <el-input
              v-if="column.inputType === 'Input'"
              v-model="params[column.key]"
              :disabled="column.disabled"
              :readonly="column.readonly"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              style="width: 100%"
            ></el-input>
            <dr-popover-select
              v-if="column.inputType === 'PopoverSelect'"
              v-model="params[column.key]"
              :source.sync="params"
              :title="column.title"
              :type="column.referName"
              :disabled="column.disabled"
              :readonly="column.readonly"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              :data-mapping="column.dataMapping"
              style="width: 100%"
            >
            </dr-popover-select>
            <el-select
              v-if="column.inputType === 'Select'"
              v-model="params[column.key]"
              :disabled="column.disabled"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              style="width: 100%"
            >
              <el-option
                v-for="item in dict.type[column.referName]"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <file-upload
              v-if="column.inputType === 'Upload'"
              v-model="params[column.key]"
              :file-type="column.fileType"
            ></file-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align: right">
      <el-button :size="size" @click="hide">取 消</el-button>
      <el-button :size="size" @click="submit('ruleForm')">新 增</el-button>
    </div>
  </el-dialog>
</template>
