<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="评价年份" prop="yearOfEvaluation"
                :rules="[
                  {required: true, message:'评价年份不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      :disabled="method === 'view' || method === 'edit'"
                      style="width: 100%;"
                      v-model="inputForm.yearOfEvaluation"
                      type="year"
                      value-format="yyyy"
                      placeholder="选择年份">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单位名称" prop="unitName"
                :rules="[
                  {required: true, message:'单位名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.unitName" placeholder="请填写单位名称"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view' && method!='edit'" @click="add()" v-noMoreClick>添加</el-button>、
      <el-button type="primary" v-if="method != 'view' && method!='add'" @click="update()" v-noMoreClick>修改</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          yearOfEvaluation: '',
          unitName: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建施工单位`
        } else if (method === 'edit') {
          this.title = '修改施工单位'
        } else if (method === 'view') {
          this.title = '查看施工单位'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/constructionUnit/constructionUnit/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.constructionUnit)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      add () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/constructionUnit/constructionUnit/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      update () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/constructionUnit/constructionUnit/update`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }

    }
  }
</script>
