<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="企业id" prop="corpId">
          <el-input v-model="formData.corpId" placeholder="请输入企业id" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="formData.deptId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="存放位置" prop="posId">
          <el-input v-model="formData.posId" placeholder="请输入存放位置" />
        </el-form-item>
        <el-form-item label="使用人id" prop="userId">
          <el-input v-model="formData.userId" placeholder="请输入使用人id" />
        </el-form-item>
        <el-form-item label="资产编号" prop="propNo">
          <el-input v-model="formData.propNo" placeholder="请输入资产编号" />
        </el-form-item>
        <el-form-item label="资产名称" prop="propName">
          <el-input v-model="formData.propName" placeholder="请输入资产名称" />
        </el-form-item>
        <el-form-item label="资产分类" prop="propCatg">
          <el-input v-model="formData.propCatg" placeholder="请输入资产分类" />
        </el-form-item>
        <el-form-item label="预计使用年限" prop="useYear">
          <el-input v-model="formData.useYear" placeholder="请输入预计使用年限" />
        </el-form-item>
        <el-form-item label="价值类型" prop="costType">
          <el-select v-model="formData.costType" placeholder="请选择价值类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMPANY_COST_TYPE)" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="资产数量" prop="propNum">
          <el-input v-model="formData.propNum" placeholder="请输入资产数量" />
        </el-form-item>
        <el-form-item label="资产原值（元）" prop="costTotal">
          <el-input v-model="formData.costTotal" placeholder="请输入资产原值（元）" />
        </el-form-item>
        <el-form-item label="资产状态" prop="propStatus">
          <el-select v-model="formData.propStatus" placeholder="请选择资产状态">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMPANY_PROP_STATUS)" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="formData.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="规格型号" prop="spec">
          <el-input v-model="formData.spec" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="生产厂家" prop="factory">
          <el-input v-model="formData.factory" placeholder="请输入生产厂家" />
        </el-form-item>
        <el-form-item label="出场序列号/编号" prop="serialNo">
          <el-input v-model="formData.serialNo" placeholder="请输入出场序列号/编号" />
        </el-form-item>
        <el-form-item label="数量计量单位" prop="unit">
          <el-input v-model="formData.unit" placeholder="请输入数量计量单位" />
        </el-form-item>
        <el-form-item label="取得日期" prop="getDate">
          <el-date-picker clearable v-model="formData.getDate" type="date" value-format="timestamp"
            placeholder="选择取得日期" />
        </el-form-item>
        <el-form-item label="出厂日期" prop="prodDate">
          <el-date-picker clearable v-model="formData.prodDate" type="date" value-format="timestamp"
            placeholder="选择出厂日期" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="formData.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="启用日期" prop="openDate">
          <el-date-picker clearable v-model="formData.openDate" type="date" value-format="timestamp"
            placeholder="选择启用日期" />
        </el-form-item>
        <el-form-item label="净值（元）" prop="netValue">
          <el-input v-model="formData.netValue" placeholder="请输入净值（元）" />
        </el-form-item>
        <el-form-item label="凭证号" prop="voucherNo">
          <el-input v-model="formData.voucherNo" placeholder="请输入凭证号" />
        </el-form-item>
        <el-form-item label="维修/保养周期单位" prop="keepCycleType">
          <el-select v-model="formData.keepCycleType" placeholder="请选择维修/保养周期单位">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="维修/保养周期" prop="keepCycle">
          <el-input v-model="formData.keepCycle" placeholder="请输入维修/保养周期" />
        </el-form-item>
        <el-form-item label="上次维修/保养日期" prop="lastKeepDate">
          <el-date-picker clearable v-model="formData.lastKeepDate" type="date" value-format="timestamp"
            placeholder="选择上次维修/保养日期" />
        </el-form-item>
        <el-form-item label="下次维修/保养日期" prop="nextKeepDate">
          <el-date-picker clearable v-model="formData.nextKeepDate" type="date" value-format="timestamp"
            placeholder="选择下次维修/保养日期" />
        </el-form-item>
        <el-form-item label="附件urls">
          <FileUpload v-model="formData.fileUrls" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as PropertyApi from '@/api/base/property/property';
import FileUpload from '@/components/FileUpload';
export default {
  name: "PropertyForm",
  components: {
    FileUpload,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        corpId: undefined,
        deptId: undefined,
        posId: undefined,
        userId: undefined,
        propNo: undefined,
        propName: undefined,
        propCatg: undefined,
        useYear: undefined,
        costType: undefined,
        propNum: undefined,
        costTotal: undefined,
        propStatus: undefined,
        brand: undefined,
        spec: undefined,
        factory: undefined,
        serialNo: undefined,
        unit: undefined,
        getDate: undefined,
        prodDate: undefined,
        supplier: undefined,
        openDate: undefined,
        netValue: undefined,
        voucherNo: undefined,
        keepCycleType: undefined,
        keepCycle: undefined,
        lastKeepDate: undefined,
        nextKeepDate: undefined,
        fileUrls: undefined,
      },
      // 表单校验
      formRules: {
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await PropertyApi.getProperty(id);
          this.formData = res.data;
          this.title = "修改企业管理-资产";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增企业管理-资产";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await PropertyApi.updateProperty(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await PropertyApi.createProperty(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        corpId: undefined,
        deptId: undefined,
        posId: undefined,
        userId: undefined,
        propNo: undefined,
        propName: undefined,
        propCatg: undefined,
        useYear: undefined,
        costType: undefined,
        propNum: undefined,
        costTotal: undefined,
        propStatus: undefined,
        brand: undefined,
        spec: undefined,
        factory: undefined,
        serialNo: undefined,
        unit: undefined,
        getDate: undefined,
        prodDate: undefined,
        supplier: undefined,
        openDate: undefined,
        netValue: undefined,
        voucherNo: undefined,
        keepCycleType: undefined,
        keepCycle: undefined,
        lastKeepDate: undefined,
        nextKeepDate: undefined,
        fileUrls: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
