<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="任务类型" prop="taskType">
        <el-select
          v-model="dataForm.taskType"
          placeholder="任务类型，0 出库，1入库"
          style="width:80%"
        >
          <el-option label="出库" :value="0"></el-option>
          <el-option label="入库" :value="1"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="产品编码" prop="model">
        <el-autocomplete
          class="inline-input"
          v-model="dataForm.model"
          placeholder="产品编码"
          :fetch-suggestions="querySearch"
          popper-class="select-option"
          clearable
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="起点" prop="startPoint">
  <el-select v-model="dataForm.startPoint" filterable placeholder="请选择起点">
    <el-option
      v-for="item in PointArr"
      :key="item.id"
      :label="item.anotherName"
      :value="item.id">
    </el-option>
  </el-select>
      </el-form-item>
      <el-form-item label="终点" prop="endPoint">
  <el-select v-model="dataForm.endPoint" filterable placeholder="请选择终点">
    <el-option
      v-for="item in PointArr"
      :key="item.id"
      :label="item.anotherName"
      :value="item.id">
    </el-option>
      </el-form-item>
      <el-form-item label="数量" prop="num">
        <el-input v-model="dataForm.num" placeholder="数量" oninput = "value=value.replace(/[^\d]/g,'')"></el-input>
      </el-form-item>
      <!-- <el-form-item label="批次号" prop="dateNum">
        <el-input
          v-model="dataForm.dateNum"
          placeholder="请填写批次号"
        ></el-input>
        oninput = "value=value.replace(/[^\d]/g,'')"
      </el-form-item> -->
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        taskType: '',
        model: '',
        startPoint: '',
        endPoint: '',
        num: '',
        unit: '',
        remarks: '',
        dateNum: ''
      },
      PointArr: [],
      dataRule: {
        // taskType: [
        //   { required: true, message: '任务类型不能为空', trigger: 'blur' }
        // ],
        model: [
          { required: true, message: '型号不能为空', trigger: 'blur' }
        ],
        num: [
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ],
        startPoint: [
          { required: true, message: '起点不能为空', trigger: 'change' }
        ],
        endPoint: [
          { required: true, message: '终点不能为空', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    inout: {
      type: Number
    }
  },
  methods: {
    init (id) {
      // if (this.inout === 1) {
      //   this.dataForm.taskType = 0
      // } else if (this.inout === 2) {
      //   this.dataForm.taskType = 1
      // }
      this.dataForm.id = id || 0
      this.visible = true
      this.$http({
        url: this.$http.adornUrl('/generator/mtccwarehousrank/list'),
        method: 'post'
      }).then(({data}) => {
        this.PointArr = data.list
      })
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/generator/mtcctaskruninfo/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.mtCcTaskRunInfo
            }
          })
        }
      })
    },
    querySearch (queryString, cb) {
      var results = []
      this.$http({
        url: this.$http.adornUrl('/generator/mtcctaskruninfo/getLikeList'),
        method: 'get',
        params: this.$http.adornParams({
          'key': queryString
        })
      }).then(({ data }) => {
        for (var i = 0; i < data.length; i++) {
          var obj = { value: '' }
          obj.value = data[i]
          results[i] = obj
        }
        cb(results)
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/generator/mtcctaskruninfo/${!this.dataForm.id ? 'saveExample' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              // 'taskType': this.dataForm.taskType,
              'model': this.dataForm.model,
              'num': this.dataForm.num,
              'startPoint': this.dataForm.startPoint,
              'endPoint': this.dataForm.endPoint,
              'remarks': this.dataForm.remarks,
              'dateNum': this.dataForm.dateNum
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
