<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="office.id"
                          :rules="[
                  {required: true, message:'归属部门不能为空', trigger:'blur'}
                 ]">
              <SelectTree
                ref="office"
                :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                url="/sys/office/treeData?type=2"
                :value="inputForm.office.id"
                :clearable="true"
                :accordion="true"
                @getValue="(value) => {inputForm.office.id=value}"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工" prop="tuser.id"
                          :rules="[
                  {required: true, message:'员工不能为空', trigger:'blur'}
                 ]">
              <user-select :limit='1' :value="inputForm.tuser.id" @getValue='(value) => {inputForm.tuser.id=value}'></user-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属区域" prop="area"
                          :rules="[
                 ]">
              <CityPicker
                style="width:100%"
                :value="inputForm.area"
                :clearable="true"
                :accordion="true"
                @getValue="(value) => {inputForm.area=value}"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假开始日期" prop="beginDate"
                          :rules="[
                  {required: true, message:'请假开始日期不能为空', trigger:'blur'},
                  {validator: validator.date, trigger:'blur'}
                 ]">
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.beginDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假结束日期" prop="endDate"
                          :rules="[
                  {required: true, message:'请假结束日期不能为空', trigger:'blur'},
                  {validator: validator.date, trigger:'blur'}
                 ]">
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.endDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" v-model="inputForm.remarks" 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'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import UserSelect from '@/components/userSelect'
  import CityPicker from '@/components/cityPicker'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          office: {
            id: ''
          },
          tuser: {
            id: ''
          },
          area: '',
          beginDate: '',
          endDate: '',
          remarks: ''
        }
      }
    },
    components: {
      SelectTree,
      UserSelect,
      CityPicker
    },
    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: `/test/one/leaveForm/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.leaveForm)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/test/one/leaveForm/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
            })
          }
        })
      }
    }
  }
</script>
