<template>
  <el-dialog title="改期" :visible.sync="dialogFormVisible" width="600px" @close="close">
    <el-form v-if="detail" ref="form" label-position="top" :model="form" :rules="rules">
      <el-row :gutter="20">
        <!-- 技术支持 -->
        <el-col v-if="detail.orderTypeName == '技术文件支持'" :span="24">
          <el-form-item label="期望完成时间" prop="expectTime">
            <el-date-picker
              v-model="form.expectTime"
              placeholder="选择期望完成时间"
              style="width: 100%"
              type="date"
              value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <!-- end -->
        <!-- 售前 -->
        <el-col v-if="detail.orderTypeName == '售前讲解支持'" :span="24">
          <el-form-item label="支持时间" prop="supportTime">
            <el-date-picker
              v-model="form.supportTime"
              placeholder="选择支持时间"
              style="width: 100%"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss" />
          </el-form-item>
        </el-col>
        <!-- end -->
        <!-- 修改试用时间 -->
        <el-col
          v-if="detail.orderTypeName == '产品试用申请（硬件）' || detail.orderTypeName == '产品试用申请（软件）'"
          :span="24">
          <el-form-item label="试用时间" prop="trialTime">
            <el-date-picker
              v-model="form.trialTime"
              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 to from 'await-to-js'
  import workOrderApi from '@/api/work/index'
  export default {
    name: 'WorkOrderFeedback',
    data() {
      return {
        detail: null,
        form: {
          expectTime: '',
          supportTime: '',
          trialTime: [],
        },
        rules: {
          expectTime: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
          supportTime: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
          trialTime: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
        },
        dialogFormVisible: false,
      }
    },

    mounted() {},
    methods: {
      open(row) {
        this.detail = row
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let params = {}
            if (
              this.detail.orderTypeName == '产品试用申请（硬件）' ||
              this.detail.orderTypeName == '产品试用申请（软件）'
            ) {
              params = {
                orderId: this.detail.id,
                trialTimeEnd: this.form.trialTime[1],
                trialTimeStart: this.form.trialTime[0],
              }
              const [err, res] = await to(workOrderApi.changeTryTime(params))
              if (err) return
              if (res.code == 200) {
                this.$message({
                  type: 'success',
                  message: '修改成功!',
                })
                this.close()
                this.$emit('update-detail')
              }
            } else if (this.detail.orderTypeName == '技术文件支持') {
              params = {
                orderId: this.detail.id,
                expectTime: this.form.expectTime,
              }
              const [err, res] = await to(workOrderApi.changeSupportTime(params))
              if (err) return
              if (res.code == 200) {
                this.$message({
                  type: 'success',
                  message: '修改成功!',
                })
                this.close()
                this.$emit('update-detail')
              }
            } else if (this.detail.orderTypeName == '售前讲解支持') {
              params = {
                orderId: this.detail.id,
                supportTime: this.form.supportTime,
              }
              const [err, res] = await to(workOrderApi.changeSaleTime(params))
              if (err) return
              if (res.code == 200) {
                this.$message({
                  type: 'success',
                  message: '修改成功!',
                })
                this.close()
                this.$emit('update-detail')
              }
            }
          }
        })
      },
    },
  }
</script>
