<template>
  <div>
    <div class="edit-container" v-if="open">
      <div class="edit-top">
        <div class="left-top">
          <img src="@/assets/images/back.png" @click="cancel()" />
          <div class="left-title"></div>
        </div>
        <div class="center-top">
        </div>
        <div class="right-top">
          <el-button @click="cancel()">{{ $t('form.cancel') }}</el-button>
          <el-button type="primary" v-hasPermi="['business:studyFormPre:tb','business:nonTrialFormPre:tb','business:drugFormPre:tb']" v-if="form.bdzt === 5 && (fromNon || isMatchSubject)" @click="openApprove = true">{{
            $t('page.business.study.studyFormPre.tb') }}</el-button>
        </div>
      </div>
      <div class="edit-content ">
        <div class="content-left">
          <TemplateTable ref="templateTable" :sn="form.templateSn" :templateData="form" fillType="detail" />
          <div class="content-title">
            <div class="line"></div>
            <div class="subtitle"> {{ $t('page.business.study.studyFormPre.qmxx') }}</div>
          </div>
          <div class="pal">
            <el-table :data="qmxxList" v-loading="loadingQmxx">
              <el-table-column :label="$t('page.business.study.studyFormPre.qmr')" align="center" prop="qmrMc"
                width="150px" />
              <el-table-column :label="$t('page.business.study.studyFormPre.qmyy')" align="center"
                :prop="$i18n.locale === 'zh_CN' ? 'qmyy' : 'qmyyEn'" width="150px" />
              <el-table-column :label="$t('page.business.study.studyFormPre.qmsj')" align="center" prop="createTime"
                width="150px" />
              <el-table-column :label="$t('page.business.study.studyFormPre.bzyy')" align="center" prop="remark"
                :show-overflow-tooltip="true" />
            </el-table>
          </div>
          <div class="pal">
            <pagination v-show="totalQmxx > 0" :total="totalQmxx" :page.sync="queryParamsQmxx.pageNum"
              :limit.sync="queryParamsQmxx.pageSize" @pagination="getQmxxList" />
          </div>
        </div>
        <div class="content-right">
          <div class="content-title">
            <div class="line"></div>
            <div class="subtitle"> {{ $t('page.business.study.studyFormPre.jcgj') }}</div>
          </div>
          <JcgjList ref="jcgjList" @handleQuery="getJjcgjList" :showBj="false" :showXg="true" />

          <pagination v-show="jcgjTotal > 0" small layout="prev, pager, next" :total="jcgjTotal"
            @pagination="getJjcgjList" />
        </div>
      </div>
    </div>
    <!-- 填报 -->
    <el-dialog :title="$t('page.business.study.studyFormPre.cjjl')" :visible.sync="openApprove" width="500px"
      append-to-body :close-on-click-modal="false">
      <el-form ref="formApprove" :model="formApprove" :rules="rulesApprove" label-width="120px" v-if="openApprove">
        <div class="sbzdtcma"> <input type="text"> <input type="password"> </div>
        <el-alert :title="$t('page.business.study.studyFormPre.ts')" :closable="false" style="margin-bottom: 10px;" type="success">
        </el-alert>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" :value="formApprove.qmyy" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.remark')" prop="remark">
              <el-input type="textarea" v-model="formApprove.remark" :rows="5" maxlength="500"
                :placeholder="$t('form.placeholderInput')">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.signer')">
              <el-input type="text" v-model="nickName" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.password')" prop="qmrmm">
              <el-input type="password" v-model="formApprove.qmrmm" maxlength="20"
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="approve">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openApprove = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { studySubject_isMatchSubject } from "@/api/business/study/studySubject"
import { studyFormPre_tb, studyFormPre_info, studyFormPre_jcgj, studyFormPre_qmxx } from "@/api/business/study/studyFormPre"
import { mapGetters } from 'vuex'
import JcgjList from "@/views/business/comps/common/JcgjList";
import TemplateTable from '@/views/business/comps/template/TemplateTable';
import { caesarCipher } from "@/utils/index";

export default {
  name: "Xq",
  components: { JcgjList, TemplateTable },
  data() {
    return {
      isMatchSubject: false,
      openApprove: false,
      open: false,
      formApprove: {
        id: null,
        qmyy: this.$t('page.business.study.studyFormPre.cjjl'),
        remark: '',
        qmrmm: '',
      },
      rulesApprove: {
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      qmxxList: [],
      totalQmxx: 0,
      loadingQmxx: true,
      queryParamsQmxx: {
        formId: null,
        pageNum: 1,
        pageSize: 10
      },
      open: false,
      showIndex: 1,
      form: {},
      rules: {
        bdmc: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        templateId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]

      },
      jcgjTotal: 0,
      jcgjList: [],
      queryParamsJcgj: {
        pageNum: 1,
        formId: null,
        pageSize: 10,
      },

      fromNon: false//是否是从非试验+麻精药
    }
  },
  computed: {
    ...mapGetters([
      'nickName',
      'id'
    ]),
  },
  created() {
  },
  methods: {
    getIsMatchSubject() {
      studySubject_isMatchSubject({ studySubjectId: this.form.studySubjectId }).then(response => {
        this.isMatchSubject = response.data
      })
    },
    getJjcgjList(val) {
      studyFormPre_jcgj(_.merge({}, this.queryParamsJcgj, val)).then(response => {
        this.jcgjList = response.rows
        this.jcgjTotal = response.total
        this.$refs.jcgjList.init(this.jcgjList)
      })
    },
    getQmxxList() {
      this.loadingQmxx = true
      studyFormPre_qmxx(this.queryParamsQmxx).then(response => {
        this.qmxxList = response.rows
        this.totalQmxx = response.total
        this.loadingQmxx = false
      })
    },
    cancel() {
      this.$emit('close')
      this.open = false
    },
    reset() {
      this.form = {
        id: null,
        studyId: null,
        bdbh: null,
        bdmc: null,
        bdsm: null,
        templateId: null,
        templateMc: null,
        bdnr: null
      }
      this.resetForm("form")
    },
    show(row, fromNon) {
      this.fromNon = fromNon
      this.reset()
      this.$modal.loading()
      this.formApprove.id = row.id
      this.queryParamsJcgj.formId = row.id
      this.queryParamsQmxx.formId = row.id
      studyFormPre_info({ id: row.id }).then(response => {
        this.form = response.data
        this.open = true
        this.getQmxxList()
        this.getJjcgjList()
        this.getIsMatchSubject()
        this.open = true
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    approve() {
      this.$refs["formApprove"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormPre_tb(this.formApprove).then(response => {
            let params = caesarCipher(JSON.stringify({
              url: this.$route.fullPath,
              studyId: this.form.studyId,
              formId: response.data.id,
              fromYt: true
            }))
            if (this.$route.fullPath.indexOf('/study/') > -1) {
              this.$tab.openPage(response.data.bdmc, '/study/formFillBj/' + params)
            } else if (this.$route.fullPath.indexOf('/nonTrial/') > -1) {
              this.$tab.openPage(response.data.bdmc, '/nonTrial/formFillBj/' + params)
            } else {
              this.$tab.openPage(response.data.bdmc, '/drug/formFillBj/' + params)
            }
            this.$emit('close')
            this.open = false
            this.openApprove = false
            this.$modal.closeLoading()
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    }
  }
}
</script>
