<template>
  <div>
    <a-row class="content-box">
      <a-card class="layout-card">
        <a-form layout='vertical'>
          <a-row :gutter="24">
            <!--系统名称-->
            <a-col :lg='6' :md='12' :sm='24' :xl='6' :xs='24'>
              <a-form-item :label="$t('base.info.system-name')">
                <a-input
                  v-model.trim="formData.systemName"
                  :maxLength='500'
                  :placeholder="$t('base.info.please-enter-system-name')"
                  allowClear/>
              </a-form-item>
            </a-col>
            <!--需求资源-->
            <a-col :lg='6' :md='12' :sm='24' :xl='6' :xs='24'>
              <a-form-item :label="$t('base.info.required.resources')">
                <a-input
                  v-model.trim="formData.assets"
                  :maxLength='500'
                  :placeholder="$t('base.info.please-enter-required.resources')"
                  allowClear/>
              </a-form-item>
            </a-col>
            <!--验证内容-->
            <a-col :lg='6' :md='12' :sm='24' :xl='6' :xs='24'>
              <a-form-item :label="$t('base.info.verification-content')">
                <a-input
                  v-model.trim="formData.verify"
                  :maxLength='50'
                  :placeholder="$t('base.info.please-enter-verification-content')"
                  allowClear/>
              </a-form-item>
            </a-col>
            <!--开发目标要求-->
            <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
              <a-form-item :label="$t('base.info.development-objective-requirements')">
                <a-input
                  v-model.trim="formData.targetRequire"
                  :maxLength='50'
                  :placeholder="$t('base.info.please-enter-development-objective-requirements')"
                  allowClear/>
              </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'
                    :placeholder="$t('base.info.please-enter-test-standards')"
                    allowClear/>
							</a-form-item>
						</a-col>
            <!--是否开展实验项目-->
							<a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
								<a-form-item :label="$t('base.info.whether-to-develop-experimental-projects')">
									<a-select v-model="formData.isCarryOut" allowClear :placeholder="$t('base.info.please-enter-whether-to-develop-experimental-projects')">
                    <a-select-option :value="1">{{ $t('global-btn.yes') }}</a-select-option>
                    <a-select-option :value="0">{{ $t('global-btn.no') }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            <!--试验来源 DVP模板/DFMEA:0/1-->
            <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
              <a-form-item :label="$t('plan.dvp.test-source')">
                <a-select v-model="formData.testSource" :placeholder="$t('plan.dvp.please-enter-test-source')" allowClear>
                    <a-select-option :value="0">{{ $t('plan.dvp.test-source-dvp-template') }}</a-select-option>
                    <a-select-option :value="1">{{ $t('plan.dvp.test-source-DFMEA') }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
              <!--试验资质 是/否:1/0-->
              <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
                <a-form-item :label="$t('plan.dvp.test-qualification')">
                  <a-select v-model="formData.testCertification" :placeholder="$t('plan.dvp.please-enter-test-qualification')" allowClear>
                    <a-select-option :value="1">{{ $t('global-btn.yes') }}</a-select-option>
                    <a-select-option :value="0">{{ $t('global-btn.no') }}</a-select-option>
                  </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-location')">
                <a-input
                  v-model.trim="formData.testSite"
                  :maxLength='50'
                  :placeholder="$t('plan.dvp.please-enter-test-location')"
                  allowClear/>
              </a-form-item>
            </a-col>
            <!--试验责任方-->
            <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
              <a-form-item :label="$t('base.info.test-responsible-party')">
                <a-select allowClear v-model="formData.testOwner" :options="testOwnerList" @change="handleTestOwner" :placeholder="$t('plan.dvp.please_select-study-director')"></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-stage')">
                  <a-input
                    v-model.trim="formData.testStage"
                    :maxLength='50'
                    :placeholder="$t('plan.dvp.please-enter-test-stage')"
                    allowClear/>
                </a-form-item>
              </a-col>
            <!--样件类型-->
            <a-col :lg='6' :md='24' :sm='24' :xl='6' :xs='24'>
              <a-form-item :label="$t('plan.dvp.sample-type')">
                <a-input
                  v-model.trim="formData.sampleType"
                  :maxLength='50'
                  :placeholder="$t('plan.dvp.please-enter-sample-type')"
                  allowClear/>
              </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="handleStartTime" allowClear valueFormat="YYYY-MM-DD" style="width: 100%" />
              </a-form-item>
            </a-col>
            <!--计划结束时间-->
            <a-col :xl='6' :lg='6' :md='24' :sm='24' :xs='24'>
              <a-form-item :label="$t('plan.dvp.scheduled-end-time')">
                <a-range-picker v-model="endTime" @change="handleEndTime" 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' >
          <!-- 导出 -->
          <a-button @click='exportFile'>
            {{ $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="testSource" slot-scope="text, record">
            <span>{{ testSourceMap[record.testSource] || '-' }}</span>
          </template>
          <!-- 是否开展实验 -->
          <template slot="isCarryOut" slot-scope="text, record">
            <span>{{ isOrNotMap[record.isCarryOut] || '-' }}</span>
          </template>
          <!-- 试验资质 -->
          <template slot="testCertification" slot-scope="text, record">
            <span>{{ isOrNotMap[record.testCertification] || '-' }}</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>
            </span>
          </template>
        </a-table>
      </a-card>
    </a-row>
    <!-- 编辑模态框 -->
    <a-modal v-model="isEdit" :title="$t('plan.dvp.edit-dvp')" @ok="save" @cancel="offModal" :width="1000">
      <a-row :gutter="24">
        <a-form>
          <!-- 系统名称 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('base.info.system-name')">
              <a-input
                v-model="editParam.systemName"
                :disabled="true"
              />
            </a-form-item>
          </a-col>
          <!-- 需求资源 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('base.info.required-resources (vehicle rack)')">
              <a-input
                v-model="editParam.assets"
                :disabled="true"
              />
            </a-form-item>
          </a-col>
          <!-- 验证内容 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('base.info.verification-content')">
              <a-input
                v-model="editParam.verify"
                :disabled="true"
              />
            </a-form-item>
          </a-col>
          <!-- 开发目标要求 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('base.info.development-objective-requirements')">
              <a-input
                v-model="editParam.targetRequire"
                :disabled="true"
              />
            </a-form-item>
          </a-col>
          <!-- 试验标准 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('base.info.Test-standards')">
              <a-input
                v-model="editParam.testStandard"
                :disabled="true"
              />
            </a-form-item>
          </a-col>
          <!-- 是否开展实验项目 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('base.info.whether-to-develop-experimental-projects')">
              <a-select v-model="editParam.isCarryOut" allowClear @change="handleCarryOut" :placeholder="$t('base.info.please-enter-whether-to-develop-experimental-projects')">
                <a-select-option :value="1">{{ $t('global-btn.yes') }}</a-select-option>
                <a-select-option :value="0">{{ $t('global-btn.no') }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-form>
      </a-row>
      <a-row v-if="causeShowFlag" :gutter="24">
        <a-form>
          <!-- 试验责任方 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('base.info.test-responsible-party')">
              <a-select allowClear v-model="editParam.testOwnerId" :options="testOwnerList" @change="handleEditTestOwner" :placeholder="$t('plan.dvp.please_select-study-director')"></a-select>
            </a-form-item>
          </a-col>
          <!--试验资质-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('plan.dvp.test-qualification')">
              <a-select allowClear v-model="editParam.testCertification"  :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-qualification')">
                <a-select-option :value="1">{{ $t('global-btn.yes') }}</a-select-option>
                <a-select-option :value="0">{{ $t('global-btn.no') }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- 试验地点 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('plan.dvp.test-location')">
              <a-input
                v-model.trim="editParam.testSite"
                :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-location')"
                allowClear
                :maxLength='17'
              />
            </a-form-item>
          </a-col>
          <!-- 试验阶段 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('plan.dvp.test-stage')">
              <a-input
                v-model.trim="editParam.testStage"
                :maxLength='50'
                :placeholder="$t('plan.dvp.please-enter-test-stage')"
                allowClear/>
            </a-form-item>
          </a-col>
          <!-- 试验频次 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('plan.dvp.test-frequency')">
              <a-input
                v-model.trim="editParam.testFrequency"
                :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.test-frequency')"
                allowClear
                :maxLength='50'
              />
            </a-form-item>
          </a-col>
          <!-- 样本数量 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item required :label="$t('plan.dvp.number-of-samples')">
              <a-input-number v-model.number="editParam.count" :defaultValue="0" :min="1" :max="999" @change="handleCount" :placeholder="$t('plan.dvp.please_select-number-of-vehicles')" 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 required :label="$t('plan.dvp.sample-type')">
              <a-input
                v-model.trim="editParam.sampleType"
                :placeholder="$t('global-btn.please-enter') + $t('plan.dvp.sample-type')"
                allowClear
                :maxLength='50'
              />
            </a-form-item>
          </a-col>
          <!--计划开始时间-->
          <a-col :lg='16' :md='24' :sm='24' :xl='12' :xs='24'>
            <a-form-item required :label="$t('plan.dvp.scheduled-start-time')">
              <a-date-picker v-model="editParam.planStartTime" :disabled-date="disabledStartDate" @change="handleEditStartTime" allowClear valueFormat="YYYY/MM/DD" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <!--计划结束时间-->
          <a-col :lg='16'  :md='24' :sm='24' :xl='12'  :xs='24'>
            <a-form-item required :label="$t('plan.dvp.scheduled-end-time')">
              <a-date-picker v-model="editParam.planEndTime" :disabled-date="disabledEndDate" @change="handleEditEndTime" style="width: 100%" valueFormat="YYYY/MM/DD" allowClear />
            </a-form-item>
          </a-col>
        </a-form>
      </a-row>
      <!--不开展试验原因-->
      <a-row v-else :gutter="24">
        <a-form>
          <a-col :xl='24' :lg='24' :md='24' :sm='24' :xs='24'>
            <a-form-item required :label="$t('base.info.reason-for-not-conducting-the-experiment')">
              <a-textarea rows='6' v-model="editParam.notCarryOutCause" style="width: 100%;" :placeholder="$t('base.info.please-enter-reason-for-not-conducting-the-experiment')" :maxLength='500'></a-textarea>
            </a-form-item>
          </a-col>
        </a-form>
      </a-row>
    </a-modal>
    <ApplyForChange
      :applyFlag="applyFlag"
      :applyTableData="applyTableData"
      :dvpType="dvpType"
      @closeModal="closeModal"
      @initData="initData"
      ref="applyChange">
    </ApplyForChange>
  </div>
</template>

<script>

import ApplyForChange from "./applyChange/applyForChange";
import {getUrlParam} from "../../../common/common";
import moment from "moment";


export default {
  components: {
    ApplyForChange
  },
  data() {
    return{
      /** 查询数据 */
      formData:{
        systemName: undefined,          // 系统名称
        assets: undefined,              // 需求资源
        verify: undefined,              // 验证内容
        testSource: undefined,          // 试验来源
        targetRequire: undefined,       // 开发目标要求
        testStandard: undefined,        // 试验标准
        isCarryOut: undefined,          // 是否开展
        testCertification: undefined,   // 试验资质
        testSite: undefined,            // 试验地点
        testOwner: undefined,           // 试验负责人
        testStage: undefined,           // 试验阶段
        sampleType: undefined,          // 样本类型
        planStartTime: undefined,      // 计划开始时间
        planEndTime: undefined,        // 计划结束时间
      },
      /** 编辑弹框 用于记录初始值 */
      planStartTime: undefined,
      planEndTime: undefined,
      /** 清除时间选框用的 */
      endTime: undefined,
      startTime: undefined,
      /** 编辑模态框显隐 */
      isEdit: false,
      /** 编辑模态框数据 */
      editParam: {
        systemName: undefined,               // 系统名称
        dvpId: undefined,                    // DVP ID
        testId: undefined,                   // 试验ID
        isCarryOut: undefined,               // 是否开展试验项目 0否 1是
        notCarryOutCause: undefined,         // 不开展试验原因
        testOwnerId: undefined,              // 试验负责人
        testCertification: undefined,        // 试验资质
        testSite: undefined,                 // 试验地点
        testStage: undefined,                // 试验阶段
        testFrequency: undefined,            // 试验频率
        count: undefined,                    // 样本数量
        sampleType: undefined,               // 样本类型
        planStartTime: undefined,            // 计划开始时间
        planEndTime: undefined               // 计划结束时间
      },
      /** 折叠 展开 */
      advanceExpand: false,
      /** 不适用原因文本框显示/隐藏 */
      causeShowFlag: true,
      /** 试验责任方 */
      testOwnerList: [],
      /** 分页 */
      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('base.info.system-name'),
          width: 180,
          dataIndex: 'systemName',
          align: 'center',
          ellipsis: true
        },
        //需求资源(整车,台架)
        {
          title: this.$t('base.info.required-resources (vehicle rack)'),
          width: 180,
          dataIndex: 'assets',
          align: 'center',
          ellipsis: true
        },
        //试验来源
        {
          title: this.$t('plan.dvp.test-source'),
          width: 120,
          dataIndex: 'testSource',
          align: 'center',
          ellipsis: true,
          scopedSlots: {
            customRender: 'testSource'
          }
        },
        //验证内容
        {
          title: this.$t('base.info.verification-content'),
          width: 180,
          dataIndex: 'verify',
          align: 'center',
          ellipsis: true
        },
        //开发目标要求(接受标准)
        {
          title: this.$t('base.info.development-objective-requirements (acceptance criteria)'),
          width: 180,
          dataIndex: 'targetRequire',
          align: 'center',
          ellipsis: true
        },
        //试验标准
        {
          title: this.$t('base.info.Test-standards'),
          width: 180,
          dataIndex: 'testStandard',
          align: 'center',
          ellipsis: true
        },
        //是否开展实验项目
        {
          title: this.$t('base.info.whether-to-develop-experimental-projects'),
          width: 150,
          dataIndex: 'isCarryOut',
          align: 'center',
          ellipsis: true,
          scopedSlots: {
            customRender: 'isCarryOut'
          }
        },
        //不开展实验原因
        {
          title: this.$t('base.info.reason-for-not-conducting-the-experiment'),
          width: 200,
          dataIndex: 'notCarryOutCause',
          align: 'center',
          ellipsis: true
        },
        //试验责任方
        {
          title: this.$t('base.info.test-responsible-party'),
          width: 120,
          dataIndex: 'testOwner',
          align: 'center',
          ellipsis: true
        },
        //试验资质
        {
          title: this.$t('plan.dvp.test-qualification'),
          width: 180,
          dataIndex: 'testCertification',
          align: 'center',
          ellipsis: true,
          scopedSlots: {
            customRender: 'testCertification'
          }
        },
        //试验地点
        {
          title: this.$t('plan.dvp.test-location'),
          width: 120,
          dataIndex: 'testSite',
          align: 'center',
          ellipsis: true
        },
        //试验阶段
        {
          title: this.$t('plan.dvp.test-stage'),
          width: 120,
          dataIndex: 'testStage',
          align: 'center',
          ellipsis: true
        },
        //试验频次
        {
          title: this.$t('plan.dvp.test-frequency'),
          width: 120,
          dataIndex: 'testFrequency',
          align: 'center',
          ellipsis: true
        },
        //样本数量
        {
          title: this.$t('plan.dvp.number-of-samples'),
          width: 100,
          dataIndex: 'count',
          align: 'center',
          ellipsis: true
        },
        //样件类型
        {
          title: this.$t('plan.dvp.sample-type'),
          width: 120,
          dataIndex: 'sampleType',
          align: 'center',
          ellipsis: true
        },
        //计划开始时间
        {
          title: this.$t('plan.dvp.scheduled-start-time'),
          width: 150,
          dataIndex: 'planStartTime',
          align: 'center',
          ellipsis: true
        },
        //计划结束时间
        {
          title: this.$t('plan.dvp.scheduled-end-time'),
          width: 150,
          dataIndex: 'planEndTime',
          align: 'center',
          ellipsis: true
        },
        //操作
        {
          title: this.$t('global-constant.operation'),
          align: 'center',
          width: 100,
          fixed: 'right',
          scopedSlots: {customRender: 'action'},
          ellipsis: true
        }
      ],
      /** 试验来源Map 用于表格插槽显示 */
      testSourceMap: {
        0: this.$t("plan.dvp.test-source-dvp-template"),
        1: this.$t("plan.dvp.test-source-DFMEA")
      },
      /** 是/否Map 用于表格插槽显示 试验资质/是否开展试验 */
      isOrNotMap: {
        0: this.$t("global-btn.no"),
        1: this.$t("global-btn.yes")
      },
      /** 申请变更弹窗标识 */
      applyFlag: false,
      /** 申请变更弹窗表格数据 */
      applyTableData: [],
      /** 变更申请按钮显隐 */
      applyButtonFlag: false,
      /** 权限 */
      isLook: false,
      /** dvpType system:系统DVP，components:零部件DVP */
      dvpType: "system"

    }
  },
  mounted() {
    this.isLook = this.$api.isLook === '1'
    this.initData();
    this.getTestOwner();
    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.$api.initDvpPlanList(_that.formData).then(res => {
        if (res.flag) {
          _that.tableData = res.data.data;
          _that.pagination.total = res.data.total;
        } 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 ;
        }
      });
    },
    /**
     * 获取试验负责人下拉选项
     */
    getTestOwner() {
      let data = {
        dvpId: this.$store.getters.dvpId,
        dvpType: this.$store.getters.dvpType
      }
      // 获取DVP团队成员中有编辑权限的用户
      this.$api.getSystemDvpTeamMember(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);
          }
        }
      });
    },

    /**
     * 查询
     */
    search() {
      this.initData();
    },

    /**
     * 重置
     */
    reset() {
      this.startTime = undefined;
      this.endTime = undefined;
      this.formData = {};
      this.pagination.current = 1;
      this.pagination.pageSize = 10;
      this.advanceExpand = false;
      this.initData();
    },

    /**
     * 收起/展开
     */
    toggleAdvance() {
      this.advanceExpand = !this.advanceExpand;
    },

    /**
     * 非空校验 判断是否为空
     * @returns {boolean}
     */
    isNull() {
      let _that = this;
      let message = [];
      // 是否开展试验项目
      if (_that.editParam.isCarryOut == undefined && _that.editParam.isCarryOut == null) {
        message.push(_that.$t('global-constant.please-enter') + _that.$t('base.info.whether-to-develop-experimental-projects'));
      } else if (_that.editParam.isCarryOut === 1) { // 如果点击是，则校验以下值，点击否只校验不适用原因
        // 试验责任人
        if (!_that.editParam.testOwnerId) {
          message.push(_that.$t('global-constant.please-enter') + _that.$t('base.info.test-responsible-party'));
        }
        // 试验资质
        if (_that.editParam.testCertification == undefined && _that.editParam.testCertification == null) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('plan.dvp.test-qualification'));
        }
        // 样本数量
        if (!_that.editParam.count) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('plan.dvp.number-of-samples'));
        }
        // 试验地点
        if (!_that.editParam.testSite) {
          message.push(_that.$t('global-constant.please-enter') + _that.$t('plan.dvp.test-location'));
        }
        // 试验阶段
        if (!_that.editParam.testStage) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('plan.dvp.test-stage'));
        }
        // 试验频次
        if (!_that.editParam.testFrequency) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('plan.dvp.test-frequency'));
        }
        // 样件类型
        if (!_that.editParam.sampleType) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('plan.dvp.sample-type'));
        }
        // 计划开始时间
        if (!_that.editParam.planStartTime) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('plan.dvp.scheduled-start-time'));
        }
        // 计划结束时间
        if (!_that.editParam.planEndTime) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('plan.dvp.scheduled-end-time'));
        }
      } else {
        // 不适用原因
        if (!_that.editParam.notCarryOutCause) {
          message.push(_that.$t('global-constant.please-select') + _that.$t('base.info.reason-for-not-conducting-the-experiment'));
        }
      }
      if (message && message.length > 0) {
        for (let i = 0; i < message.length; i++) {
          _that.$notification.warning({message: message[i]});
        }
        return false;
      }
      return true;
    },

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

    /** 编辑 保存 */
    save() {
      if (!this.isNull()) {
        return false;
      }
      // 如果是否开展试验选择了否，则清除试验相关的信息
      if (this.editParam.isCarryOut === 0) {
        this.editParam.planEndTime = undefined;
        this.editParam.planStartTime = undefined;
        this.editParam.count = undefined;
        this.editParam.sampleType = undefined;
        this.editParam.testOwnerId = undefined;
        this.editParam.testFrequency = undefined;
        this.editParam.testStage = undefined;
        this.editParam.testSite = undefined;
        this.editParam.testCertification = undefined;
      } else { // 如果选择了是，则清除不适用原因
        this.editParam.notCarryOutCause = undefined;
      }

     let data = {
       dvpId: this.editParam.dvpId,
       testId: this.editParam.testId,
       isCarryOut: this.editParam.isCarryOut,
       notCarryOutCause: this.editParam.notCarryOutCause,
       testOwnerId: this.editParam.testOwnerId,
       testCertification: this.editParam.testCertification,
       testSite: this.editParam.testSite,
       testStage: this.editParam.testStage,
       testFrequency: this.editParam.testFrequency,
       count: this.editParam.count,
       sampleType: this.editParam.sampleType,
       planStartTime: this.editParam.planStartTime,
       planEndTime: this.editParam.planEndTime
     };
      this.$api.editTest(data).then(res => {
        if (!res.flag) {
          this.$notification.error({message: res.message});
        } else {
          this.$notification.success({message: res.message});
          this.initData();
          this.isEdit = false;
        }
      });
    },

    /**
     * 编辑弹框 初始化
     */
    editInit(val) {
      //以下写法会导致更改编辑弹框内容，连同编辑弹框外的表格对应的字段也更改
      // this.editParam = val;
      //所以这样写 this.editParam = {...val};
      //或者如下写，挨个赋值
      this.editParam.isCarryOut = val.isCarryOut;
      this.editParam.systemName = val.systemName;
      this.editParam.assets = val.assets;
      this.editParam.verify = val.verify;
      this.editParam.testSource = val.testSource;
      this.editParam.targetRequire = val.targetRequire;
      this.editParam.isCarryOut = val.isCarryOut;
      this.editParam.testCertification = val.testCertification || undefined;
      this.editParam.planEndTime = val.planEndTime;
      this.editParam.planStartTime = val.planStartTime;
      this.editParam.count = val.count;
      this.editParam.sampleType = val.sampleType;
      this.editParam.testOwnerId = val.testOwnerId;
      this.editParam.testFrequency = val.testFrequency;
      this.editParam.testStage = val.testStage;
      this.editParam.testSite = val.testSite;
      this.editParam.testStandard = val.testStandard;
      this.editParam.notCarryOutCause = val.notCarryOutCause;
      this.editParam.dvpId = this.$store.getters.dvpId;
      this.editParam.testId = val.testId;
      this.testOwnerList.forEach( item => {
        if (item.label === val.testOwner) {
          this.editParam.testOwnerId = item.value;
        }
      });
      this.planStartTime = val.planStartTime;
      this.planEndTime = val.planEndTime;
      if (val.isCarryOut === 0) {
        this.causeShowFlag = false;
      } else {
        this.causeShowFlag = true;
      }
      this.isEdit = true;
    },


    /** 编辑 取消 */
    offModal() {
      this.isEdit = false;
      this.editParam.isCarryOut = undefined;
      this.editParam.planEndTime = undefined;
      this.editParam.planStartTime = undefined;
      this.editParam.count = undefined;
      this.editParam.sampleType = undefined;
      this.editParam.testOwnerId = undefined;
      this.editParam.testFrequency = undefined;
      this.editParam.testStage = undefined;
      this.editParam.testSite = undefined;
      this.editParam.testCertification = undefined;
    },
    /**
     * 导出
     */
    exportFile() {
      this.download('/DFMEA/dvp/system/dvpPlanExcelExport', {
        dvpId: this.$store.getters.dvpId || '',
        systemName: this.formData.systemName || '',
        assets: this.formData.assets || '',
        verify: this.formData.verify || '',
        testSource: this.formData.testSource || '',
        targetRequire: this.formData.targetRequire || '',
        testStandard: this.formData.testStandard || '',
        isCarryOut: this.formData.isCarryOut || '',
        testCertification: this.formData.testCertification || '',
        testSite: this.formData.testSite || '',
        testOwner: this.formData.testOwner || '',
        testStage: this.formData.testStage || '',
        sampleType: this.formData.sampleType || '',
        planStartTime: this.formData.planStartTime || '',
        planEndTime: this.formData.planEndTime || '',
      });
    },

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

    /**
     * 计划开始时间 时间选择器Change方法 查询
     */
    handleStartTime(date, dateString){
      this.formData.planStartStartTime = dateString[0];
      this.formData.planStartEndTime = dateString[1];
    },

    /**
     * 计划结束时间 时间选择器Change方法 查询
     */
    handleEndTime(date, dateString){
      this.formData.planEndStartTime = dateString[0];
      this.formData.planEndEndTime = dateString[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;
    },

    /**
     * 试验负责人下拉改变 查询
     * @param val
     */
    handleTestOwner(val) {
      this.formData.testOwner = val;
    },

    /**
     * 试验负责人下拉改变 编辑
     * @param val
     */
    handleEditTestOwner(val) {
      this.editParam.testOwnerId = val;
    },

    /**
     * 样本数量下拉改变 编辑
     * @param val
     */
    handleCount(val) {
      this.editParam.count = val;
    },

    /**
     * 是否开展实验项目 编辑
     * @param val
     */
    handleCarryOut(val){
      this.editParam.isCarryOut = val;
      if (val === 0) {
        this.causeShowFlag = false;
      } else {
        this.causeShowFlag = true;
      }
    },
    /**
     * 申请变更
     * @param record
     */
    applyChange(record) {
      // 行数据 赋值
      let rowData = [
        // 是否开展实验
        {
          changeContent: this.$t("base.info.whether-to-develop-experimental-projects"),
          currentDesc: this.isOrNotMap[record.isCarryOut] || "-",
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验负责人
        {
          changeContent: this.$t("base.info.test-responsible-party"),
          currentDesc: record.testOwner,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验资质
        {
          changeContent: this.$t("plan.dvp.test-qualification"),
          currentDesc: this.isOrNotMap[record.testCertification] || "-",
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验地点
        {
          changeContent: this.$t("plan.dvp.test-location"),
          currentDesc: record.testSite,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验阶段
        {
          changeContent: this.$t("plan.dvp.test-stage"),
          currentDesc: record.testStage,
          changeDesc: undefined,
          changeReason: undefined
        },
        // 试验频率
        {
          changeContent: this.$t("plan.dvp.test-frequency"),
          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.count,
          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("base.info.reason-for-not-conducting-the-experiment"),
          currentDesc: "-",
          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;
      // 是否显示不开展试验原因
      if (record.isCarryOut === 1 || record.isCarryOut === undefined || record.isCarryOut === null) {
        this.$refs.applyChange.causeShowFlag = true;
      }
      // 开启弹窗
      this.applyFlag = true;
    }
  }
}

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

.layout-card {
 padding: 12px;
}

.table-mr {
  margin-top: 12px;
}
</style>
