<template>
<!-- 开始、生产节点 -->
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="工时单价">
        <el-input
          v-model="form.quotedPrice"
          placeholder="工时单价"
          :disabled="disabled"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input
          v-model="form.notes"
          :disabled="disabled || selectOperator"
        ></el-input>
      </el-form-item>
      <el-form-item label="工序要求" prop="require">
        <el-input
          v-model="form.require"
          :disabled="disabled || selectOperator"
        ></el-input>
      </el-form-item>
      <el-form-item label="工序特性" prop="process">
        <el-input
          v-model="form.process"
          :disabled="disabled || selectOperator"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="所需工时"
        prop="preTaskTime"
      >
        <el-input
          v-model="form.preTaskTime"
          :disabled="disabled || selectOperator"
          oninput="value=value.replace(/[^0-9.]/g,'')"
        >
          <i slot="suffix" style="font-style: normal; margin-right: 10px">h</i>
        </el-input>
      </el-form-item>
      <el-form-item label="是否编程" prop="isProgramme" v-if="!disabled && !selectOperator">
        <el-radio-group v-model="form.isProgramme">
          <el-radio :label="2">是</el-radio>
          <el-radio :label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
       <el-form-item label="要求完成时间" prop="planCompletionTime" v-if="selectOperator">
          <el-date-picker
            v-model="form.planCompletionTime"
            value-format="yyyy-MM-dd"
            type="date"
          >
          </el-date-picker>
      </el-form-item>
      <el-form-item label="操作人" v-if="form.operatorName && !selectOperator">
        <el-input
          :disabled="disabled && selectOperator || !isEdit"
          v-model="form.operatorName"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="操作人"
        prop="operatorId"
        v-if="selectOperator"
      >
        <el-select
          v-model="form.operatorId"
          :disabled="disabled && selectOperator || !isEdit"
          multiple
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in operatorIdOptions"
            :key="item.userId"
            :label="item.name"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="附件" prop="attachList" v-if="!disabled && !selectOperator">
        <upload-component
          :accept="'*'"
          v-model="form.attachList"
          :file-obj-list="form.attachList"
        />
      </el-form-item>
      <el-form-item v-else prop="attachList">
        <upload-component
          :display="true"
          :accept="'*'"
          v-model="form.attachList"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getWorkType } from '@/api/crafts'
import { workTypeMasterList } from '@/api/worktype'
import UploadComponent from '@/views/modules/common/upload-component-v2'
export default {
  name: 'nodeEdit',
  components: { UploadComponent },
  props: {
    data: {
      type: Object,
      default: () => {}
    },
    disabled: {
      type: Boolean,
      default: false
    },
    // 是否选择操作人，当选择操作人时，其他字段不可编辑
    selectOperator: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 工种列表
      workTypeOptions: [],
      // 操作人列表
      operatorIdOptions: [],
      node: {},
      form: {
        nodeName: '',
        workTypeId: '',
        operatorId: [],
        prodProductionRequireList: [{}],
        planCompletionTime: '',
        attachList: []
      },
      fileList: [],
      inspectionMethodOptions: [
        { value: '1', name: '游标卡尺' },
        { value: '2', name: '千分尺' },
        { value: '3', name: '高度尺' },
        { value: '4', name: '百分表' },
        { value: '5', name: 'R规' },
        { value: '6', name: '环规、塞规' },
        { value: '7', name: '游标角度尺' },
        { value: '8', name: '三坐标' },
        { value: '9', name: '模具' },
        { value: '10', name: '样板' },
        { value: '11', name: '夹具' },
        { value: '12', name: '目测' },
        { value: '13', name: '组合测量' },
        { value: '14', name: '精密测量' },
        { value: '15', name: '敲击' },
        { value: '16', name: '测厚仪' },
        { value: '17', name: '其他' }
      ],
      rules: {
        attachList: [
          { required: true, message: '请上传附件', trigger: 'change' }
        ],
        preTaskTime: [
          { required: true, message: '请输入所需工时', trigger: 'blur' }
        ],
        pageNo: [{ required: true, message: '请输入', trigger: 'blur' }],
        inspectionParam: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        inspectionMethod: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        allowValues: [{ required: true, message: '请输入', trigger: 'blur' }],
        isProgramme: [{ required: true, message: '请选择', trigger: 'change' }]
      }
    }
  },
  watch: {
    data (val) {
      console.log('node-edit-normal watch', val)
      this.node = val
      this.form = {
        ...this.form,
        ...this.node
      }
      if (this.node.operatorId) {
        this.form.operatorId = this.node.operatorId.split(',')
      }

      // 附件
      // if (this.node.attachList) {
      //   this.$nextTick(() => {
      //     this.fileList = []
      //     this.node.attachList.forEach((item) => {
      //       this.fileList.push({
      //         name: item.fileName,
      //         url: item.url,
      //         id: item.url
      //       })
      //     })
      //   })
      // } else {
      //   this.form.attachList = []
      // }
    },
    disabled (val) {
      this.disabled = val
    },
    isEdit (val) {
      this.isEdit = val
    }
  },
  activated () {},
  created () {
    this.getWorkTypeOptions()
  },
  computed: {},
  mounted () {
    this.node = this.data
    this.form = {
      ...this.form,
      ...this.node
    }
    if (this.node.operatorId) {
      this.form.operatorId = this.node.operatorId.split(',')
    }

    // 附件
    // if (this.node.attachList) {
    //   this.fileList = []
    //   this.node.attachList.forEach((item) => {
    //     this.fileList.push({
    //       name: item.fileName,
    //       url: item.url,
    //       id: item.url
    //     })
    //   })
    // }
    this.getOperatorList()
  },
  methods: {
    getWorkTypeOptions () {
      this.workTypeOptions = []
      getWorkType().then(({ data }) => {
        if (data && data.code === '200') {
          this.workTypeOptions = data.data
        }
      })
    },
    onChage (val) {
      this.workTypeOptions.forEach((v) => {
        if (v.typeId === val) {
          this.form.quotedPrice = Number(v.quotedPrice || 0).toFixed(2)
        }
      })
    },
    // 按工种ID查询操作人列表
    getOperatorList () {
      if (this.node.workTypeId) {
        workTypeMasterList(this.node.workTypeId).then(({ data }) => {
          if (data && data.code === '200') {
            this.operatorIdOptions = []
            data.data.forEach((item) => {
              this.operatorIdOptions.push(item)
            })

            this.onChage(this.node.workTypeId)
          }
        })
      }
    },
    // 校验表单
    validateFormData () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (!valid) {
            // eslint-disable-next-line prefer-promise-reject-errors
            reject()
            return
          }
          resolve()
        })
      })
    },
    // 获取表单数据
    formData () {
      const form = { ...this.form, operatorName: '' }
      if (this.form.operatorId != null) {
        form.operatorId = this.form.operatorId.toString()
        this.form.operatorId.forEach((id) => {
          let op = this.operatorIdOptions.find((t) => t.userId === id)
          if (op.name != null) {
            form.operatorName += op.name + ' '
          }
        })
      }

      // // 填充附件
      // let fList = this.fileList
      // if (fList.length > 0) {
      //   form.attachList = []
      //   for (let i = 0; i < fList.length; i++) {
      //     form.attachList.push({
      //       fileName: fList[i].name,
      //       url: fList[i].url
      //     })
      //   }
      // }

      return form
    }
  }
}
</script>
<style scoped></style>
