<!-- 细胞换液/观察记录表 -->
<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"
          />

          <LineLabel label="template.dl.dl015.sysj" />
          <CustomTable
            :columns="sysjColumns"
            :ref="refConf.sysj"
            :showOperation="fillType === 'actFill'"
            :showAddRow="fillType === 'actFill'"
            :formData="formData"
            :prefixKey="`sysjTable`"
          >
            <template slot="operation" slot-scope="{ row, rowIndex, columns }">
              <TableOpertaionDelete
                :row="row"
                :rowIndex="rowIndex"
                :columns="columns"
                @deleteRow="() => deleteTableRow(rowIndex, 'sysjTableRef')"
              ></TableOpertaionDelete>
            </template>
          </CustomTable>

          <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.dl015.xbxx" />
          <BaseInfoFormPackage
            :ref="refConf.xbInfo"
            :formConfig="xbInfoFormConfig"
            :formData="formData"
          />

          <!-- 预留区域 - 换液情况 -->
          <LineLabel label="template.dl.dl015.hyqk" />

          <LineLabel label="template.dl.dl015.czbz" />
          <Step :ref="refConf.step" :formData="formData.stepData" />

          <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'
import moment from 'moment'

const refConf = {
  base: 'baseInfoRef',
  sysj: 'sysjTableRef',
  yqsy: 'yqsyTableRef',
  xbInfo: 'xbInfoRef',
  step: 'stepRef',
  remark: 'remarkRef'
}
const refNames = Object.values(refConf)

export default {
  name: 'DL015',
  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
            }
          }
        },
        {
          type: 'conditionItem',
          label: 'template.dl.dl015.sydd',
          config: {
            sydd: {
              span: 1,
              label: 'template.dl.dl015.sydd',
              type: 'select',
              options: this.getDictOptions('business_sydd'),
              fillType: 'actFill',
              otherCode: 'syddOther'
            }
          }
        },
        {
          type: 'cellItem',
          label: 'template.dl.dl015.czsj',
          config: {
            kssj: {
              label: 'template.dl.dl015.kssj',
              type: 'input',
              disabled: true
            },
            jssj: {
              label: 'template.dl.dl015.jssj',
              type: 'input',
              disabled: true
            }
          }
        }
      ]
    },
    // 试验试剂信息
    sysjColumns() {
      return [
        {
          label: 'template.dl.dl015.sjmc',
          prop: 'sjmc',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.bh',
          prop: 'bh',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.ph',
          prop: 'ph',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.ndhlcd',
          prop: 'ndhlcd',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.ly',
          prop: 'ly',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.sxr',
          prop: 'sxr',
          bodyType: 'input',
          bodyFillType: 'actFill'
        }
      ]
    },
    // 仪器使用信息
    yqsyColumns() {
      return [
        {
          label: 'template.dl.dl015.yqmc',
          prop: 'yqmc',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.yqxh',
          prop: 'yqxh',
          bodyType: 'input',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.yqbh',
          prop: 'yqbh',
          bodyType: 'yq',
          bodyFillType: 'actFill'
        },
        {
          label: 'template.dl.dl015.xccsjzjdrq',
          prop: 'xccsjzjdrq',
          bodyType: 'input',
          bodyDisabled: true,
          bodyFillType: 'actFill',
          disabled: true
        }
      ]
    },
    // 细胞信息表单配置
    xbInfoFormConfig() {
      return [
        {
          type: 'cellItem',
          config: {
            qxbd: {
              label: 'template.dl.dl015.qxbd',
              type: 'qxbd',
              fillType: 'actFill',
              filledCodes: ['bdmc', 'bdbh']
            },
            bltj: {
              label: 'template.dl.dl015.bltj',
              type: 'select',
              options: this.getDictOptions('business_bltj'),
              fillType: 'preFill',
              otherCode: 'bltjOther'
            }
          }
        }
      ]
    }
  },
  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 sysjStepResource = this.$refs.sysjTableRef.getStepResource()
      const yqsyStepResource = this.$refs.yqsyTableRef.getStepResource()
      const stepResource = this.$refs.stepRef.getStepResource()
      // 使用的试剂、仪器
      this.resourceTmp = [
        ...(sysjStepResource.sjResource || []),
        ...(yqsyStepResource.sjResource || []),
        ...(stepResource.sjResource || [])
      ]
      this.yqResourceTmp = [
        ...(yqsyStepResource.yqResource || []),
        ...(stepResource.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,
          xccsjzjdrq: 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">
.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>
