<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="提料时间" prop="tlTime">
      <el-date-picker
        :disabled="num === 2"
        v-model="dataForm.tlTime"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="提料人" prop="tlUserId">
      <template v-for="item in sysUserList" v-if="dataForm.tlUserId === item.userId">
        {{ item.authenticName }}
      </template>
    </el-form-item>
    <el-form-item label="立项名称" prop="tlProjectId">
      <el-select
        :disabled="num === 2"
        v-model="dataForm.tlProjectId"
        placeholder="选择立项"
        style="width: 90%;"
      >
        <el-option
          v-for="item in lxList"
          :key="item.projectId"
          :label="item.project"
          :value="item.projectId"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="要求完成时间" prop="tlPlanEndTime">
      <el-date-picker
        :disabled="num === 2"
        v-model="dataForm.tlPlanEndTime"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="提料内容" prop="tlNr">
      <el-input :disabled="num === 2" v-model="dataForm.tlNr" placeholder="提料内容"></el-input>
    </el-form-item>
      <div v-if="num === 1 || (dataForm.tlEndUserId !== null && num === 2)">
        <el-form-item label="实际完成时间" prop="tlEndTime">
          <el-date-picker
            :disabled="num === 2"
            v-model="dataForm.tlEndTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成人" prop="tlEndUserId">
          <template v-for="item in sysUserList" v-if="dataForm.tlEndUserId === item.userId">
            {{ item.authenticName }}
          </template>
        </el-form-item>
        <el-form-item label="完成内容" prop="tlEndSm">
          <el-input :disabled="num === 2" v-model="dataForm.tlEndSm" placeholder="完成说明"></el-input>
        </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="deleteForm()">删除</el-button>
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
      <el-button type="primary" @click="tjSubmit()">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        num: 0,
        dataForm: {
          id: 0,
          tlTime: '',
          tlUserId: '',
          tlProjectId: '',
          tlPlanEndTime: '',
          tlNr: '',
          tlEndTime: '',
          tlEndUserId: '',
          tlEndSm: '',
          status: 0,
          type: 0
        },
        userId: '',
        authenticName: '',
        sysUserList: [],
        lxList: [],
        dataRule: {
          tlTime: [
            { required: true, message: '提料时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, num) {
        this.num = num
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessplantl/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tlTime = data.businessPlanTl.tlTime
                this.dataForm.tlUserId = data.businessPlanTl.tlUserId
                this.dataForm.tlProjectId = data.businessPlanTl.tlProjectId
                this.dataForm.tlPlanEndTime = data.businessPlanTl.tlPlanEndTime
                this.dataForm.tlNr = data.businessPlanTl.tlNr
                this.dataForm.tlEndTime = data.businessPlanTl.tlEndTime
                this.dataForm.tlEndUserId = data.businessPlanTl.tlEndUserId
                this.dataForm.tlEndSm = data.businessPlanTl.tlEndSm
                this.dataForm.status = data.businessPlanTl.status
                this.dataForm.type = data.businessPlanTl.type
                this.dataForm.createTime = data.businessPlanTl.createTime
                this.dataForm.updateTime = data.businessPlanTl.updateTime
                this.dataForm.createUser = data.businessPlanTl.createUser
              }
            })
          }
        })
        // 获取登录人信息
        this.$http.request({
          url: this.$http.adornUrl('/sys/user/info'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.userId = data.user.userId
            this.authenticName = data.user.authenticName
            this.dataForm.tlUserId = this.userId
            if (this.num === 1) {
              this.dataForm.tlEndUserId = this.userId
            }
          }
        })
        // 获取系统的所有用户 仅限id 真实姓名
        this.$http.request({
          url: this.$http.adornUrl('/business/businessplantl/sysUserlist'),
          method: 'get'
        }).then(({data}) => {
          console.log(data.page)
          if (data && data.code === 0) {
            this.sysUserList = data.page
          }
        })
        // 获取所有的立项信息
        this.$http.request({
          url: this.$http.adornUrl('/business/businessplan/projectAllList/'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.lxList = data.page
            this.lxList.forEach((item) => {
              if (item.projectNewName !== null) {
                item.project = item.projectNewName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessplantl/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'tlTime': this.dataForm.tlTime,
                'tlUserId': this.dataForm.tlUserId,
                'tlProjectId': this.dataForm.tlProjectId,
                'tlPlanEndTime': this.dataForm.tlPlanEndTime,
                'tlNr': this.dataForm.tlNr,
                'tlEndTime': this.dataForm.tlEndTime,
                'tlEndUserId': this.dataForm.tlEndUserId,
                'tlEndSm': this.dataForm.tlEndSm,
                'status': this.dataForm.status,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 提交
      tjSubmit () {
        this.$http.request({
          url: this.$http.adornUrl('/business/businessplantl/update'),
          method: 'post',
          body: this.$http.adornData({
            'id': this.dataForm.id,
            'tlTime': this.dataForm.tlTime,
            'tlUserId': this.dataForm.tlUserId,
            'tlProjectId': this.dataForm.tlProjectId,
            'tlPlanEndTime': this.dataForm.tlPlanEndTime,
            'tlNr': this.dataForm.tlNr,
            'tlEndTime': this.dataForm.tlEndTime,
            'tlEndUserId': this.dataForm.tlEndUserId,
            'tlEndSm': this.dataForm.tlEndSm,
            'isFinish': this.dataForm.isFinish,
            'status': this.dataForm.status,
            'type': 1
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 删除
      deleteForm () {
        var ids = [this.dataForm.id]
        this.$http.request({
          url: this.$http.adornUrl('/business/businessplantl/delete'),
          method: 'post',
          body: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
