<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <div class="bos">
        <div class="lab">
          <div class="bsg"></div>
          <div class="tit">基础信息</div>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" clearable :disabled="param.detail" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" prop="num">
              <el-input v-model="form.num" placeholder="自动根据年月日生成" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目周期" prop="dateRange">
              <el-date-picker v-model="form.dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :disabled="param.detail" style="width: 100% !important"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算日" prop="balanceDate">
              <el-date-picker clearable v-model="form.balanceDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择结算日" :disabled="param.detail"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附加说明" prop="contents">
          <el-input v-model="form.contents" type="textarea" placeholder="请输入附加说明" rows="3" clearable :disabled="param.detail" />
        </el-form-item>
      </div>
    </el-form>
    <div class="mfooter" v-if="!param.detail">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: { dateRange: [] },
      rules: {
        projectName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        dateRange: [{ required: true, message: '不能为空', trigger: 'blur' }],
        balanceDate: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  mounted() {
    if (this.param.id) {
      this.ajax({ url: '/work/project/detail/' + this.param.id }).then((response) => {
        response.data.dateRange=[];
        this.form = response.data;
        this.form.dateRange.push(this.form.startDate);
        this.form.dateRange.push(this.form.finishDate);
      });
    }
  },
  methods: {
    submitForm() {
      if (this.form.dateRange) {
        this.form.startDate = this.form.dateRange[0];
        this.form.finishDate = this.form.dateRange[1];
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.ajax({ method: 'post', url: '/work/project/edit', data: this.form }).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            this.ajax({ method: 'post', url: '/work/project/add', data: this.form }).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
