<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-position="top" :model="form" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="计划标题" prop="planTitle">
            <el-input v-model="form.planTitle" placeholder="请输入计划标题" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="时间范围" prop="date">
            <el-date-picker
              v-model="form.date"
              end-placeholder="结束日期"
              range-separator="至"
              start-placeholder="开始日期"
              style="width: 100%"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import planApi from '@/api/work/deliverPlan'

  export default {
    name: 'WorkOrderFeedback',
    components: {},
    props: {
      // orderStatusOptions: {
      //   type: Array,
      //   default: () => [],
      // },
    },
    data() {
      return {
        form: {
          deliverOrderId: 0,
          planTitle: undefined,
          date: [],
        },
        rules: {
          planTitle: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
          date: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
        },
        dialogFormVisible: false,
        planId: 0,
        title: '',
      }
    },
    mounted() {},
    methods: {
      open(planId) {
        console.log('planId', planId)
        if (planId) {
          this.planId = planId
          this.title = '编辑'
          this.getPlanDetail()
        } else {
          this.title = '新建计划'
          this.planId = 0
        }
        this.form.deliverOrderId = parseInt(this.$route.query.id)
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      async getPlanDetail() {
        const { data, code } = await planApi.getPlan({ id: this.planId })
        if (code == 200) {
          this.form.date = [data.planStartDate, data.planEndDate]
          this.form.planTitle = data.planTitle
        }
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            console.log(this.form.date)
            const params = {
              deliverOrderId: this.form.deliverOrderId,
              planTitle: this.form.planTitle,
              planStartDate: this.form.date[0],
              planEndDate: this.form.date[1],
            }
            if (this.planId) {
              params.id = this.planId
              const { msg } = await planApi.updatePlan(params)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              const { msg } = await planApi.addPlan(params)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
