<!-- Ames试验（掺入法）观察结果记录表 -->
<template>
  <div>
    <div class="detail-container">
      <div class="detail-title">
        <img src="@/assets/images/detail-title.png" />{{ formData.bdmc
        }}<img src="@/assets/images/detail-title.png" />
      </div>
      <div class="detail-content">
        <div class="content">
          <BaseInfoFormPackage
            fieldItemLabel="template.common.baseInfo"
            label="template.common.baseInfo"
            :ref="refConf.base"
            :formConfig="baseInfoFormConfig"
            :formData="formData"
          />

          <BaseInfoFormPackage
            :ref="refConf.cltj"
            :formConfig="cltjFormConfig"
            :formData="formData"
          />

          <BaseInfoFormPackage
            :ref="refConf.czsj"
            :formConfig="czsjFormConfig"
            :formData="formData"
          />

          <LineLabel label="template.dl.dl007.yqsyxx" />
          <CustomTable
            :columns="yqsyColumns"
            :ref="refConf.yqsy"
            @onRegentSubmit="(e) => onRegentSubmit(e)"
            :showOperation="fillType === 'actFill'"
            :showAddRow="fillType === 'actFill'"
            :formData="formData"
            :prefixKey="`yqsyTable`"
          >
            <template slot="operation" slot-scope="{ row, rowIndex, columns }">
              <TableOpertaionDelete
                :row="row"
                :rowIndex="rowIndex"
                :columns="columns"
                @deleteRow="() => deleteTableRow(rowIndex, 'yqsyTableRef')"
              ></TableOpertaionDelete>
            </template>
          </CustomTable>

          <!-- 结果信息 -->
          <LineLabel label="template.dl.dl009.jgxx" />
          <BaseInfoFormPackage
            :ref="refConf.jgxxQxbd"
            :formConfig="jgxxQxbdFormConfig"
            :formData="formData"
          />
          <div class="jgxx-description">
            <p>{{ $t('template.dl.dl009.czqkDesc') }}</p>
            <p>{{ $t('template.dl.dl009.bjjtDesc') }}</p>
            <p>{{ $t('template.dl.dl009.jlsDesc') }}</p>
          </div>
          <CustomTable
            :columns="jgxxColumns"
            :ref="refConf.jgxx"
            :showOperation="false"
            :showAddRow="false"
            :formData="formData"
            :prefixKey="`jgxxTable`"
          >
          </CustomTable>

          <BaseInfoFormPackage
            fieldItemLabel="template.dl.dl007.bz"
            label="template.dl.dl007.bz"
            :ref="refConf.remark"
            :formConfig="remarkConfig"
            :formData="formData"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BaseInfoFormPackage from '@/components/Template/BaseInfoFormPackage'
import TableOpertaionDelete from '@/components/Template/operation/TableOpertaionDelete.vue'
import CustomTable from '@/components/Template/CustomTable.vue'
import LineLabel from '@/components/Template/LineLabel'
import TableList from '@/components/Template/Table'
import Step from '@/components/Template/Step'
import templateMixin from '../../mixins/templateMixin'

const refConf = {
  base: 'baseInfoRef',
  cltj: 'cltjRef',
  czsj: 'czsjRef',
  yqsy: 'yqsyTableRef',
  jgxxQxbd: 'jgxxQxbdRef',
  jgxx: 'jgxxTableRef',
  remark: 'remarkRef'
}
const refNames = Object.values(refConf)

export default {
  name: 'DL009',
  components: {
    BaseInfoFormPackage,
    LineLabel,
    TableList,
    Step,
    CustomTable,
    TableOpertaionDelete
  },
  mixins: [templateMixin],
  props: {
    fillType: {
      type: String,
      default: 'preFill'
    }
  },
  computed: {
    // 备注表单配置
    remarkConfig() {
      return [
        {
          type: 'cellItem',
          config: {
            remark: {
              label: '',
              type: 'textarea',
              fillType: 'actFill',
              span: 1,
              placeholder: 'template.common.remarkPlaceholder',
              maxlength: 1000,
              rows: 5
            }
          }
        }
      ]
    },
    // 试验基本信息表单配置
    baseInfoFormConfig() {
      return [
        {
          type: 'cardItem',
          config: {
            studyMc: {
              label: 'template.common.testName',
              type: 'input',
              disabled: true
            },
            studySn: {
              label: 'template.common.testNumber',
              type: 'input',
              disabled: true
            }
          }
        }
      ]
    },
    // 处理条件表单配置
    cltjFormConfig() {
      return [
        {
          type: 'conditionItem',
          config: {
            cltj: {
              span: 1,
              label: 'template.dl.dl009.cltj',
              type: 'select',
              options: this.getDictOptions('business_dl_amescltj'),
              fillType: 'actFill',
              otherCode: 'cltjOther'
            }
          }
        },
        {
          type: 'conditionItem',
          config: {
            sydd: {
              span: 1,
              label: 'template.dl.dl009.sydd',
              type: 'select',
              options: this.getDictOptions('business_sydd'),
              fillType: 'actFill',
              otherCode: 'syddOther'
            }
          }
        }
      ]
    },
    // 操作时间表单配置
    czsjFormConfig() {
      return [
        {
          type: 'cellItem',
          config: {
            kssj: {
              label: 'template.dl.dl009.kssj',
              type: 'input',
              disabled: true
            },
            jssj: {
              label: 'template.dl.dl009.jssj',
              type: 'input',
              fillType: 'actFill',
              maxlength: 200
            }
          }
        }
      ]
    },
    // 仪器使用信息
    yqsyColumns() {
      return [
        {
          label: 'template.dl.dl007.yqbh',
          prop: 'yqbh',
          bodyType: 'yq',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl007.yqmc',
          prop: 'yqmc',
          bodyType: 'input',
          bodyDisabled: true,
          bodyFillType: 'actFill',
          disabled: true
        },
        {
          label: 'template.dl.dl007.yqxh',
          prop: 'yqxh',
          bodyType: 'input',
          bodyDisabled: true,
          bodyFillType: 'actFill',
          disabled: true
        },
        {
          label: 'template.dl.dl007.xccsjzjdrq',
          prop: 'jzrq',
          bodyType: 'input',
          bodyDisabled: true,
          bodyFillType: 'actFill',
          disabled: true
        }
      ]
    },
    // 结果信息前序表单配置
    jgxxQxbdFormConfig() {
      return [
        {
          type: 'cellItem',
          config: {
            jgxxQxbd: {
              label: 'template.dl.dl009.qxbd',
              type: 'qxbd',
              fillType: 'actFill',
              filledCodes: ['bdmc', 'bdbh']
            }
          }
        }
      ]
    },
    // 结果信息列表
    jgxxColumns() {
      return [
        {
          label: 'template.dl.dl009.jlzb',
          prop: 'jlzb',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl009.jzbh',
          prop: 'jzbh',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl009.czqk',
          prop: 'czqk',
          bodyType: 'select',
          bodyFillType: 'actFill',
          bodyOptions: this.getDictOptions('business_czqk')
        },
        {
          label: 'template.dl.dl009.bjjt',
          prop: 'bjjt',
          bodyType: 'select',
          bodyFillType: 'actFill',
          bodyOptions: this.getDictOptions('business_bjjt')
        },
        {
          label: 'template.dl.dl009.jls',
          prop: 'jls',
          bodyType: 'input',
          bodyFillType: 'actFill'
        }
      ]
    }
  },
  data() {
    return {
      formData: {},
      refConf
    }
  },
  methods: {
    // 删除表格行
    deleteTableRow(rowIndex, refName) {
      this.$refs[refName].deleteRow(rowIndex)
    },
    // 获取已填写的表单数据
    getFilledFormData() {
      return this.getFilledFormDataByRefs(refNames)
    },
    // 获取填写完成的表单数据
    async getFormData() {
      return await this.validFormFields(refNames)
    },
    // 只做校验
    async validFields() {
      return await this.validFormFields(refNames)
    },
    getResource() {
      const yqsyStepResource = this.$refs.yqsyTableRef.getStepResource()
      const jgxxStepResource = this.$refs.jgxxTableRef.getStepResource()
      // 使用的试剂、仪器
      this.resourceTmp = [
        ...(yqsyStepResource.sjResource || []),
        ...(jgxxStepResource.sjResource || [])
      ]
      this.yqResourceTmp = [
        ...(yqsyStepResource.yqResource || []),
        ...(jgxxStepResource.yqResource || [])
      ]
      return this.resourceTmp
    },
    onRegentSubmit(e) {
      const { selectInfo, key, rowIndex } = e
      const { row } = selectInfo

      if (key === 'yqbh') {
        const params = {
          yqmc: row.mc,
          yqxh: row.xh,
          jzrq: row.jzrq
        }
        this.$refs.yqsyTableRef.updateDataSourceByRowIndex(rowIndex, params)
      }
    },
    // 保存
    async onSave() {
      const formData = this.getStepResource()
      console.log(formData, 'formData')
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.jgxx-description {
  margin: 15px 0;
  padding: 12px 15px;
  background-color: #f5f7fa;
  border-left: 4px solid #409eff;
  font-size: 13px;
  line-height: 1.8;
  color: #606266;

  p {
    margin: 5px 0;
  }
}

.mt-20 {
  margin-top: 20px;
}

.print-btn {
  margin-bottom: 20px;
}
.config-header-end {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 15px;
  font-weight: bold;
  font-size: 16px;
  color: #303133;
}
</style>
