<template>
  <div class="p12">
    <a-row :gutter="24" class="top-card">
      <!-- DVP计划-检索条件 -->
      <a-card class="top-card-margin p12">
        <a-form layout="vertical">
          <!-- 验证内容 -->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('base.info.verification-content')">
              <a-input v-model.trim="queryParam.verify" :placeholder="$t('base.info.please-enter-verification-content')" allowClear :maxLength='500'/>
            </a-form-item>
          </a-col>
          <!-- 试验来源 -->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('plan.dvp.test-source')">
              <a-select v-model="queryParam.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>
          <!-- 样本类型 -->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('base.info.sample-type')">
              <a-input v-model.trim="queryParam.sampleType" :placeholder="$t('plan.dvp.please-enter-sample-type')" allowClear :maxLength='50'/>
            </a-form-item>
          </a-col>
          <!-- 实际开始时间 -->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('dvp.full.vehicle.dvp.actual-start-time')">
              <a-range-picker v-model="queryParam.actualStartTime" @change="changeStartTime" allowClear valueFormat="YYYY-MM-DD" style="width: 100%" />
            </a-form-item>
          </a-col>
          <span v-show="advanceExpand">
          <!-- 实际结束时间 -->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('dvp.full.vehicle.dvp.actual-end-time')">
              <a-range-picker v-model="queryParam.actualEndTime" @change="changeEndTime" allowClear valueFormat="YYYY-MM-DD" style="width: 100%" />
            </a-form-item>
          </a-col>
          <!--DV是否完成-->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('base.info.is-DV-completed')">
              <a-select v-model="queryParam.isFinish" :placeholder="$t('base.info.please-select-is-DV-completed')">
                <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>
          <!-- DV试验结果 -->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('base.info.DV-test-results')">
              <a-select v-model="queryParam.testResult" :placeholder="$t('base.info.please-select-DV-test-results')" allowClear>
                <a-select-option :value="1">{{ $t('global-btn.pass') }}</a-select-option>
                <a-select-option :value="0">{{ $t('global-btn.fail') }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- 试验报告编号 -->
          <a-col :lg="8" :md="12" :sm="24" :xl="6" :xs="24">
            <a-form-item :label="$t('dvp.full.vehicle.dvp.test-report-code')">
              <a-input v-model.trim="queryParam.testReportCode" :placeholder="$t('dvp.full.vehicle.dvp.please-enter-test-report-code')" allowClear :maxLength='50'/>
            </a-form-item>
          </a-col>
          </span>
          <a-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
            <a-form-item class='tr' :label="'\xa0'">
              <!-- 展开/收起 -->
              <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'  class="ml10">
                {{ $t('global-btn.reset') }}
              </a-button>
              <!-- 查询 -->
              <a-button @click='search' type="primary" class="ml10">
                {{ $t('global-btn.search') }}
              </a-button>
            </a-form-item>
          </a-col>
        </a-form>
      </a-card>
    </a-row>
    <a-row class="mt10">
      <a-card class="p12 card-overFlow" :style="{ height: tableHeight }">
        <template slot="extra">
          <!-- 导出 -->
          <a-button class="exportBtn" @click='exportFile'>
            {{ $t('global-btn.export') }}
          </a-button>
        </template>
        <a-table
            :columns="columns"
            :dataSource="tableData"
            :pagination="pagination"
            :rowKey="(record, index) => { return index; }"
            :loading="tableLoading"
            size="small"
            @change="handleOuterTableChange"
            :scroll="{ x: 2100, y: tableHeightValue }">
          <!-- 序号列 -->
          <template slot='index' slot-scope='text, record, index'>
            <span>{{ (pagination.pageCurrent - 1) * pagination.pageSize + index + 1 }}</span>
          </template>
          <!-- 是否开展实验 -->
          <template slot="isFinish" slot-scope="text, record">
            <span>{{ isOrNotMap[record.isFinish] || '-' }}</span>
          </template>
          <!-- 试验结果 -->
          <template slot="testResult" slot-scope="text, record">
            <span>{{ passOrNotMap[record.testResult] || '-' }}</span>
          </template>
          <!-- 整改后试验结果 -->
          <template slot="reformVerifyResult" slot-scope="text, record">
            <span>{{ passOrNotMap[record.reformVerifyResult] || '-' }}</span>
          </template>
          <!--操作-->
          <template slot='operation' slot-scope='name, row'>
            <!-- 编辑 -->
            <a href='javascript: void(0);' @click='editInit(row)' :disabled="isLook">
              <a-tooltip :title="$t('global-btn.edit')">
                <i class='iconfont iconicon-bianji'></i>
              </a-tooltip>
            </a>
          </template>
        </a-table>
      </a-card>
    </a-row>
    <!-- 编辑模态框 -->
    <a-modal v-model="isEdit" :title="$t('header.dvp-verify')" @ok="save" @cancel="offModal" :width="1100">
      <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 :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 :label="$t('plan.dvp.test-source')">
              <a-select v-model="editParam.testSource" :disabled="true" :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>
          <!-- 样本数量 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('plan.dvp.number-of-samples')">
              <a-input v-model="editParam.count" :disabled="true"/>
            </a-form-item>
          </a-col>
          <!-- 样本类型 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('base.info.sample-type')">
              <a-input v-model="editParam.sampleType" :disabled="true"/>
            </a-form-item>
          </a-col>
          <!-- 实际开始时间 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('dvp.full.vehicle.dvp.actual-start-time')" required>
              <a-date-picker
                :placeholder="$t('global-btn.please-select') + $t('dvp.full.vehicle.dvp.actual-start-time')"
                style="width: 100%"
                valueFormat="YYYY/MM/DD"
                :disabled-date="disabledStartDate"
                @change='changeStartTime'
                :value="editParam.actualStartTime"
              />
            </a-form-item>
          </a-col>
          <!-- 实际结束时间 -->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('dvp.full.vehicle.dvp.actual-end-time')" required>
              <a-date-picker
                :placeholder="$t('global-btn.please-select') + $t('dvp.full.vehicle.dvp.actual-end-time')"
                style="width: 100%"
                valueFormat="YYYY/MM/DD"
                :disabled-date="disabledEndDate"
                @change='changeEndTime'
                :value="editParam.actualEndTime"
              />
            </a-form-item>
          </a-col>
          <!--DV是否完成-->
          <a-col :lg="16" :md="24" :sm="24" :xl="12" :xs="24">
            <a-form-item :label="$t('base.info.is-DV-completed')" required>
              <a-select v-model="editParam.isFinish"  :disabled="isFinishDisabled" :placeholder="$t('global-btn.please-select') + $t('base.info.is-DV-completed')">
                <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 :gutter="24">
        <!-- 当DV完成值为是的时候，显示表格 -->
        <div class="p12" v-if="editParam.isFinish == 1">
          <!-- 整改记录表格 -->
          <a-form-item :label="$t('base.info.rectification-record')" required>
          <a-table
            :columns="editColumns"
            :dataSource="editTableData"
            :pagination="false"
            :rowKey="(record, index) => { return index; }"
            :loading="tableLoading"
            size="small"
            bordered
            :scroll="{ x: 950, y: tableHeightValue }">
            <!-- 整改后试验结果 -->
            <template slot="testResult" slot-scope="text, row,index">
              <span v-if="row.logId"> {{ passOrNotMap[row.testResult] || '-' }} </span>
              <a-select
                v-else
                v-model="row.testResult"
                style="width: 120px"
                @change="handleResult(row,row.testResult,index)"
              >
                <a-select-option :value="0">{{ $t('global-btn.fail') }}</a-select-option>
                <a-select-option :value="1">{{ $t('global-btn.pass') }}</a-select-option>
              </a-select>
            </template>
            <!-- 整改后试验报告编号 -->
            <template slot="testReportCode" slot-scope="text, row">
              <span v-if="row.logId"> {{ row.testReportCode }} </span>
              <a-textarea v-else autoSize v-model.trim="row.testReportCode" :maxLength="128" :disabled="row.isDisabled"></a-textarea>
            </template>
            <!-- 整改后试验报告 -->
            <template slot="testReportId" slot-scope="text, row, index">
              <div v-if="row.testReportId">
               <span style=' font-size:15px;display:block'>
                 <a-icon type="paper-clip"/>
                 <a style='color:#1890FF' :href="$api.requestUrl + '/DFMEA/file/downloadFile.do?id=' +row.fileId + '&fileType=17'">
                  {{ row.fileName }}
                 </a>
               </span>
              </div>
              <div v-else>
                <!-- 上传 -->
                <a-upload
                  name='file'
                  v-if="row.logId == null"
                  :disabled="row.isDisabled"
                  action=''
                  accept='.jpg,.txt,.png,.doc,.docx,.xls,.xlsx,.pdf,.pptx'
                  :fileList='fileList'
                  :showUploadList="currentRowIndex.indexOf(index) !== -1"
                  :customRequest='addUploadFile'
                  :remove='removeFile'>
                  <a class='operation-btn ml16'>
                    <a-tooltip :title="$t('global-btn.select-file')">
                      <a-icon type="upload" @click="fileShow(index)"/>
                    </a-tooltip>
                  </a>
                </a-upload>
              </div>
            </template>
            <!-- 整改后试验计划 -->
            <template slot="reformVerifyPlan" slot-scope="text, row">
              <span v-if="row.logId"> {{ row.reformVerifyPlan }} </span>
              <a-textarea v-else autoSize v-model.trim="row.reformVerifyPlan" :maxLength="128" :disabled="row.isDisabled"></a-textarea>
            </template>
            <!-- 序号列 -->
            <template slot='index' slot-scope='text, record, index'>
              <span>{{ index + 1 }}</span>
            </template>
            <!-- 操作 -->
            <template slot="action" slot-scope="name, row, index">
              <div v-if='row.logId == null'>
                <!-- 保存 -->
                <a class="operate-btn"
                   :disabled="row.isDisabled"
                   @click="saveReform(row)">
                  <a-tooltip :title="$t('global-btn.save')">
                    <a-icon type="check"/>
                  </a-tooltip>
                </a>
                <!-- 取消 -->
                <a class="ml10 operate-btn" :disabled="row.isDisabled" @click="cancel(row)">
                  <a-tooltip :title="$t('global-btn.cancel')">
                    <a-icon type="undo"/>
                  </a-tooltip>
                </a>
              </div>
            </template>
          </a-table>
          </a-form-item>
        </div>
      </a-row>
    </a-modal>
  </div>
</template>

<script>
import moment from "moment";

export default {
  name: "dvpVerify",
  data() {
    return {
      /** 查询参数 */
      queryParam: {
        verify: undefined,               // 验证内容
        actualStartTime: undefined,      // 实际开始时间
        actualEndTime: undefined,        // 实际结束时间
        testSource: undefined,          // 试验来源
        sampleType: undefined,         // 样本类型
        testResult: undefined,         // DV试验结果
        testReportCode: undefined,     // 测试报告编号
        isFinish: undefined            // DV是否完成
      },
      /** 分页实体 */
      pagination: {
        pageCurrent: 1,
        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, //总条数,
        position: "bottom"
      },
      /** 表头信息 */
      columns: [
        {
          // 序号
          title: () => this.$t('dvp.full.vehicle.dvp.serial-number'),
          width: 100,
          fixed: 'left',
          align: 'center',
          scopedSlots: {
            customRender: 'index'
          },
          ellipsis: true,
        },
        // 系统名称
        {
          title: () => this.$t('base.info.system-name'),
          width: 100,
          fixed: 'left',
          align: 'center',
          dataIndex: 'systemName',
          ellipsis: true
        },
        // 验证内容
        {
          title: () => this.$t('base.info.verification-content'),
          width: 200,
          fixed: 'left',
          align: 'center',
          dataIndex: 'verify',
          ellipsis: true
        },
        // 样本数量
        {
          title: () => this.$t('plan.dvp.number-of-samples'),
          width: 200,
          align: 'center',
          dataIndex: 'count',
          ellipsis: true
        },
        // 样本类型
        {
          title: () => this.$t('base.info.sample-type'),
          width: 150,
          align: 'center',
          dataIndex: 'sampleType',
          ellipsis: true
        },
        // 实际开始时间
        {
          title: () => this.$t('dvp.full.vehicle.dvp.actual-start-time'),
          width: 150,
          align: 'center',
          dataIndex: 'actualStartTime',
          ellipsis: true
        },
        // 实际结束时间
        {
          title: () => this.$t('dvp.full.vehicle.dvp.actual-end-time'),
          width: 150,
          align: 'center',
          dataIndex: 'actualEndTime',
          ellipsis: true
        },
        // DV是否完成
        {
          title: () => this.$t('base.info.is-DV-completed'),
          width: 100,
          align: 'center',
          dataIndex: 'isFinish',
          ellipsis: true,
          scopedSlots: {
            customRender: 'isFinish'
          }
        },
        // DV试验结果
        {
          title: () => this.$t('base.info.DV-test-results'),
          width: 100,
          align: 'center',
          dataIndex: 'testResult',
          ellipsis: true,
          scopedSlots: {
            customRender: 'testResult'
          }
        },
        // 试验报告编号
        {
          title: () => this.$t('dvp.full.vehicle.dvp.test-report-code'),
          width: 200,
          align: 'center',
          dataIndex: 'testReportCode',
          ellipsis: true
        },
        // 整改后验证计划
        {
          title: () => this.$t('base.info.verification-plan-after-rectification'),
          width: 150,
          align: 'center',
          dataIndex: 'reformVerifyPlan',
          ellipsis: true,
        },
        // 整改后验证结果
        {
          title: () => this.$t('base.info.verification-results-after-rectification'),
          width: 150,
          align: 'center',
          dataIndex: 'reformVerifyResult',
          ellipsis: true,
          scopedSlots: {
            customRender: 'reformVerifyResult'
          }
        },
        // 操作
        {
          title: () => this.$t('global-constant.operation'),
          dataIndex: 'operation',
          key: 'operation',
          align: 'center',
          width: 100,
          scopedSlots: {
            customRender: 'operation'
          },
          fixed: 'right'
        }
      ],
      /** 表格数据 */
      tableData: [],
      /** 编辑模态框数据 */
      editParam: {
        systemName: undefined,                // 系统名称
        verify: undefined,                    // 验证内容
        testSource: undefined,                // 试验来源
        count: undefined,                     // 样本数量
        sampleType: undefined,                // 样本类型
        actualStartTime: undefined,           // 实际开始时间
        actualEndTime: undefined,             // 实际结束时间
        isFinish: undefined                   // 是否完成
      },
      /** 编辑 表头信息 */
      editColumns: [
        // 上传次数
        {
          title: () => this.$t('base.info.number-of-upload'),
          width: 50,
          align: 'center',
          scopedSlots: {
            customRender: 'index'
          },
          ellipsis: true
        },
        // 整改结果
        {
          title: () => this.$t('base.info.rectification-result'),
          width: 80,
          align: 'center',
          dataIndex: 'testResult',
          scopedSlots: {
            customRender:'testResult'
          },
          ellipsis: true,
        },
        // 整改试验报告编号
        {
          title: () => this.$t('base.info.rectification-test-report-code'),
          width: 150,
          align: 'center',
          dataIndex: 'testReportCode',
          scopedSlots: {
            customRender: 'testReportCode'
          },
          ellipsis: true,
        },
        // 整改试验报告
        {
          title: () => this.$t('base.info.rectification-test-report'),
          width: 150,
          align: 'center',
          dataIndex: 'testReportId',
          scopedSlots: {
            customRender: 'testReportId'
          },
          ellipsis: true,
        },
        // 整改试验计划
        {
          title: () => this.$t('base.info.verification-plan-after-rectification'),
          width: 150,
          align: 'center',
          dataIndex: 'reformVerifyPlan',
          scopedSlots: {
            customRender: 'reformVerifyPlan'
          },
          ellipsis: true,
        },
        //操作
        {
          title: this.$t('global-constant.operation'),
          align: 'center',
          width: 50,
          scopedSlots: {customRender: 'action'},
          ellipsis: true
        }
      ],
      /** 编辑 表格数据 */
      editTableData: [],
      /** 折叠 展开 */
      advanceExpand: false,
      /** 表格Loading */
      tableLoading: false,
      /** 编辑模态框显隐 */
      isEdit: false,
      /** 试验报告附件list */
      fileList: [],
      /** 实际开始时间 */
      actualStartTime: undefined,
      /** 实际结束时间 */
      actualEndTime: undefined,
      /** DV是否完成 选择框 可选/不可选 */
      isFinishDisabled: true,
      /** 高度计算 */
      tableHeight: window.innerHeight - 350 + 'px',
      tableHeightValue: window.innerHeight - 350,
      isLook: false,
      /** 是/否Map 用于表格插槽显示 DV是否完成 */
      isOrNotMap: {
        0: this.$t("global-btn.no"),
        1: this.$t("global-btn.yes")
      },
      /** 通过/不通过Map 用于表格插槽显示 DV验证结果 */
      passOrNotMap: {
        0: this.$t("global-btn.fail"),
        1: this.$t("global-btn.pass")
      },
      /** 控制上传文件显示用的 */
      currentRowIndex: []
    }
  },
  created() {
    this.isLook = this.$api.isLook === '1'
    this.initSystemDvpVerifyList()
    window.onresize = () => {
      this.tableHeight = window.innerHeight - 350 + 'px';
      this.tableHeightValue = window.innerHeight - 350;
    };
  },
  methods: {
    /** 获取列表数据 */
    initSystemDvpVerifyList() {
      let _that = this;
      _that.tableLoading = true;
      let data = {
        dvpId: _that.$store.getters.dvpId,
        verify: _that.queryParam.verify,
        actualStartStartTime: _that.queryParam.actualStartStartTime,
        actualStartEndTime: _that.queryParam.actualStartEndTime,
        actualEndStartTime: _that.queryParam.actualEndStartTime,
        actualEndEndTime: _that.queryParam.actualEndEndTime,
        testSource: _that.queryParam.testSource,
        sampleType: _that.queryParam.sampleType,
        testResult: _that.queryParam.testResult,
        testReportCode: _that.queryParam.testReportCode,
        isFinish: _that.queryParam.isFinish,
        pagination: {
          pageSize: _that.pagination.pageSize,
          pageCurrent: _that.pagination.current
        }
      };
      this.$api.initSystemDvpVerifyList(data).then(res => {
        if (!res.flag) {
          this.$notification.error({message: res.message});
        }
        _that.tableData = res.data.data || [];
        _that.pagination.total = res.data.total || 0;
      }).finally(() => {
        _that.tableLoading = false;
      });
    },
    /** 导出 */
    exportFile() {
      this.download('/DFMEA/dvp/system/dvpVerifyExcelExport', {
        dvpId: this.$store.getters.dvpId || '',
        verify: this.queryParam.verify || '',
        testSource: this.queryParam.testSource || '',
        sampleType: this.queryParam.sampleType || '',
        actualStartTime: this.queryParam.actualStartTime || '',
        actualEndTime: this.queryParam.actualEndTime || '',
        isFinish: this.queryParam.isFinish || '',
        testResult: this.queryParam.testResult || '',
        testReportCode: this.queryParam.testReportCode || '',
      });
    },
    /** 查询 */
    search() {
      this.pagination.pageCurrent = 1;
      this.initSystemDvpVerifyList()
    },
    /** 重置 */
    reset() {
      this.queryParam = [];
      this.initSystemDvpVerifyList();
    },

    /** 编辑弹框 表格初始化 */
    editTableDataInit(testId) {
      let data = {
        dvpId: this.$store.getters.dvpId,
        testId: testId
      };
      let _that = this;
      _that.$api.dvpVerifyEditInit(data).then(res => {
        if (!res.flag) {
          _that.$notification.error({message: res.message});
        }
        _that.editParam.testSource = res.data.dvpSystemDetail.testSource;
        _that.editParam.testId = res.data.dvpSystemDetail.testId;
        _that.editTableData = res.data.dvpSystemVerifyInitModels || [];
        _that.editTableData.forEach( item => {
          if (_that.editTableData.testReportId) {
            let fileList = {
              uid: item.fileId,
              name: item.fileName,
              status: 'done',
              fileName: item.fileName,
              filePath: item.filePath,
              newFileName: item.newFileName
            };
            _that.fileList.push(fileList);
          }
          item.testResult = parseInt(item.testResult);
        });
        if (_that.editTableData.length == 0  || _that.editTableData[_that.editTableData.length - 1].testResult === 0) {
          let addDate = {
            testResult: 1,
            testReportCode: '',
            reformVerifyPlan: ''
          };
          _that.editTableData.push(addDate);
        }
      });
    },

    /** 编辑模态框及模态框数据获取 */
    editInit(row) {
      // 编辑弹框内容
      this.editParam.systemName = row.systemName;
      this.editParam.verify = row.verify;
      this.editParam.sampleType = row.sampleType;
      this.editParam.count = row.count;
      this.editParam.actualStartTime = row.actualStartTime;
      this.editParam.actualEndTime = row.actualEndTime;
      this.actualStartTime = row.actualStartTime;
      this.actualEndTime = row.actualEndTime;
      this.editParam.isFinish = row.isFinish;

      // 当实际开始时间和实际结束时间都有值时，才能选择DV是否完成
      if (this.editParam.actualStartTime && this.editParam.actualEndTime) {
        this.isFinishDisabled = false;
        this.editParam.isFinish = 1;
      } else {
        this.editParam.isFinish = 0;
      }
      // 表格初始化
      this.editTableDataInit(row.testId);
      this.isEdit = true;
    },


    /** 控制上传文件用的 记录点击行的index  */
    fileShow(val) {
      this.currentRowIndex.push(val);
    },

    /**
     * 新建整改记录 上传文件
     * @param file
     */
    addUploadFile(file) {
      let _that = this;
      if (file.file.size > 52428800) {
        _that.$message.warning(this.$t('global-message.warn.uploaded-files-cannot-exceed'));
        return;
      }
      let fd = new FormData();
      fd.append('file', file.file);
      fd.append('fileType', '17');
      _that.fileList = [];
      _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: file.file.uid
          });
        } else {
          _that.$message.error(res.message, 1);
        }
      }).catch((error) => {
        _that.$message.error(error.response.data.message, 1);
      });
    },
    /** 文件删除 */
    removeFile(file) {
      let files = this.fileList;
      for (let i = files.length - 1; i >= 0; i--) {
        if (file.uid === files[i].uid) { files.splice(i, 1); }
      }
    },

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

    /** 编辑 保存 */
    save() {
      let _that = this;
      let data = {
        dvpId: _that.$store.getters.dvpId,
        testId: _that.editParam.testId,
        actualStartTime: _that.editParam.actualStartTime,
        actualEndTime: _that.editParam.actualEndTime,
        isComplete: _that.editParam.isFinish
      };
      _that.$api.editVerifyTest(data).then(res => {
        if (!res.flag) {
          _that.$notification.error({message: res.message});
        } else {
          _that.$message.success(res.message, 1);
          _that.offModal();
          _that.initSystemDvpVerifyList();
        }
      });
    },

    /** 编辑 取消 */
    offModal() {
      this.isEdit = false;
      this.isFinishDisabled = true;
      this.editParam.actualEndTime = undefined;
      this.editParam.actualStartTime = undefined;
      this.editParam.isFinish = undefined;
      this.fileList = [];
    },

    /**
     * 非空校验 判断是否为空
     * @returns {boolean}
     */
    isNull(row) {
      let _that = this;
      let message = [];
      // 实际开始时间
      if (!_that.editParam.actualStartTime) {
        message.push(_that.$t('global-constant.please-select') + _that.$t('dvp.full.vehicle.dvp.actual-start-time'));
      }
      // 实际结束时间
      if (!_that.editParam.actualEndTime) {
        message.push(_that.$t('global-constant.please-select') + _that.$t('dvp.full.vehicle.dvp.actual-end-time'));
      }
      // DV完成
      if (!_that.editParam.isFinish) {
        message.push(_that.$t('global-constant.please-select') + _that.$t('base.info.is-DV-completed'));
      }
      // 试验结果
      if (row.testResult === undefined  || row.testResult == null) {
        message.push(_that.$t('global-constant.please-select') + _that.$t('base.info.DV-test-results'));
      }
      if (message && message.length > 0) {
        for (let i = 0; i < message.length; i++) {
          _that.$notification.warning({message: message[i]});
        }
        return false;
      }
      return true;
    },


    /** 编辑弹框表格 保存 */
    saveReform(row) {
      if (!this.isNull(row)) {
        return false;
      }
      // 如果选择了通过，但没有上传实验报告，弹出提醒
      if (row.testResult === 1 && this.fileList.length < 1) {
        this.$notification.warning({message: this.$t('dvp.full.vehicle.dvp.please-upload-test-report')});
        return false;
      }

      let _that = this;
      let data = {
        dvpId: _that.$store.getters.dvpId,
        testId: _that.editParam.testId,
        actualStartTime: _that.editParam.actualStartTime,
        actualEndTime: _that.editParam.actualEndTime,
        verifyResult: row.testResult,
        verifyPlan: row.reformVerifyPlan,
        testReportCode: row.testReportCode,
        isComplete: _that.editParam.isFinish,
        dvpAddFileModels: _that.fileList
      };
      _that.$api.editVerifyTest(data).then(res => {
        if (!res.flag) {
          _that.$notification.error({message: res.message});
        } else {
          _that.$message.success(res.message, 1);
          _that.fileList = [];
          // 编辑弹框 表格初始化
          _that.editTableDataInit(_that.editParam.testId);
          _that.initSystemDvpVerifyList();
        }
      });
    },

    /** 编辑弹框表格 取消 */
    cancel(row) {
      row.testResult = 1;
      row.reformVerifyPlan = '';
      row.testReportCode = '';
    },

    /**
     * 开始时间不能选择当天
     * @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.actualStartTime).endOf('day');
    },

    /** 实际开始时间-选择变化 */
    changeStartTime(data, dateString) {
      if (this.isEdit) {
        this.editParam.actualStartTime = dateString;
        // 当选择的开始时间大于结束时间时，清空结束时间，让用户重新选择结束时间
        if (Date.parse(this.editParam.actualStartTime) > Date.parse(this.editParam.actualEndTime)) {
          this.editParam.actualEndTime = undefined;
        }
      } else {
        this.queryParam.actualStartStartTime = dateString[0];
        this.queryParam.actualStartEndTime = dateString[1];
      }
    },
    /** 实际结束时间-选择变化 */
    changeEndTime(data, dateString) {
      if (this.isEdit) {
        this.editParam.actualEndTime = dateString;
        this.isFinishDisabled = false;
      } else {
        this.queryParam.actualEndStartTime = dateString[0];
        this.queryParam.actualEndEndTime = dateString[1];
      }
    },

    /** 整改结果 */
    handleResult(row,testResult,index) {
      // 选择“不通过”，新增一行空白行
      if (testResult == 0) {
        let addDate = {
          testResult: 1,
          testReportCode: '',
          reformVerifyPlan: '',
          isDisabled: true
        };
        this.editTableData.push(addDate);
      }
    },

    /** 点击分页中数字触发的方法 */
    handleOuterTableChange(pagination) {
      this.pagination.current = pagination.current;
      this.pagination.pageSize = pagination.pageSize;
      this.pagination.pageNo = pagination.current;
      this.initSystemDvpVerifyList();
    }
  }
}
</script>

<style lang="less" scoped>
.top-card-margin {
  margin-top: -12px;
}
.top-card {
  padding: 0 12px;
}
.card-overFlow {
  overflow-x: hidden;
  overflow-y: auto;
}
.exportBtn {
  margin-top: -12px;
  margin-right: -24px;
}
</style>
