<!--suppress ALL -->
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="contractForm" label-well-code="right" label-width="120px" >
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="外协项目申请" prop="externalProjectApply">
            <el-select v-model="contractForm.externalProjectApply" placeholder="外协项目申请" clearable value="">
              <el-option
                v-for="item in options1"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="外协合同申请" prop="externalContractApply">
            <el-select v-model="contractForm.externalContractApply" placeholder="外协合同申请" clearable value="">
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="外协验收申请" prop="externalCheckApply">
            <el-select v-model="contractForm.externalCheckApply" placeholder="外协验收申请" clearable value="">
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">提交</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { updateFlow } from '@/api/contract'

export default {
  name: 'EditFlow',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      deviceList: null, // 在产设备列表
      contractForm: {
        id: '',
        externalCheckApply: '',
        externalContractApply: '',
        externalProjectApply: ''
      }, // 表单
      options2: [{
        value: '0',
        label: '未提交'
      }, {
        value: '1',
        label: '流程中'
      }, {
        value: '2',
        label: '已完成'
      }],
      options1: [{
        value: '1',
        label: '流程中'
      }, {
        value: '2',
        label: '已完成'
      }],
      equipmentFile: null,
      textMap: {
        update: '编辑内网流程信息',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      deviceTypeList: null, // 设备类型列表
      rules: {
        externalCheckApply: [{ required: true, message: '请选择外协项目申请状态', trigger: 'change' }],
        externalContractApply: [{ required: true, message: '请选择外协合同申请状态', trigger: 'change' }],
        externalProjectApply: [{ required: true, message: '请选择外协验收申请状态', trigger: 'change' }]
      }, // 前端校验规则
      isEditMode: false
    }
  },
  watch: {
  },
  mounted() {
  },
  created() {
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.contractForm = {
          id:  parseInt(row.id),
          externalCheckApply: row.externalCheckApply,
          externalContractApply: row.externalContractApply,
          externalProjectApply: row.externalProjectApply
        }
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.equipmentFile = null
      this.contractForm = {
        id: '',
        externalCheckApply: '',
        externalContractApply: '',
        externalProjectApply: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        //this.createData()
      }
    },

    // 修改数据
    updateData: function() {
      console.log('!!!!!!!!!!!!!!!!!!!!!!!!')
      console.log(this.contractForm)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateFlow(this.contractForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .labeltext{
    vertical-align: -webkit-baseline-middle;
  }
</style>
