<!--
 * @Author: liuzl 461480418@qq.com
 * @Date: 2023-01-09 15:34:20
 * @LastEditors: liuzhenlin
 * @LastEditTime: 2023-01-09 16:13:14
 * @Description: file content
 * @FilePath: \订单全流程管理系统\src\views\contract\components\EditPlan.vue
-->
<template>
  <el-dialog :title="title" :visible.sync="editVisible" @close="handleClose">
    <el-form ref="editForm" :model="editForm" :rules="editRules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合同编号" prop="contractCode">
            <el-input v-model="editForm.contractCode" disabled placeholder="请输入合同编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划回款金额" prop="planAmount">
            <el-input v-model.number="editForm.planAmount" clearable placeholder="请输入计划回款金额" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="计划回款日期" prop="planDatetime">
            <el-date-picker
              v-model="editForm.planDatetime"
              placeholder="选择计划回款日期"
              style="width: 100%"
              type="date"
              value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="editForm.remark" clearable placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button v-show="!editForm.id" type="primary" @click="collectionPlanSave">保存</el-button>
      <el-button v-show="editForm.id" type="primary" @click="contractEdit">保存</el-button>
      <el-button @click="editVisible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import to from 'await-to-js'
  import collectionPlanApi from '@/api/contract/collectionPlan'
  export default {
    props: {
      details: {
        type: Object,
        default: () => {},
      },
    },
    data() {
      return {
        title: '新建回款计划',
        editVisible: false,
        editForm: {
          planDatetime: '', //计划回款日期
          contractCode: '', //合同编号
          contractId: null, //合同id
          planAmount: '', //计划回款金额
          remark: '', //备注
        },
        editRules: {
          contractCode: [{ required: true, trigger: 'blur', message: '请选择合同' }],
          planAmount: [{ required: true, trigger: 'blur', message: '请输入计划回款金额' }],
          planDatetime: [
            { required: true, trigger: 'change', validator: this.pickerOptionsStart, message: '请选择计划回款日期' },
          ],
        },
      }
    },
    mounted() {},
    methods: {
      async init(id) {
        if (!id) {
          this.title = '新建回款计划'
          if (this.details) {
            this.editForm.contractCode = this.details.contractCode
            this.editForm.contractId = this.details.id
          }
        } else {
          this.title = '编辑回款计划'
          const [err, res] = await to(collectionPlanApi.getDetails({ id }))
          if (err) return
          if (res.data) {
            this.editForm = res.data
          }
        }
        this.editVisible = true
      },
      // 保存回款计划
      async collectionPlanSave() {
        let params = { ...this.editForm }
        const [valid] = await to(this.$refs.editForm.validate())
        if (valid == false) return
        const [err, res] = await to(collectionPlanApi.addCollectionPlan(params))
        if (err) return
        if (res.code == 200) this.$message.success(res.msg)
        else return
        this.editVisible = false
        this.$emit('collectionPlanSave')
      },
      // 编辑回款计划
      async contractEdit() {
        let params = { ...this.editForm }
        const [valid] = await to(this.$refs.editForm.validate())
        if (valid == false) return
        const [err, res] = await to(collectionPlanApi.updateCollectionPlan(params))
        if (err) return
        if (res.code == 200) this.$message.success(res.msg)
        else return
        this.editVisible = false
        this.$emit('collectionPlanSave')
      },
      handleClose() {
        this.editForm = {
          planDatetime: '', //计划回款日期
          contractCode: '', //合同编号
          contractId: null, //合同id
          planAmount: '', //计划回款金额
          remark: '', //备注
        }
        this.$refs.editForm.resetFields()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .mb10 {
    margin-bottom: 10px;
  }
  .proj-col {
    text-align: right;
  }
</style>
