<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      label-width="135px"
      @close="close"
      @keyup.enter.native="doSubmit"
      :visible.sync="visible">
      <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               label-width="120px" @submit.native.prevent>

        <!--<el-divider content-position="left"><i class="el-icon-document"></i> 项目信息</el-divider>-->
        <el-row  :gutter="0">
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName"
                          :rules="[
                              {required: true, message:'项目名称不能为空', trigger:'blur'},
                              {required: true, message:'项目名称不能为空', trigger:'change'}
                   ]">
              <el-input size="medium" :readonly="true" @focus="openContractForm()" v-model="inputForm.projectName" placeholder="请选择项目名称">
                <el-button slot="append" icon="el-icon-search" @click="openContractForm()"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" prop="projectNumber"
                          :rules="[
                   ]">
              <el-input :disabled="true" v-model="inputForm.projectNumber" placeholder="请填写项目编号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目经理" prop="projectMasterName"
                          :rules="[
                   ]">
              <el-input :disabled="true" v-model="inputForm.projectMasterName" placeholder="请填写项目经理" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目预计时间" prop="planDate"
                          :rules="[
                                {required: true, message:'项目预计时间不能为空', trigger:'blur'}
                   ]">
              <el-date-picker
                style="width: 100%"
                placement="bottom-start"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                v-model="inputForm.planDate"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                @change="forUpdate"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
    <ProjectRecoredChooseCom  ref="projectRecoredChooseCom" @getProject="getContract"></ProjectRecoredChooseCom>
  </div>
</template>

<script>
  import ManHourManagementForm from '@/api/cw/projectRecords/ManHourManagementService'
  import ProjectRecoredChooseCom from '../projectRecords/ProjectRecordsChooseCom'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          projectId: '',
          projectName: '',
          projectNumber: '',
          planDate: [],
          projectMasterName: ''
        }
      }
    },
    ManHourManagementForm: null,
    created () {
      this.manHourManagementForm = new ManHourManagementForm()
    },

    components: {
      ProjectRecoredChooseCom
    },
    methods: {
      forUpdate () {
        this.$forceUpdate()
      },
      init (method, id) {
        this.method = method
        this.inputForm = {
          projectId: '',
          projectName: '',
          projectNumber: '',
          planDate: [],
          projectMasterName: ''
        }
        if (method === 'add') {
          this.title = `新增项目计划时间`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改项目计划时间'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看项目计划时间'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.manHourManagementForm.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
          }
        })
      },
      openContractForm () {
        if (!this.commonJS.isEmpty(this.inputForm.contractId)) {
          // this.$refs.projectInfoForm.init(this.inputForm.contractId)
          this.$refs.projectRecoredChooseCom.init(this.inputForm.contractId)
        } else {
          this.$refs.projectRecoredChooseCom.init()
        }
      },
      getContract (row) {
        this.inputForm.projectName = row.projectName // 项目名称
        this.inputForm.projectId = row.id // 项目id
        this.inputForm.projectNumber = row.projectNumber // 项目编号
        this.inputForm.projectMasterName = row.projectMasterName // 项目经理
        this.clearClientList()
        this.$forceUpdate()
        this.inputForm.cwProjectInfoList = []
      },
      clearClientList () {
        // 项目直接对接联系人列表清除
        this.inputForm.clientList = []
        this.$forceUpdate()
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.manHourManagementForm.save(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
      }
    }
  }
</script>
