<template>
  <a-modal
    :title="$t('plan.dvp.edit-dvp')"
    :visible="isEditRow"
    @ok="save"
    @cancel="cancel"
    :width="1200"
  >
    <a-form>
      <a-row :gutter="24">
        <!--零部件编号-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('workbench.compoment-number')">
            <a-input v-model.trim="editParam.partNo" :disabled="true" allowClear :placeholder="$t('global-btn.please-select') + $t('workbench.compoment-number')"></a-input>
          </a-form-item>
        </a-col>
        <!--零部件名称-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('workbench.compoment-name')">
            <a-input v-model.trim="editParam.partName" :disabled="true" allowClear :placeholder="$t('global-btn.please-select') + $t('workbench.compoment-name')"></a-input>
          </a-form-item>
        </a-col>
        <!--CPAC编号-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('base.info.CPAC-code')">
            <a-input v-model.trim="editParam.cpacCode" :disabled="true" :placeholder="$t('global-btn.please-enter') + $t('base.info.CPAC-code')"/>
          </a-form-item>
        </a-col>
        <!--FND代码-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('plan.dvp.fnd-code')">
            <a-input v-model.trim="editParam.fndCode" :disabled="true" :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.fnd-code')" allowClear/>
          </a-form-item>
        </a-col>
        <!--试验类型-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('plan.dvp.test-type')" required>
            <a-select v-model="editParam.testType" :options="testTypeList" :filter-option="handleFilterDataList" allowClear :placeholder="$t('plan.dvp.please_select-test-type')"></a-select>
          </a-form-item>
        </a-col>
        <!--试验来源-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('plan.dvp.test-source')" required>
            <a-select v-model="editParam.testSource" :options="testSourceList" :filter-option="handleFilterDataList" allowClear :placeholder="$t('global-btn.please-select') + $t('plan.dvp.test-source')"></a-select>
          </a-form-item>
        </a-col>
        <!--试验项目-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('plan.dvp.test-project')" required>
            <a-input v-model.trim="editParam.testProject" :maxLength='128' allowClear :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-project')"/>
          </a-form-item>
        </a-col>
        <!--开发目标要求-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('base.info.development-objective-requirements')" required>
            <a-input v-model.trim="editParam.targetRequire" :maxLength='500' allowClear :placeholder="$t('global-btn.please-enter') + $t('base.info.development-objective-requirements')"/>
          </a-form-item>
        </a-col>
        <!--试验标准-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24" >
          <a-form-item :label="$t('base.info.Test-standards')" required>
            <a-input v-model.trim="editParam.testStandard" :maxLength='50' allowClear :placeholder="$t('global-btn.please-enter') + $t('base.info.Test-standards')"/>
          </a-form-item>
        </a-col>
        <!--等效/非等效-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('plan.dvp.is-it-an-equivalent-test')">
            <a-radio-group v-model="editParam.isEquivalent" @change="handleEquivalent">
              <a-radio :value="0">
                {{$t('plan.dvp.non-equivalent-experiment') }}
              </a-radio>
              <a-radio :value="1">
                {{ $t('plan.dvp.equivalent-experiment') }}
              </a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <span v-if="editParam.isEquivalent === 0">
          <!--测试地点-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.test-site')" required>
              <a-input v-model.trim="editParam.testSite" :maxLength='50' allowClear :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-site')"/>
            </a-form-item>
          </a-col>
          <!--测试负责人-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.test-user')" required>
              <a-select v-model="editParam.testOwner" :options="testOwnerList" :filter-option="handleFilterDataList"  allowClear :placeholder="$t('global-btn.please-select') + $t('plan.dvp.test-user')"></a-select>
            </a-form-item>
          </a-col>
          <!--测试阶段-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.test-step')" required>
              <a-input v-model.trim="editParam.testStep" :maxLength='50' allowClear :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-step')"/>
            </a-form-item>
          </a-col>
          <!--测试次数-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.test-count')" required>
              <a-input-number v-model.number="editParam.testFrequency" :defaultValue="0" :min="1" :max="999" :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-count')" style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <!--样本数量-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.number-of-samples')" required>
              <a-input-number v-model.number="editParam.sampleNumber" :defaultValue="0" :min="1" :max="999" :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.number-of-samples')" style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <!--样件类型-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.sample-type')" required>
              <a-input v-model.trim="editParam.sampleType" :maxLength='50' allowClear :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.sample-type')"/>
            </a-form-item>
          </a-col>
          <!--计划开始时间-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.scheduled-start-time')" required>
               <a-date-picker v-model="editParam.planStartTime" :disabled-date="disabledStartDate"  @change="handleEditStartTime"  allowClear valueFormat="YYYY/MM/DD" style="width: 100%" :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.scheduled-start-time')"/>
            </a-form-item>
          </a-col>
          <!--计划结束时间-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.scheduled-end-time')" required>
               <a-date-picker v-model="editParam.planEndTime" :disabled-date="disabledEndDate" @change="handleEditEndTime" allowClear valueFormat="YYYY/MM/DD" style="width: 100%" :placeholder="$t('global-btn.please-select') + $t('plan.dvp.scheduled-end-time')"/>
            </a-form-item>
          </a-col>
        </span>
        <span v-else>
          <!--等效实验报告编号-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.equivalent-experiment-report-number')" required>
              <a-select v-model="editParam.equivalentId" :options="equivalentList" :filter-option="handleFilterDataList" allowClear :placeholder="$t('global-btn.please-select') + $t('plan.dvp.equivalent-experiment-report-number')"></a-select>
            </a-form-item>
          </a-col>
        </span>
        <!--材料清单编号-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('plan.dvp.material-number')" required>
            <a-input v-model.trim="editParam.materialInventoryCode" :maxLength='50' allowClear :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.material-number')"/>
          </a-form-item>
        </a-col>
        <!--是否推送至零部件模板-->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-form-item :label="$t('plan.dvp.whether-to-push-to-component-template')">
            <a-checkbox @change="handlePushTemplate">
              {{ $t('plan.dvp.push-to-component-DVP-template') }}
            </a-checkbox>
          </a-form-item>
        </a-col>
        <!--上传材料清单 -->
        <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
          <a-row>
            <a-form-item :label="$t('plan.dvp.upload-material-list')" required>
              <a-upload-dragger
                  name="file"
                  :multiple="false"
                  action=""
                  :customRequest='editUploadFile'
                  :file-list="fileList"
              >
                <p class="ant-upload-drag-icon">
                  <a-icon type="inbox"/>
                </p>
                <p class="ant-upload-text">
                  {{ $t('dvp.full.vehicle.dvp.click-drag-file-area') }}
                </p>
                <p class="ant-upload-hint">
                  {{ $t('dvp.full.vehicle.dvp.only-support-single-upload') }}
                </p>
              </a-upload-dragger>
            </a-form-item>
          </a-row>
<!--          <a-row>-->
<!--            &lt;!&ndash;已上传的材料清单&ndash;&gt;-->
<!--            <a-col v-if="isUpload">-->
<!--              <a-form-item :label="$t('plan.dvp.uploaded-material-list')">-->
<!--                <a :href="$api.baseUrl + '/DFMEA/file/downloadFile.do?id=' + editParam.materialInventoryId + '&fileType=18'">-->
<!--                  {{ editParam.materialInventoryName }}-->
<!--                </a>-->
<!--              </a-form-item>-->
<!--            </a-col>-->
<!--          </a-row>-->
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>


<script>

import moment from 'moment';

export default {
  props: {
    isEditRow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      /** 编辑数据 */
      editParam: {
        testId: undefined,
        partNo: undefined,
        partName: undefined,
        cpacCode: undefined,
        fndCode: undefined,
        testType: undefined,
        testSource: undefined,
        testProject: undefined,
        targetRequire: undefined,
        testStandard: undefined,
        equivalentId: undefined,
        testSite: undefined,
        testOwnerId: undefined,
        testOwner: undefined,
        testStep: undefined,
        testFrequency: undefined,
        sampleNumber: undefined,
        sampleType: undefined,
        materialInventoryCode: undefined,
        materialInventoryId: undefined,
        materialInventoryName: undefined,
        planStartTime: undefined,
        planEndTime: undefined,
        isEquivalent: undefined
      },
      /** 用于记录初始值 */
      planStartTime: undefined,
      planEndTime: undefined,
      /** 试验类型下拉选项 */
      testTypeList: [
        {
          value: '1',
          label: this.$t('dvp.appearance')
        },
        {
          value: '2',
          label: this.$t('dvp.size')
        },
        {
          value: '3',
          label: this.$t('dvp.material-regulations')
        },
        {
          value: '4',
          label: this.$t('dvp.performance-and-functionality')
        }
      ],
      /** 试验来源下拉选项 */
      testSourceList: [
        {
          value: '1',
          label: this.$t('dvp.drawing')
        },
        {
          value:  '2',
          label: this.$t('dvp.KPC')
        },
        {
          value: '3',
          label: this.$t('dvp.web-require')
        },
        {
          value: '4',
          label: this.$t('dvp.standard')
        },
        {
          value: '5',
          label: this.$t('dvp.laws-and-regulations')
        }
      ],
      /** 试验负责人下拉选项 */
      testOwnerList: [],
      /** 等效实验报告编号下拉选项 */
      equivalentList: [],
      /** 等效实验报告编号下拉选项 */
      fileList: [],
      /** 已经上传过材料清单 */
      isUpload: false
    }
  },

  methods: {

    /**
     * 初始化数据
     */
    initData() {
      this.planStartTime = this.editParam.planStartTime;
      this.planEndTime = this.editParam.planEndTime;
      this.equivalentList = [];
      this.testOwnerList = [];
      // 获取团队成员 用于测试负责人下拉选
      this.$api.getDvpTeamMember({dvpId: this.$store.getters.dvpId,dvpType: this.$store.getters.dvpType}).then(res => {
        if (res.flag) {
          let userList = res.data.teamMemberModels;
          let engineerList = res.data.engineerModels;
          userList.forEach(item => {
            if (item.permission === 1){
              let options = {
                label: item.name,
                value: item.userId
              };
              this.testOwnerList.push(options);
            }
          });
          // 工程师数据处理
          engineerList.forEach(data => {
            let index = this.testOwnerList.findIndex(item => item.value === data.userId);
            if (index === -1) {
              let tempData = {
                label: data.name,
                value: data.userId
              };
              this.testOwnerList.push(tempData);
            }
          });
          // 是否包含Owner
          let ownerFlag = this.testOwnerList.find(item => item.value === this.$store.getters.dvpOwnerId);
          if (!ownerFlag) {
            // 测试负责人要包括DVP的Owner
            let owner = {
              label: this.$store.getters.dvpOwnerName,
              value: this.$store.getters.dvpOwnerId
            }
            this.testOwnerList.push(owner);
          }
        }
      });

      // 获取等效报告下拉选
      let data = {
        dvpId: this.$store.getters.dvpId
      };
      this.$api.getPartList(data).then( res => {
        if (res.flag) {
          // 等效报告 下拉选项
          let reportList = res.data.equivalentTestList;
          reportList.forEach(item => {
            let reportOptions = {
              label: item.reportCode,
              value: item.id
            }
            this.equivalentList.push(reportOptions);
          });
        }
      });

    },

		/**
		 * 非空校验 判断是否为空
		 * @returns {boolean}
		 */
		isNull() {
			let _that = this;
			let message = [];
      // 试验类型
			if (!_that.editParam.testType) {
				message.push(_that.$t('plan.dvp.please_select-test-type'));
			}
      // 试验来源
			if (!_that.editParam.testSource) {
				message.push(_that.$t('global-btn.please-select') + _that.$t('plan.dvp.test-source'));
			}
      // 试验项目
			if (!_that.editParam.testProject) {
				message.push(_that.$t('global-btn.please-enter') + _that.$t('plan.dvp.test-project'));
			}
      // 开发目标要求
			if (!_that.editParam.targetRequire) {
				message.push(_that.$t('global-btn.please-enter') + _that.$t('base.info.development-objective-requirements'));
			}
      // 试验标准
			if (!_that.editParam.testStandard) {
				message.push(_that.$t('global-btn.please-enter') + _that.$t('base.info.Test-standards'));
			}
      // 非等效试验时校验以下数据
      if (_that.editParam.isEquivalent === 0) {
        // 测试地点
        if (!_that.editParam.testSite) {
          message.push(_that.$t('global-btn.please-enter') + _that.$t('plan.dvp.test-site'));
        }
        // 测试负责人
        if (!_that.editParam.testOwner) {
          message.push(_that.$t('global-btn.please-select') + _that.$t('plan.dvp.test-user'));
        }
        // 测试阶段
        if (!_that.editParam.testStep) {
          message.push(_that.$t('global-btn.please-enter') + _that.$t('plan.dvp.test-step'));
        }
        // 测试次数
        if (!_that.editParam.testFrequency) {
          message.push(_that.$t('global-btn.please-enter') + _that.$t('plan.dvp.test-count'));
        }
        // 样本数量
        if (!_that.editParam.sampleNumber) {
          message.push(_that.$t('global-btn.please-enter') + _that.$t('plan.dvp.number-of-samples'));
        }
        // 样件类型
        if (!_that.editParam.sampleType) {
          message.push(_that.$t('global-btn.please-enter') + _that.$t('plan.dvp.sample-type'));
        }
        // 计划开始时间
        if (!_that.editParam.planStartTime) {
          message.push(_that.$t('global-btn.please-select') + _that.$t('plan.dvp.scheduled-start-time'));
        }
        // 计划结束时间
        if (!_that.editParam.planEndTime) {
          message.push(_that.$t('global-btn.please-select') + _that.$t('plan.dvp.scheduled-end-time'));
        }
      } else {    
        // 等效实现时校验
        // 等效试验报告编号    
        if (!_that.editParam.equivalentId) {
          message.push(_that.$t('global-btn.please-select') + _that.$t('plan.dvp.equivalent-experiment-report-number'));
        }
      }
      // 材料清单编号
			if (!_that.editParam.materialInventoryCode) {
				message.push(_that.$t('global-btn.please-enter') + _that.$t('plan.dvp.material-number'));
			}
      // 材料清单附件  
			if (_that.fileList.length == 0) {
				message.push(_that.$t('global-message.warn..message.warn.select-file'));
			}
			if (message && message.length > 0) {
				for (let i = 0; i < message.length; i++) {
					_that.$notification.warning({message: message[i]});
				}
				return false;
			}
			return true;
		},

    /**
     * 确认按钮
     */
    save() {
      // 非空校验
      if (!this.isNull()) {
				return false;
			}
      let data = {
        dvpId: this.$store.getters.dvpId,
        testId: this.editParam.testId,
        testType: this.editParam.testType,
        testSource: this.editParam.testSource,
        testProject: this.editParam.testProject,
        targetRequire: this.editParam.targetRequire,
        testStandard: this.editParam.testStandard,
        equivalentId: this.editParam.equivalentId,
        testSite: this.editParam.testSite,
        testUserId: this.editParam.testOwner,
        testStep: this.editParam.testStep,
        testFrequency: this.editParam.testFrequency,
        sampleNumber: this.editParam.sampleNumber,
        sampleType: this.editParam.sampleType,
        materialInventoryCode: this.editParam.materialInventoryCode,
        planStartTime: this.editParam.planStartTime,
        planEndTime: this.editParam.planEndTime,
        isPush: this.editParam.isPushTemplate,
        dvpAddFileModel: this.fileList[0]
      };
      this.$api.planTestSave(data).then( res => {
        if (res.flag) {
          this.$message.success(res.message);
          this.cancel();
          this.$emit("initData");
        } else {
          this.$message.error(res.message);
        }
      });
    },

    /**
     * 实验报告，文件上传
     * @param obj 回调对象
     */
    editUploadFile(obj) {
      let _that = this;
      _that.fileList = [];
      if (obj.file.size > 52428800) {
        _that.$message.warning(_that.$t('global-message.warn.uploaded-files-cannot-exceed'));
        return;
      }
      let fd = new FormData();
      fd.append('file', obj.file);
      fd.append('fileType', '18');
      _that.$api.uploadFile(fd).then((res) => {
        if (res.flag) {
          _that.$message.success(res.message, 1);
          _that.fileList.push({
            name: res.data.fileName,
            fileName: res.data.fileName,
            filePath: res.data.filePath,
            fileSize: res.data.fileSize,
            newFileName: res.data.newFileName,
            uid: obj.file.uid
          });
        } else {
          _that.$message.error(res.message, 1);
        }
      }).catch((error) => {
        _that.$message.error(error.response.data.message, 1);
      });
    },
    /**
     * 取消按钮
     */
    cancel() {
      this.fileList = [];
      this.$emit("closeEditRowModal");
    },

    /**
     * 下拉选模糊查询
     * @param input
     * @param option
     * @returns {boolean}
     */
    handleFilterDataList(input,option) {
      return option.componentOptions.children[0].text.toLowerCase().includes(input.toLowerCase());
    },

    /**
     * 等效/非等效
     * @param e
     */
    handleEquivalent(e) {
      this.editParam.isEquivalent = e.target.value;
    },

    /**
     * 是否推送至零部件模板
     * @param e
     */
    handlePushTemplate(e) {
      if (e.target.checked) {
        this.editParam.isPushTemplate = 0;
      } else {
        this.editParam.isPushTemplate = 1;
      }
    },

    /**
     * 开始时间不能选择当天
     * @param current
     * @returns {boolean}
     */
    disabledStartDate(current) {
      return current && current < moment().subtract(1,"day").endOf('day');
    },

    /**
     * 结束时间不能选择开始时间前的时间
     * @param current
     * @returns {boolean}
     */
    disabledEndDate(current) {
      return current && current < moment(this.editParam.planStartTime).endOf('day');
    },

    /**
     * 计划开始时间 时间选择器Change方法 编辑
     */
    handleEditStartTime(date, dateString){
      if (!dateString) {
        this.editParam.planStartTime = "";
        return;
      }
      this.editParam.planStartTime = dateString;
      // 当选择的开始时间大于结束时间时，清空结束时间，让用户重新选择结束时间
      if (Date.parse(this.editParam.planStartTime) > Date.parse(this.editParam.planEndTime)) {
        this.editParam.planEndTime = undefined;
      }
    },
    /**
     * 计划结束时间 时间选择器Change方法 编辑
     */
    handleEditEndTime(date, dateString) {
      if (!dateString) {
        this.editParam.planEndTime = "";
        return;
      }
      this.editParam.planEndTime = dateString;
    }
  }
}

</script>

<style lang="less" scoped>

</style>
