<template>
  <a-modal
    title="编辑"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="作业票"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择作业票" v-decorator="['name', {rules: [{ required: true, message: '请选择作业票！' }]}]">
            <a-select-option v-for="(item,index) in nameData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="月份"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-month-picker show-time style="width: 100%" placeholder="请选择月份" v-decorator="['happenTime',{rules: [{ required: true, message: '请选择月份！' }]}]" @change="happenTimeOnChange"/>
        </a-form-item>
        <a-form-item
          label="数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input-number placeholder="请输入数量" style="width: 100%" v-decorator="['totalNum', {rules: [{required: true, message: '请输入数量！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { scrWorkTaskEdit } from '@/api/modular/main/scrworktask/scrWorkTaskManage'
  import moment from 'moment'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        nameData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        const nameOption = this.$options
        this.nameData = nameOption.filters['dictData']('work_task_type')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              name: record.name,
              totalNum: record.totalNum
            }
          )
        }, 100)
        if (record.happenTime) {
            this.form.getFieldDecorator('happenTime', { initialValue: moment(record.happenTime, 'YYYY-MM-DD') })
            this.happenTimeDateString = moment(record.happenTime).format('YYYY-MM-DD')
        }
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.happenTime = moment(this.happenTimeDateString).format('YYYY-MM-DD') || null
            scrWorkTaskEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + res.message)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      happenTimeOnChange(date, dateString) {
        this.happenTimeDateString = dateString
      },
      handleCancel () {
        this.happenTimeDateString = ''
        this.form.getFieldDecorator('happenTime', { initialValue: null })
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
