<template>
  <div>
    <a-row class="content-box">
      <a-card class="layout-card">
        <a-form layout='vertical'>
          <a-row :gutter="24">
            <!--零部件名称-->
            <a-col :xl='6' :lg='8' :md='24' :sm='24' :xs='24'>
              <a-form-item :label="$t('workbench.compoment-name')">
                <a-input allowClear :maxLength='50' v-model.trim="formData.partName" :placeholder="$t('global-btn.please-enter') + $t('workbench.compoment-name')"/>
              </a-form-item>
            </a-col>
            <!--零部件编号-->
            <a-col :xl='6' :lg='8' :md='24' :sm='24' :xs='24'>
              <a-form-item :label="$t('workbench.compoment-number')">
                <a-input allowClear :maxLength='50' v-model.trim="formData.partCode" :placeholder="$t('global-btn.please-enter') + $t('workbench.compoment-number')"/>
              </a-form-item>
            </a-col>
            <!--FND代码-->
            <a-col :xl='6' :lg='8' :md='24' :sm='24' :xs='24'>
              <a-form-item :label="$t('plan.dvp.fnd-code')">
                <a-select allowClear
                          show-search
                          v-model='formData.fndCode'
                          :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.fnd-code')"
                          @search="getFndCodeList"
                          @change="fndChange">
                  <a-icon slot="suffixIcon" type="search" />
                  <a-select-option v-for='(item, index) in fndCodeList' :key="item.id" :value="item.fndValue" :label="item.fndCn">
                    {{ item.fndCode + " " + (item.fndCn || "") }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!--归属部门-->
            <a-col :xl='6' :lg='8' :md='24' :sm='24' :xs='24'>
              <a-form-item :label="$t('workbench.belonging-department')">
                <a-select v-model="formData.ownerDepartmentId" @change="orgInput" @search="getDepartmentList" :placeholder="$t('global-btn.please-enter') + $t('workbench.belonging-department')" allowClear showSearch>
                  <a-icon slot="suffixIcon" type="search"/>
                  <a-select-option v-for="item in departmentList" :key="item.orgId">
                    {{ item.orgName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!--试验类型-->
            <a-col :xl='6' :lg='8' :md='12' :sm='24' :xs='24'>
              <a-form-item :label="$t('plan.dvp.test-type')">
                <a-select v-model="formData.testType" :options="testTypeList" :filter-option="handleFilterDataList" allowClear :placeholder="$t('plan.dvp.please_select-test-type')"></a-select>
              </a-form-item>
            </a-col>
            <!--试验来源-->
            <a-col :xl='6' :lg='8' :md='24' :sm='24' :xs='24'>
              <a-form-item :label="$t('plan.dvp.test-source')">
                <a-select v-model="formData.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 :xl='6' :lg='8' :md='12' :sm='24' :xs='24'>
              <a-form-item :label="$t('plan.dvp.test-project')">
                <a-input allowClear :maxLength='500' v-model.trim="formData.testProject" :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-project')"/>
              </a-form-item>
            </a-col>
            <!--开发目标要求-->
            <a-col :xl='6' :lg='8' :md='24' :sm='24' :xs='24'>
              <a-form-item :label="$t('base.info.development-objective-requirements')">
                <a-input allowClear :maxLength='50' v-model.trim="formData.targetRequire" :placeholder="$t('global-btn.please-enter') + $t('base.info.development-objective-requirements')"/>
              </a-form-item>
            </a-col>
            <span v-show="advanceExpand">
              <!--试验标准-->
              <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
                <a-form-item :label="$t('base.info.Test-standards')">
                 <a-input v-model.trim="formData.testStandard" :maxLength='50' allowClear :placeholder="$t('global-btn.please-enter') + $t('base.info.Test-standards')"/>
                </a-form-item>
              </a-col>
              <!--测试负责人-->
              <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
                <a-form-item :label="$t('plan.dvp.test-user')">
                  <a-select v-model="formData.testOwner" :options="testOwnerList"  allowClear :placeholder="$t('global-btn.please-select') + $t('plan.dvp.test-user')"></a-select>
                </a-form-item>
              </a-col>
              <!--测试地点-->
              <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
                <a-form-item :label="$t('plan.dvp.test-site')">
                 <a-input v-model.trim="formData.testSite" :maxLength='50' allowClear :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-site')"/>
                </a-form-item>
              </a-col>
              <!--数据来源-->
              <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
                <a-form-item :label="$t('plan.dvp.data-sources')">
                  <a-select v-model="formData.dataSource" :options="dataSourceList" :filter-option="handleFilterDataList" allowClear :placeholder="$t('global-btn.please-select') + $t('plan.dvp.data-sources')"></a-select>
                </a-form-item>
              </a-col>
              <!--计划开始时间-->
              <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
                <a-form-item :label="$t('plan.dvp.scheduled-start-time')">
                  <a-range-picker v-model="startTime" @change="handleQueryStartTime" allowClear valueFormat="YYYY-MM-DD" style="width: 100%" />
                </a-form-item>
              </a-col>
              <!--计划结束时间-->
              <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
                <a-form-item :label="$t('plan.dvp.scheduled-end-time')">
                  <a-range-picker v-model="endTime" @change="handleQueryEndTime" allowClear valueFormat="YYYY-MM-DD" style="width: 100%" />
                </a-form-item>
              </a-col>
            </span>
          </a-row>
          <a-row :gutter="24">
            <a-form-item class='tr' :label="'\xa0'">
              <a-col :xl='24' :lg='24' :md='24' :sm='24' :xs='24'>
                <!-- 展开/收起 -->
                <a @click="toggleAdvance" class="mr10">
                  {{ advanceExpand ? $t('plan.dvp.collapse') : $t('plan.dvp.expand') }}
                  <a-icon :type="advanceExpand ? 'up' : 'down'"/>
                </a>
                <!-- 重置 -->
                <a-button @click='reset' type="primary" class="mr10">
                  {{ $t('global-btn.reset') }}
                </a-button>
                <!-- 查询 -->
                <a-button @click='search' type="primary" class="mr10">
                  {{ $t('global-btn.search') }}
                </a-button>
              </a-col>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
      <a-card class="layout-card" :style="{ height: tableHeight }">
        <a-form-item class='tr' >
          <!-- 编辑零部件DVP -->
          <a-button :disabled="isLook || applyButtonFlag" @click='editComponentDvp' type="primary" class="mr10">
            {{ $t('plan.dvp.edit-component-dvp') }}
          </a-button>
          <!-- 上传材料清单 -->
          <a-button :disabled="isLook || applyButtonFlag" @click='uploadMaterialList' type="primary" class="mr10">
            {{ $t('plan.dvp.upload-material-list') }}
          </a-button>
          <!-- 导出 -->
          <a-button @click='exportFile' class="mr10">
            {{ $t('global-btn.export') }}
          </a-button>
        </a-form-item>
        <a-table
          size="small"
          class="table-mr"
          :columns='tableColumns'
          :dataSource='tableData'
          :rowKey='(record, index) => { return index; }'
          :scroll='{ x: 1850, y: tableHeightValue }'
          :pagination='pagination'
          :loading='tableLoading'
          @change='handlePageChange'
        >
          <!-- 序号列 -->
          <template slot='index' slot-scope='text, record, index'>
            <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
          </template>
          <!-- 是否等效 -->
          <template slot='isEquivalent' slot-scope='text, record, index'>
            <span>{{ isOrNotMap[record.isEquivalent] || '-' }}</span>
          </template>
          <template slot="materialInventoryName" slot-scope="text, record">
            <span class="clickContent" @click="onClick(record)" :title="text">{{ text }}</span>
          </template>
          <!-- 材料确认清单是否上传 -->
          <template slot='isUpload' slot-scope='text, record, index'>
            <span>{{ isOrNotMap[record.isUpload] || '-' }}</span>
          </template>
          <!-- 数据来源 -->
          <template slot='dataSource' slot-scope='text, record, index'>
            <span>{{ dataSourceMap[record.dataSource] || '-' }}</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <span>
              <!-- 编辑 -->
             <a class='operation-btn ml16' @click='editInit(record)' :disabled="isLook || applyButtonFlag">
               <a-tooltip :title="$t('global-btn.edit')">
                 <i class="iconfont iconicon-bainji"></i>
               </a-tooltip>
             </a>
              <!-- 申请变更 -->
               <a class='operation-btn' @click="applyChange(record)" v-if="applyButtonFlag" :disabled="isLook">
                <a-tooltip :title="$t('global-btn.apply-for-changes')">
                  <i class="iconfont  iconprocess_branch"></i>
                </a-tooltip>
              </a>
              <!-- 清空 -->
             <a class='operation-btn' @click="clearDvpData(record)" :disabled="isLook || applyButtonFlag">
                <a-tooltip :title="$t('plan.dvp.clear')">
                  <i class="iconfont iconshanchu"></i>
                </a-tooltip>
              </a>
            </span>
          </template>
        </a-table>
      </a-card>
    </a-row>

    <!--零部件编辑DVP弹框-->
    <a-modal
      :title="editDvpTittle"
      :visible="isEdit"
      @ok="saveEditDvp"
      @cancel="cancelEditDvp"
      :width="1000"
    >
      <a-tabs :activeKey='tabKey' @change="callback">
        <!-- 编辑DVP -->
        <a-tab-pane :key="'1'" :tab="$t('plan.dvp.edit-dvp')">
          <edit-dvp ref="editDvp" @cancelEditDvp="cancelEditDvp" @initData="initData"/>
        </a-tab-pane>
        <!-- 模板编辑 -->
        <a-tab-pane :key="'2'" :tab="$t('plan.dvp.template-edit')">
          <template-edit ref="templateEdit" @cancelEditDvp="cancelEditDvp" @initData="initData"/>
        </a-tab-pane>
        <!-- 历史DVP编辑 -->
        <a-tab-pane :key="'3'" :tab="$t('plan.dvp.history-dvp-edit')">
          <history-dvp-edit ref="historyDvpEdit" @cancelEditDvp="cancelEditDvp" @initData="initData"/>
        </a-tab-pane>
        <!-- 项目FMEA编辑 -->
        <a-tab-pane :key="'4'" :tab="$t('plan.dvp.project-fmea-edit')">
          <project-fmea-edit ref="projectFmeaEdit" @cancelEditDvp="cancelEditDvp" @initData="initData"/>
        </a-tab-pane>
      </a-tabs>
    </a-modal>

    <!--上传材料清单弹框-->
    <upload-material ref="uploadMaterial" @closeUploadModal="closeUploadModal" @initData="initData" :isUpload="isUpload" ></upload-material>

    <!-- 编辑表格行数据弹框 -->
    <edit-table-row ref="editTableRow" @closeEditRowModal="closeEditRowModal" @initData="initData" :isEditRow="isEditRow"></edit-table-row>

    <!--申请变更-->
    <apply-change ref="applyChange" :applyFlag="applyFlag" :applyTableData="applyTableData" :dvpType="dvpType" @closeModal="closeModal" @initData="initData"></apply-change>

  </div>
</template>

<script>

import EditDvp from "./editModal/editDvp";
import HistoryDvpEdit from "./editModal/historyDvpEdit";
import ProjectFmeaEdit from "./editModal/projectFmeaEdit";
import TemplateEdit from "./editModal/templateEdit";
import EditTableRow from "./editModal/editTableRow";
import UploadMaterial from "./editModal/uploadMaterialList";
import ApplyChange from "./editModal/applyForChange";
import {getUrlParam} from "../../../common/common";


export default {
  components: {
    EditDvp,
    HistoryDvpEdit,
    ProjectFmeaEdit,
    TemplateEdit,
    EditTableRow,
    UploadMaterial,
    ApplyChange
  },
  data() {
    return{
      /** 查询数据 */
      formData:{},
      /** 清除时间选框用的 */
      endTime: undefined,
      startTime: undefined,
      /** 折叠 展开 */
      advanceExpand: false,
      /** 分页 */
      pagination: {
        size: 'small',
        current: 1,
        pageNo: 1,
        pageSize: 10, // 默认每页显示数量
        showTotal: total => this.$t('global-constant.table.show-total', {total: `${total}`}), // 显示总数
        showSizeChanger: true, // 显示可改变每页数量
        pageSizeOptions: ['10', '20', '50', '100', '200'], // 每页数量选项
        onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize), // 改变每页数量时更新显示
        total: 0, //总条数
        showQuickJumper: true,
      },
      /** 表格高度 */
      tableHeight: window.innerHeight - 352 + 'px',
      tableHeightValue: window.innerHeight - 352,
      /** 表格加载状态 */
      tableLoading: false,
      /** 表格数据 */
      tableData: [],
      /** 表格列 */
      tableColumns: [
        //序号
        {
          title: this.$t('plan.dvp.serial-number'),
          width: 50,
          fixed: 'left',
          align: 'center',
          scopedSlots: {
            customRender: 'index'
          },
          ellipsis: true
        },
        // 零部件编号
        {
          title: this.$t('workbench.compoment-number'),
          width: 100,
          dataIndex: 'partNo',
          align: 'center',
          ellipsis: true
        },
        // 零部件名称
        {
          title: this.$t('workbench.compoment-name'),
          width: 100,
          dataIndex: 'partName',
          align: 'center',
          ellipsis: true
        },
        // CPAC编码
        {
          title: this.$t('base.info.CPAC-code'),
          width: 100,
          dataIndex: 'cpacCode',
          align: 'center',
          ellipsis: true
        },
        // FND代码
        {
          title: this.$t('plan.dvp.fnd-code'),
          width: 100,
          dataIndex: 'fndCode',
          align: 'center',
          ellipsis: true
        },
        // 归属部门
        {
          title: this.$t('workbench.belonging-department'),
          width: 100,
          dataIndex: 'ownerDepartmentName',
          align: 'center',
          ellipsis: true
        },
        // 试验类型
        {
          title: this.$t('plan.dvp.test-type'),
          width: 100,
          dataIndex: 'testType',
          align: 'center',
          ellipsis: true,
          customRender: val => {
            return val !== null ? this.testTypeList.find( item => item.value === val ).label : '-';
          }
        },
        // 试验来源
        {
          title: this.$t('plan.dvp.test-source'),
          width: 100,
          dataIndex: 'testSource',
          align: 'center',
          ellipsis: true,
          customRender: val => {
            return val !== null ? this.testSourceList.find( item => item.value === val ).label : '-';
          }
        },
        // 数据来源
        {
          title: this.$t('plan.dvp.data-sources'),
          width: 100,
          dataIndex: 'dataSource',
          align: 'center',
          ellipsis: true,
          scopedSlots: {
            customRender: 'dataSource'
          },
        },
        // 试验项目
        {
          title: this.$t('plan.dvp.test-project'),
          width: 100,
          dataIndex: 'testProject',
          align: 'center',
          ellipsis: true
        },
        // 开发目标要求
        {
          title: this.$t('base.info.development-objective-requirements'),
          width: 100,
          dataIndex: 'targetRequire',
          align: 'center',
          ellipsis: true
        },
        // 试验标准和试验方法
        {
          title: this.$t('plan.dvp.test-standards-and-test-method'),
          width: 150,
          dataIndex: 'testStandard',
          align: 'center',
          ellipsis: true
        },
        // 是否等效试验
        {
          title: this.$t('plan.dvp.is-it-an-equivalent-test'),
          width: 100,
          dataIndex: 'isEquivalent',
          align: 'center',
          ellipsis: true,
          scopedSlots: {
            customRender: 'isEquivalent'
          }
        },
        // 等效试验报告编号
        {
          title: this.$t('plan.dvp.equivalent-report-code'),
          width: 150,
          dataIndex: 'equivalentTestCode',
          align: 'center',
          ellipsis: true
        },
        // 测试地点
        {
          title: this.$t('plan.dvp.test-site'),
          width: 100,
          dataIndex: 'testSite',
          align: 'center',
          ellipsis: true
        },
        // 测试负责人
        {
          title: this.$t('plan.dvp.test-user'),
          width: 100,
          dataIndex: 'testOwner',
          align: 'center',
          ellipsis: true
        },
        // 测试阶段
        {
          title: this.$t('plan.dvp.test-step'),
          width: 100,
          dataIndex: 'testStep',
          align: 'center',
          ellipsis: true
        },
        // 测试次数
        {
          title: this.$t('plan.dvp.test-count'),
          width: 100,
          dataIndex: 'testFrequency',
          align: 'center',
          ellipsis: true
        },
        // 样本数量
        {
          title: this.$t('plan.dvp.number-of-samples'),
          width: 100,
          dataIndex: 'sampleNumber',
          align: 'center',
          ellipsis: true
        },
        // 样件类型
        {
          title: this.$t('plan.dvp.sample-type'),
          width: 100,
          dataIndex: 'sampleType',
          align: 'center',
          ellipsis: true
        },
        // 材料清单文件名称
        {
          title: this.$t('plan.dvp.material-list-file-name'),
          width: 150,
          dataIndex: 'materialInventoryName',
          align: 'center',
          scopedSlots: {
            customRender: 'materialInventoryName'
          },
          ellipsis: true
        },
        // 材料清单确认状态
        {
          title: this.$t('plan.dvp.confirmation-status-of-material-list'),
          width: 150,
          dataIndex: 'isUpload',
          align: 'center',
          scopedSlots: {
            customRender: 'isUpload'
          },
          ellipsis: true
        },
        // 计划开始时间
        {
          title: this.$t('plan.dvp.scheduled-start-time'),
          width: 100,
          dataIndex: 'planStartTime',
          align: 'center',
          ellipsis: true
        },
        // 计划结束时间
        {
          title: this.$t('plan.dvp.scheduled-end-time'),
          width: 100,
          dataIndex: 'planEndTime',
          align: 'center',
          ellipsis: true
        },
        //操作
        {
          title: this.$t('global-constant.operation'),
          align: 'center',
          width: 100,
          fixed: 'right',
          scopedSlots: {customRender: 'action'},
          ellipsis: true
        }
      ],
      /** 是/否Map 用于表格插槽显示 */
      isOrNotMap: {
        0: this.$t("global-btn.no"),
        1: this.$t("global-btn.yes")
      },
      /** 试验类型字典  未确定具体字段-暂时写死*/
      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')
        }
      ],
      /** 权限 */
      isLook: false,
      /** 编辑零部件DVP弹框 tab 默认tab的key */
      tabKey: '1',
      /** 编辑零部件DVP弹框 是否显示、关闭 */
      isEdit: false,
      /** 编辑零部件DVP弹框标题 */
      editDvpTittle: this.$t('plan.dvp.edit-dvp'),
      /** 测试负责人下拉选项 */
      testOwnerList: [],
      /** CPAC List */
      CPACList: [],
      /** FND Code List */
      fndCodeList: [],
      /** 部门下拉选项 List */
      departmentList: [],
      /** 数据来源下拉选项 List  数据来源 0手动修改 1模板 2DFMEA 3历史DVP */
      dataSourceList: [
        {
          label: this.$t("plan.dvp.manual-modification"),
          value: '0'
        },
        {
          label: this.$t("plan.dvp.test-source-dvp-template"),
          value: '1'
        },
        {
          label: this.$t("plan.dvp.test-source-DFMEA"),
          value: '2'
        },
        {
          label: this.$t("plan.dvp.history-dvp"),
          value: '3'
        }
      ],
      /** 数据来源Map 用于表格插槽显示 数据来源 0手动修改 1模板 2DFMEA 3历史DVP */
      dataSourceMap: {
        '0': this.$t("plan.dvp.manual-modification"),
        '1': this.$t("plan.dvp.test-source-dvp-template"),
        '2': this.$t("plan.dvp.test-source-DFMEA"),
        '3': this.$t("plan.dvp.history-dvp")
      },
      /** 编辑表格行数据弹框 是否显示、关闭 */
      isEditRow: false,
      /** 上传材料清单弹窗 是否显示、关闭 */
      isUpload: false,
      /** 申请变更弹窗 是否显示、关闭 */
      applyFlag: false,
      /** 申请变更弹窗表格数据 */
      applyTableData: [],
      /** 申请变更按钮 是否显示 */
      applyButtonFlag: false,
      /** dvpType system:系统DVP，components:零部件DVP */
      dvpType: "components",
      cpacId: this.$store.getters.cpacId
   }
  },
  mounted() {
    this.isLook = this.$api.isLook === '1'
    this.initData();
    this.getDepartmentList();
    this.getDvpTestOwnerList();
    window.onresize = () => {
      this.tableHeight = window.innerHeight - 352 + 'px';
      this.tableHeightValue = window.innerHeight - 352;
    };
  },
  methods: {
    /**
     * 初始化表格数据
     */
    initData() {
      let _that = this;
      _that.tableLoading = true;
      _that.formData.dvpId = _that.$store.getters.dvpId;
      _that.formData.pagination = {
        pageSize: _that.pagination.pageSize,
        pageCurrent: _that.pagination.current
      }
      _that.formData.systemTestSource = _that.formData.dataSource;
      _that.$api.initPartDvpPlanList(_that.formData).then(res => {
        if (res.flag) {
          _that.tableData = res.data.data || [];
          _that.tableData.forEach( item => {
            if (item.equivalentTestCode) {
              item.isEquivalent = 1;
            } else {
              item.isEquivalent = 0;
            }
          });
          _that.pagination.total = res.data.total;
          _that.cpacId = res.data.data[0].cpacId;
          if (res.data.data) {
            _that.getFndCodeList();
          }
        } else {
          _that.$message.error(res.message,1);
          _that.tableData = [];
          _that.pagination.total = 0;
        }
      }).finally(() => {
        _that.tableLoading = false;
      });

      // 变更申请按钮显示隐藏，如果DVP编制进行到DVP验证阶段，则显示
      let data = {
        dvpId: _that.$store.getters.dvpId,
        dvpFlag: getUrlParam("from")
      }
      _that.$api.getDvpCurrentStep(data).then(res => {
        if (res.flag) {
          _that.applyButtonFlag = res.data === 2 ;
        }
      });

    },

    /**
     *  获取团队成员 用于测试负责人下拉选
     */
    getDvpTestOwnerList() {
      let data = {
        dvpId: this.$store.getters.dvpId,
        dvpType: this.$store.getters.dvpType
      };
      this.$api.getDvpTeamMember(data).then(res => {
        if (res.flag) {
          let userList = res.data.teamMemberModels;
          userList.forEach(item => {
            if (item.permission === 1){
              let options = {
                label: item.name,
                value: item.userId
              };
              this.testOwnerList.push(options);
            }
          });
          // 是否包含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);
          }
        }
      });
    },


    /**
     * 获取FND代码下拉选数据
     */
    getFndCodeList(fndCode,cpacId) {
      let _that = this;
      _that.$api.getFndInfoList({
        fndCode: fndCode ? fndCode.toString() : '',
        fndCn: fndCode ? fndCode.toString() : '',
        cpacId: _that.cpacId
      }).then(res => {
        if (res.flag) {
          _that.fndCodeList = res.data || [];
          _that.fndCodeList.forEach(item => {
            item.fndValue = item.id + '-' + item.fndCode + item.fndCn;
          });
        } else {
          _that.$notification.error({message: res.message});
        }
      });
    },

    /**
     * FND选择事件
     * @param value
     * @param options
     * @constructor
     */
    fndChange(value,options) {
      this.formData.fndId = options ? options.data.key : null;
      this.$forceUpdate();
    },

    /**
     * 部门 下拉选项
     * @param orgName
     */
    getDepartmentList(orgName) {
      this.$api.post('/DFMEA/fmea/org/getFuzzyOrg.do', {orgName: orgName}).then(res => {
        if (res.flag) {
          this.departmentList = res.data;
        } else {
          this.departmentList = [];
        }
      });
    },

    /**
     * 部门选择事件
     * @param value
     * @param options
     */
    orgInput(value, options) {
      this.formData.ownerDepartmentId = options ? options.data.key : undefined;
    },

    /**
     * 搜索模块按钮-查询
     */
    search() {
      this.initData();
    },

    /**
     * 搜索模块按钮-重置
     */
    reset() {
      this.endTime = undefined;
      this.startTime = undefined;
      this.formData = {};
      this.pagination.current = 1;
      this.pagination.pageSize = 10;
      this.advanceExpand = false;
      this.initData();
    },

    /**
     * 搜索模块按钮-收起/展开
     */
    toggleAdvance() {
      this.advanceExpand = !this.advanceExpand;
    },

    /**
     * 表格上方按钮-编辑零部件DVP
     */
    editComponentDvp() {
      this.isEdit = true;
    },

    /**
     * 编辑零部件DVP-确认按钮
     */
    saveEditDvp() {
      switch (this.tabKey) {
        case '1':
          this.$refs.editDvp.saveDvpEdit();
          break;
        case '2':
          this.$refs.templateEdit.saveTemplateEdit();
          break;
        case '3':
          this.$refs.historyDvpEdit.saveHistoryDvpEdit();
          break;
        case '4':
          this.$refs.projectFmeaEdit.saveProjectFmea();
          break;
        default:
          this.editDvpTittle = this.$t('plan.dvp.edit-dvp');
          break;
      }
    },

    /**
     * 编辑零部件DVP-取消按钮
     */
    cancelEditDvp() {
      this.$refs.editDvp.editParam.isEquivalent = 0;
      this.$refs.editDvp.editParam = { isEquivalent: 0 };
      this.isEdit = false;
      this.tabKey = '1'
    },

    /**
     * 编辑表格行数据弹框-编辑
     */
    editInit(val) {
      this.$refs.editTableRow.editParam.testId = val.testId;
      this.$refs.editTableRow.editParam.partNo = val.partNo;
      this.$refs.editTableRow.editParam.partName = val.partName;
      this.$refs.editTableRow.editParam.cpacCode = val.cpacCode;
      this.$refs.editTableRow.editParam.fndCode = val.fndCode;
      this.$refs.editTableRow.editParam.testType = val.testType || undefined;
      this.$refs.editTableRow.editParam.testSource = val.testSource || undefined;
      this.$refs.editTableRow.editParam.testProject = val.testProject;
      this.$refs.editTableRow.editParam.targetRequire = val.targetRequire;
      this.$refs.editTableRow.editParam.testStandard = val.testStandard;
      this.$refs.editTableRow.editParam.equivalentId = val.equivalentTestId;
      this.$refs.editTableRow.editParam.testSite = val.testSite;
      this.$refs.editTableRow.editParam.testOwnerId = val.testOwnerId;
      this.$refs.editTableRow.editParam.testOwner = val.testOwnerId || undefined;
      this.$refs.editTableRow.editParam.testStep = val.testStep;
      this.$refs.editTableRow.editParam.testFrequency = val.testFrequency;
      this.$refs.editTableRow.editParam.sampleNumber = val.sampleNumber;
      this.$refs.editTableRow.editParam.sampleType = val.sampleType;
      this.$refs.editTableRow.editParam.materialInventoryId = val.materialInventoryId;
      this.$refs.editTableRow.editParam.materialInventoryName = val.materialInventoryName;
      this.$refs.editTableRow.editParam.materialInventoryCode = val.materialInventoryCode;
      this.$refs.editTableRow.editParam.planStartTime = val.planStartTime;
      this.$refs.editTableRow.editParam.planEndTime = val.planEndTime;
      this.$refs.editTableRow.editParam.isEquivalent = val.isEquivalent;
      this.$refs.editTableRow.isUpload = !!val.materialInventoryId;
      // 材料清单回显
      if (val.isUpload && val.isUpload === 1) {
        let fileList = {
          uid: val.materialInventoryId,
          name: val.materialInventoryName,
          status: 'done',
          fileName: val.materialInventoryName,
          fileSize: val.materialInventorySize,
          filePath: val.materialInventoryPath,
          newFileName: val.materialInventoryNewName
        };
        this.$refs.editTableRow.fileList.push(fileList);
      }
      this.$refs.editTableRow.initData();
      this.isEditRow = true;
    },

    /**
     * 编辑表格行数据弹框-关闭
     */
    closeEditRowModal() {
      this.isEditRow = false;
    },

    /**
     * 上传材料清单弹框-关闭
     */
    closeUploadModal() {
      this.isUpload = false;
    },

    /**
     * 表格操作列-清空
     */
    clearDvpData(val) {
      let data = {
        dvpId: this.$store.getters.dvpId,
        testId: val.testId
      };
      this.$api.planTestEmpty(data).then(res => {
        if (res.flag) {
          this.$message.success(res.message);
          this.initData();
        } else {
          this.$message.error(res.message);
        }
      });
    },


    /**
     * 表格上方按钮-上传材料清单
     */
    uploadMaterialList() {
      this.isUpload = true;
      this.$refs.uploadMaterial.initSelectData();
    },

    /**
     * 申请变更 关闭弹窗
     */
    closeModal() {
      this.applyFlag = false;
      this.applyTableData = [];
    },

    /**
     * 材料清单文件下载
     * @param record
     */
    onClick(record) {
      this.download('/DFMEA/file/downloadFile', {
        id: record.materialInventoryId,
        fileType: 18,
      });
    },

    /**
     * 申请变更
     * @param record
     */
    applyChange(record) {
      // 行数据 赋值
      let rowData = [
        // 试验类型
        {
          changeContent: this.$t("plan.dvp.test-type"),
          currentDesc: record.testType || '-',
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验来源
        {
          changeContent: this.$t("plan.dvp.test-source"),
          currentDesc: record.testSource || '-',
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验项目
        {
          changeContent: this.$t("plan.dvp.test-project"),
          currentDesc: record.testProject,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 目标要求
        {
          changeContent: this.$t("base.info.development-objective-requirements"),
          currentDesc: record.targetRequire,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验标准
        {
          changeContent: this.$t("base.info.Test-standards"),
          currentDesc: record.testStandard,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 等效报告编号
        {
          changeContent: this.$t("plan.dvp.equivalent-experiment-report-number"),
          currentDesc: record.equivalentTestCode,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验地点
        {
          changeContent: this.$t("plan.dvp.test-location"),
          currentDesc: record.testSite,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 测试负责人
        {
          changeContent: this.$t("plan.dvp.test-user"),
          currentDesc: record.testOwner,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 计划开始时间
        {
          changeContent: this.$t("plan.dvp.scheduled-start-time"),
          currentDesc: record.planStartTime,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 计划结束时间
        {
          changeContent: this.$t("plan.dvp.scheduled-end-time"),
          currentDesc: record.planEndTime,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 测试阶段
        {
          changeContent: this.$t("plan.dvp.test-step"),
          currentDesc: record.testSite,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 测试次数
        {
          changeContent: this.$t("plan.dvp.test-count"),
          currentDesc: record.testFrequency,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 样件类型
        {
          changeContent: this.$t("plan.dvp.sample-type"),
          currentDesc: record.sampleType,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 样本数量
        {
          changeContent: this.$t("plan.dvp.number-of-samples"),
          currentDesc: record.sampleNumber,
          changeDesc: undefined,
          changeReason: undefined
        }

      ];
      // 赋值给申请变更弹窗的表格
      this.applyTableData = rowData;
      // 调用申请变更弹窗的初始化表格数据方法
      this.$refs.applyChange.initTableData(this.applyTableData);
      this.$refs.applyChange.testId = record.testId;
      this.$refs.applyChange.dvpType = this.dvpType;
      // 开启弹窗
      this.applyFlag = true;
    },

    /**
     * 表格上方按钮-导出
     */
    exportFile() {
      this.download('/DFMEA/partDvp/dvpPlanExcelExport', {
        dvpId: this.$store.getters.dvpId || '',
        partCode: this.formData.partCode || '',
        partName: this.formData.partName || '',
        fndId: this.formData.fndCode || '',
        ownerDepartmentId: this.formData.ownerDepartmentId || '',
        testOwner: this.formData.testOwner || '',
        testType: this.formData.testType || '',
        testSource: this.formData.testSource || '',
        systemTestSource: this.formData.systemTestSource || '',
        targetRequire: this.formData.targetRequire || '',
        testStandard: this.formData.testStandard || '',
        testSite: this.formData.testSite || '',
        planStartTime: this.formData.planStartTime || '',
        planEndTime: this.formData.planEndTime || '',
      });
    },

    /**
     * 搜索框-开始时间
     * @param date
     * @param dateString
     */
    handleQueryStartTime(date, dateString) {
      this.formData.planStartStartTime = dateString[0];
      this.formData.planStartEndTime = dateString[1];
    },

    /**
     * 搜索框-结束时间
     * @param date
     * @param dateString
     */
    handleQueryEndTime(date, dateString) {
      this.formData.planEndStartTime = dateString[0];
      this.formData.planEndEndTime = dateString[1];
    },


    /**
     * 表格 - 点击分页中数字触发的方法
     * @param pagination
     */
    handlePageChange(pagination) {
      this.pagination.current = pagination.current;
      this.pagination.pageSize = pagination.pageSize;
      this.pagination.pageNo = pagination.current;
      this.initData();
    },

    /**
     * 编辑零部件DVP弹框 tab回调函数
     * @param key
     */
    callback(key) {
      this.tabKey = key;
      switch (this.tabKey) {
        case '1':
          this.editDvpTittle = this.$t('plan.dvp.edit-dvp');
          // 不能直接调用子组件初始化方法，是因为tab容器还未加载完，就执行了子组件的初始化方法 浏览器控制台报错
          // 所以加上$nextTick,如下case同理
          this.$nextTick(() => { this.$refs.editDvp.initSelectData(); });
          break;
        case '2':
          this.editDvpTittle = this.$t('plan.dvp.template-edit');
          this.$nextTick(() => { this.$refs.templateEdit.initData(); });
          break;
        case '3':
          this.editDvpTittle = this.$t('plan.dvp.history-dvp-edit');
          this.$nextTick(() => { this.$refs.historyDvpEdit.initData(); });
          break;
        case '4':
          this.editDvpTittle = this.$t('plan.dvp.project-fmea-edit');
          break;
        default:
          this.editDvpTittle = this.$t('plan.dvp.edit-dvp');
          break;
      }
    },

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

</script>
<style lang="less" scoped>
.content-box {
  padding: 0 12px;
}

.layout-card {
  padding: 12px;
}

.table-mr {
  margin-top: 12px;
}

/deep/ .clickContent {
  color: #1890FF;
  cursor: pointer;
}
</style>
