<!-- 添加/编辑 -->
<template>
  <el-dialog :title="title" :visible.sync="isShow" :close-on-click-modal="false" class="drawer-box" width="700px" @close="handleCancel" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="150px" label-position="top">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司名称">
            <CommonName str="cn" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" prop="deptId">
            <el-cascader clearable v-model="form.deptId" size="small" filterable class="wd100" :options="$store.getters.userDeptTree" :props="$store.getters.newDefaultProps"></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="预算年度" prop="budgetYear">
            <el-date-picker v-model="form.budgetYear" class="wd100" type="year" placeholder="选择年">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预算金额" prop="amount">
            <el-input-number class="wd100" :controls="false" size="small" v-model="form.amount" placeholder="请输入">
            </el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" @click="handleCancel" size="small">取消</el-button>
      <el-button type="primary" :loading="btnLoading" size="small" @click="handleSave">{{!id?'添加':'编辑'}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createBudget, updateBudget, deleteBudget, getBudget, getBudgetPage, exportBudgetExcel } from "@/api/bs/budget";
import VerifyFunc from '@/utils/verity'
import dayjs from 'dayjs';
export default {
  name: '',
  props: {
    id: [String, Number],
    isOpen: Boolean,
  },
  components: {
    CommonName: () => import('@/components/CommonName'),
  },
  watch: {
    isOpen: {
      handler(val) {
        this.isShow = val
        this.title = this.id ? '编辑预算' : '添加预算'
        if (val) {
          this.handleInitForm()
        }
      },
      deep: true,
      immediate: true
    }
  },

  data() {
    return {

      btnLoading: false,
      isShow: false,
      rules: {
        budgetYear: [{ required: true, message: "预算年度不能为空", trigger: "blur" }],
        deptId: [{ required: true, message: "部门", trigger: "change" }],
        amount: [{ required: true, message: "预算金额不能为空", trigger: "blur" }, { validator: VerifyFunc.newNumber }],
      },
      form: {
        deptId: null,
        budgetYear: undefined,
        amount: undefined,
      },
      nameArr: [],
      fileList: [],
      isDisabled: true,
    }
  },

  methods: {
    handlePayCancel() {
      this.payShow = false
    },
    handleBeginScan() {
      if (!(this.fileList && this.fileList.length)) {
        return this.$fm('请上传营业执照！')
      }
    },
    // id 不存在 新增 否则编辑
    handleInitForm(id = this.id) {
      // 初始化参数
      if (!id) {
        this.form = {
          deptId: null,
          budgetYear: undefined,
          amount: undefined,
        }
        this.$nextTick(() => {
          this.$refs.form && this.$refs.form.resetFields()
          this.$refs.form && this.$refs.form.clearValidate()
        })
      } else {
        getBudget(id).then(res => {
          this.form = {
            ...(res.data || {}),
            budgetYear: new Date(dayjs(res.data.budgetYear))
          }
          this.$nextTick(() => {
            this.$refs.form && this.$refs.form.clearValidate()
          })
        })
      }
    },
    handleClose() {
      this.$emit('onClose')
    },
    handleSave() {
      this.$nextTick(() => {
        this.$refs.form.validate((bool) => {
          if (bool) {
            // 编辑
            let form = {
              ...this.form,
              budgetYear: dayjs(this.form.budgetYear).valueOf()
            }
            if (this.id) {
              this.btnLoading = true
              updateBudget(form).then((res) => {
                this.btnLoading = false
                this.$modal.msgSuccess(`编辑成功啦！`);
                this.$emit('onConfirm')
              }).catch(() => {
                this.btnLoading = false
              })
            } else {
              this.btnLoading = true
              createBudget(form).then((res) => {
                this.btnLoading = false
                this.$emit('onConfirm')
                this.$modal.msgSuccess(`新增成功啦！`);
              }).catch(() => {
                this.btnLoading = false
              })
            }

          } else {
            this.$nextTick(() => {
              this.$scErr()
            })
          }
        })
      })
    },
    handleCancel() {
      this.$emit('onCancel')
    },
    handleRemove(file, files) {
      this.fileList = []
      this.form.logoUrl = null
    },
    onUploadChange(file, files) {
      if (['image/png', 'image/jpeg'].includes(file.raw.type)) {
        if (file.size / 1024 > 5000) {
          setTimeout(() => {
            this.$fm('文件超过5Mb，请压缩后上传!')
          }, 10)
          files.splice(files.length - 1, 1)
          return
        }
        this.fileList = []
        this.fileList.push(file)
      } else {
        this.$fm('不支持此文件类型，请重试！')
      }
    },
  }
}

</script>
<style lang='scss' scoped>
@import '@/assets/styles/edit/index.scss';
</style>