<!--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="taskForm" label-well-code="right" label-width="100px" >
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="项目名称" prop="projectId">
            <el-select v-model="taskForm.projectId" :disabled="isEditMode" filterable placeholder="项目名称" clearable value="" @change="changeType($event)">
              <el-option
                v-for="item in projectList"
                :key="item.id"
                :label="item.projectName"
                :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="设备类型" prop="devtypeId">
            <el-select v-model="taskForm.devtypeId" :disabled="isEditMode" filterable placeholder="设备类型" clearable value="" value-key="id" @change="changeModel($event)">
              <el-option
                v-for="item in deviceTypeList"
                :key="item.id"
                :label="item.productName"
                :value="item"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="设备型号" prop="devmodeId">
            <el-select v-model="taskForm.devmodeId" :disabled="isEditMode" filterable placeholder="设备型号" clearable value="">
              <el-option
                v-for="item in modelList"
                :key="item.id"
                :label="item.productModel"
                :value="item.productModel"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="设备数量" prop="devcount">
            <el-input v-model.trim="taskForm.devcount" :disabled="isEditMode" type="text" placeholder="必填" oninput="value=value.replace(/^\.+|[^\d.]/g,'')"  @blur="moneyChange">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <el-form-item label="卡类型">
          <el-select v-model="taskForm.cardType" filterable placeholder="卡类型" clearable value="">
            <el-option key="1" label="工业级" value="工业级"/>
            <el-option key="2" label="消费级" value="消费级"/>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="运营商">
            <el-select v-model="taskForm.operator" filterable placeholder="运行商" clearable value="">
              <el-option key="1" label="电信" value="电信"/>
              <el-option key="2" label="移动" value="移动"/>
              <el-option key="3" label="联通" value="联通"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="交付时间">
            <el-date-picker
              v-model="taskForm.deliveryTimeFmt"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="设备要求" >
            <el-input v-model.trim="taskForm.demand" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="!isEditMode">
        <el-col :span="12" :offset="6">
          <el-form-item label="备货任务">
            <template>
              <el-radio v-model="taskForm.isstored"  label="1">是</el-radio>
              <el-radio v-model="taskForm.isstored"  label="0">否</el-radio>
            </template>
          </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 { addTask,updateTask,getProjectList,getModelList,getTypeList } from '@/api/batch'
import { getDeviceList } from '@/api/factory'

export default {
  name: 'EditTask',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      deviceList: null, // 在产设备列表
      devTypeId: '',
      taskForm: {
        id:'',
        projectId: '',
        devtypeId: '',
        devmodeId: '',
        devcount: '',
        deliveryTimeFmt: '',
        demand: '',
        isstored: '0',
        cardType: '',
        operator: ''
      }, // 表单
      equipmentFile: null,
      textMap: {
        update: '任务单编辑',
        create: '任务单新增',
        detail: '详情'
      }, // 表头显示标题
      deviceTypeList: null, // 设备类型列表
      rules: {
        projectId: [{ required: true, message: '请选择项目名称', trigger: ['blur', 'change'] }],
        devtypeId: [{ required: true, message: '请选择设备类型', trigger: ['blur', 'change'] }],
        devmodeId: [{ required: true, message: '请选择设备型号', trigger: ['blur', 'change'] }],
        devcount: [{ required: true, message: '设备数量为不能为空', trigger: ['blur', 'change'] }],
        cardType: [{ required: true, message: '请选择卡类型', trigger: ['blur', 'change'] }],
        operator: [{ required: true, message: '请选择运营商', trigger: ['blur', 'change'] }]
        // contractFactory: [{ required: true, message: '合同乙方不能为空', trigger: [ 'change'] }],
        // principal: [{ required: true, message: '负责人不能为空', trigger: [ 'change'] }]
      }, // 前端校验规则
      isEditMode: false,
      projectList:null,
      modelList:null
    }
  },
  watch: {
  },
  mounted() {
  },
  created() {
    this.fetchList()
  },
  methods: {
    changeModel(event)
    {
      var str = event.productName
      getModelList(str).then(response => {
        this.modelList = response.data
        this.taskForm.devmodeId = ''
      })
    },
    changeType(str)
    {
      console.log(str)
      getTypeList(str).then(response => {
        console.log(response.data)
        this.deviceTypeList = response.data
        this.taskForm.devtypeId = ''
        this.taskForm.devmodeId = ''
      })
    },
    // 初始化对话框
    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.devTypeId = row.devtypeId
        this.taskForm = {
          id: row.id,
          projectId: row.projectId,
          devtypeId: row.devTypeName,
          devmodeId: row.devmodeId,
          devcount: row.devcount,
          deliveryTimeFmt: row.deliveryTime,
          demand: row.demand,
          isstored: row.isstored,
          cardType: row.cardType,
          operator: row.operator
        }
        this.isEditMode = true
      }
    },
    moneyChange(e){
      this.taskForm.devcount = e.target.value
    },
    // 重置表单
    resetForm() {
      this.equipmentFile = null
      this.taskForm = {
        id:'',
        projectId: '',
        devtypeId: '',
        devmodeId: '',
        devcount: '',
        deliveryTimeFmt: '',
        demand: '',
        isstored: '0',
        cardType: '',
        operator: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },

    // 新增数据
    createData: function() {
      var item = this.taskForm.devtypeId
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.taskForm.devtypeId = this.taskForm.devtypeId.id
          addTask(this.taskForm).then(response => {
            if (response.code === 200) {
              this.taskForm.devtypeId = item
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.taskForm.devtypeId= this.devTypeId
          console.log(this.taskForm)
          updateTask(this.taskForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },

    // 获取项目名称
    fetchList() {
      getProjectList().then(response => {
        this.projectList = response.data
      })
      // getDeviceList().then(response => {
      //   this.deviceTypeList = response.data
      // })
    },
    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>
