<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="1500px"
      @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="100px" @submit.native.prevent>
        <el-row :gutter="26">
          <el-col :span="12">
            <el-form-item label="活动" prop="eventName">
              <el-input placeholder="活动" v-model="inputForm.eventName" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input placeholder="姓名" v-model="inputForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input placeholder="手机号" v-model="inputForm.phone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门" prop="officeName">
              <el-select v-model="inputForm.officeName" size="small" placeholder="请选择"  style="width: 100%;">
                <el-option
                  v-for="(item, index) in  columnTypes"
                  :key="index"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </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>
  </div>
</template>

<script>
  import CollectService from '@/api/materialManagement/CollectService'
  import LuckyDrawMemberService from '@/api/luckyDraw/LuckyDrawMemberService'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import MaterialManagementService from '@/api/materialManagement/MaterialManagementService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        columnTypes: [
          {
            label: '工程一部',
            value: '工程一部'
          },
          {
            label: '工程二部',
            value: '工程二部'
          },
          {
            label: '工程三部',
            value: '工程三部'
          },
          {
            label: '工程四部',
            value: '工程四部'
          },
          {
            label: '工程五部',
            value: '工程五部'
          },
          {
            label: '招标代理部',
            value: '招标代理部'
          },
          {
            label: '兴光评估',
            value: '兴光评估'
          },
          {
            label: '其他',
            value: '其他'
          }
        ],
        inputForm: {
          eventId: '',
          id: '',
          officeName: '',
          eventName: '',
          phone: '',
          name: ''
        }
      }
    },
    CollectService: null,
    LuckyDrawMemberService: null,
    MaterialManagementService: null,
    created () {
      this.collectService = new CollectService()
      this.luckyDrawMemberService = new LuckyDrawMemberService()
      this.materialManagementService = new MaterialManagementService()
    },
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      },
      status: {
        type: String,
        default: ''
      }
    },
    components: {
      SelectTree
    },
    computed: {
      bus: {
        get () {
          return this.businessId
        },
        set (val) {
          this.businessId = val
        }
      }
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          eventId: '',
          id: '',
          officeName: '',
          eventName: '',
          phone: '',
          name: ''
        }
        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(() => {
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$refs.inputForm.resetFields()
            this.luckyDrawMemberService.findById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.luckyDrawMemberService.save(this.inputForm).then(({data}) => {
              this.$message.success(data)
              this.$emit('refreshList')
              this.loading = false
              this.close()
            }).catch(() => {
              // this.$refs.inputForm.resetFields()
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
      }
    }
  }
</script>
