<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="采购类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="procurementCategory">
              <j-dict-select-tag type="list" v-model="model.procurementCategory" dictCode="purchasing_categories"
                placeholder="请选择采购类别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="采购方向" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="procurementDirection">
              <a-input v-model="model.procurementDirection" placeholder="请输入采购方向"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="采购内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- 循环渲染 content 数组中的每个对象 -->
              <div v-for="(item, index) in content" :key="index">
                <a-row :gutter="16">
                  <a-col :span="8">
                    <a-input v-model="item.name" placeholder="物品名称" />
                  </a-col>
                  <a-col :span="6">
                    <a-input v-model="item.number" placeholder="数量" />
                  </a-col>
                  <a-col :span="6">
                    <a-input v-model="item.unit" placeholder="单位" />
                  </a-col>
                  <a-col :span="2">
                    <!-- 通过点击按钮触发新增行 -->
                    <a-icon type="plus-circle" @click="addRow(index)" />
                  </a-col>
                  <a-col :span="2">
                    <!-- 通过点击按钮触发删除行 -->
                    <a-icon type="minus-circle" @click="removeRow(index)" />
                  </a-col>
                </a-row>
              </div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="采购预算" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="procurementBudget">
              <a-input v-model="model.procurementBudget" placeholder="请输入采购预算"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="annex">
              <j-upload v-model="model.annex"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" style="display:none">
            <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="annex">
              <j-upload v-model="model.createBy"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" style="display:none">
            <a-form-model-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="annex">
              <j-upload v-model="model.sysOrgCode"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { USER_INFO } from "@/store/mutation-types"
import Vue from 'vue'

export default {
  name: 'PurchaseRequestForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        createBy: Vue.ls.get(USER_INFO).username,
        sysOrgCode: Vue.ls.get(USER_INFO).orgCode,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        procurementCategory: [
          { required: true, message: '请输入采购类别!' },
        ],
        procurementDirection: [
          { required: true, message: '请输入采购方向!' },
        ],
        procurementContent: [
          { required: true, message: '请输入采购内容!' },
        ],
        procurementBudget: [
          { required: true, message: '请输入采购预算!' },
        ],
      },
      url: {
        add: "/Try/purchaseRequest/add",
        edit: "/Try/purchaseRequest/edit",
        queryById: "/Try/purchaseRequest/queryById",
      },
      content: [
        {
          name: '',
          number: '',
          unit: '',
        },
      ]
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    addRow(index) {
      this.content.splice(index + 1, 0, {
        name: '',
        number: '',
        unit: '',
      });
    },
    // 移除指定索引位置的行
    removeRow(index) {
      //如果只有一行了，则不允许删除
      if (this.content.length === 1) {
        return;
      }
      this.content.splice(index, 1);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      //判断procurementContent是否为空，如果为空则新建
      console.log(this.model.procurementContent)
      if (this.model.procurementContent != undefined && this.model.procurementContent != null) {
        this.content = JSON.parse(this.model.procurementContent);
      }
      console.log(this.content)
      this.visible = true;
    },
    submitForm() {
      //将content数组封装为JSON字符串赋予model.procurementContent
      this.model.procurementContent = JSON.stringify(this.content);
      //修改采购内容
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>