<template>
  <div>
    <div class="edit-container">
      <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="showIndex == 1 ? 'el-step__icon is-text is-finish' : 'el-step__icon is-text is-info'">
              <div class="el-step__icon-inner">1</div>
            </div>
            {{ $t('page.business.study.studyFormFill.jcsz') }}
          </div>
          <div class="line"></div>
          <div>
            <div :class="showIndex == 2 ? 'el-step__icon is-text is-finish' : 'el-step__icon is-text is-info'">
              <div class="el-step__icon-inner">2</div>
            </div>
            {{ $t('page.business.study.studyFormFill.bdtb') }}
          </div>
        </div>
        <div class="right-top">
          <el-button @click="cancel()">{{ $t('form.cancel') }}</el-button>
          <template v-if="showIndex == 1">
            <el-button @click="save">{{ $t('page.business.study.studyFormFill.zc') }}</el-button>
            <el-button type="primary" @click="next">{{ $t('page.business.study.studyFormFill.next') }}</el-button>
          </template>
          <template v-else>
            <el-button @click="showDy" v-if="form.templateShowDy == 10">{{
              $t('template.common.bqdy')
            }}</el-button>
            <el-button @click="showYjcc" v-if="form.templateShowYjcc == 10">{{
              $t('page.business.study.studyFormFill.yjcc')
            }}</el-button>
            <el-button @click="showSave">{{ $t('page.business.study.studyFormFill.save') }}</el-button>
            <el-button type="primary" v-if="form.blxjshzt != 10 && form.showBlxjsh == 10" @click="showTjsh">{{
              $t('page.business.study.studyFormFill.tjsh')
            }}</el-button>
            <el-button type="primary" v-else @click="showApprove">{{ $t('page.business.study.studyFormFill.submit')
            }}</el-button>
            <el-button type="primary" @click="pre">{{ $t('page.business.study.studyFormFill.pre') }}</el-button>
          </template>
        </div>
      </div>
      <div class="edit-content">
        <div class="content" style="width:60%" v-show="showIndex == 1">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <el-col :span="24">
                <el-form-item :label="bdmbTitle" prop="templateId">
                  <SelectTemplate v-model="form.templateId" :disabled="form.id && form.id !== ''"
                    :name="form.templateMc" :studyType="studyType" :studyFormType="studyFormType"
                    @change="selectTemplateChange" :needPre="1" :selectedDeptId="selectedDeptId" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item :label="$t('page.business.form.bdmc')" prop="bdmc">
                  <el-input type="text" v-model="form.bdmc" maxlength="50" :placeholder="$t('form.placeholderInput')" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item :label="$t('page.business.study.studyFormFill.sfbl')" prop="sfbl">
                  <el-select v-model="form.sfbl" :placeholder="$t('form.placeholderSelect')" style="width: 100%;"
                    @change="validForm">
                    <el-option :label="$t('page.business.study.studyFormFill.yes')" value="是" />
                    <el-option :label="$t('page.business.study.studyFormFill.no')" value="否" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item :label="$t('page.business.study.studyFormFill.bdsm')" prop="bdsm">
                  <el-input type="textarea" v-model="form.bdsm" :rows="5" maxlength="500"
                    :placeholder="$t('form.placeholderInput')">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="content" v-if="showIndex == 2 && templateData.templateId">
          <TemplateTable emitName="onFillCallback" @onFillCallback="onFillCallback" ref="templateTable"
            :sn="templateData.templateSn" :templateData="templateData" fillType="actFill" />
        </div>
      </div>
    </div>

    <el-dialog :title="$t('page.business.study.studyFormFill.tjjl')" :visible.sync="openApprove" width="1200px"
      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" show-password> </div>
        <el-row v-if="form.sftb == 1 && form.onlySubmit == 1">
          <el-col :psna="24">
            <el-form-item>
              <div><el-checkbox v-model="formApprove.sfcz" @change="sfczChange">{{
                $t('page.business.study.studyFormFill.cz') }}</el-checkbox>
                <el-button type="primary" v-if="formApprove.sfcz" @click="addCz" style="margin-left: 10px;">{{
                  $t('page.business.study.studyFormFill.tjcz') }}</el-button>
              </div>
              <template v-if="formApprove.sfcz">
                <div v-if="czlist.length > 0">
                  <el-card class="box-card" v-for="(citem, cindex) in czlist" :key="'cz' + cindex"
                    style="margin-top: 10px;">
                    <div slot="header" class="clearfix">
                      <div style="display: flex;justify-content: space-between;">
                        <div style="display: flex;">
                          <div> 处置方式：</div>
                          <div style="display: flex;">
                            <BusinessSelect v-model="citem.czfs" @change="czfsChange(cindex)" dictType="business_czfs"
                              style="width: 200px;">
                            </BusinessSelect>
                            <el-input type="text" v-if="citem.czfs == '其他'" v-model="citem.czfsqt" maxlength="50"
                              :placeholder="$t('form.placeholderInput')" style="width: 200px;" />
                          </div>
                        </div>
                        <div>
                          <el-button type="danger" icon="el-icon-delete" circle @click="delcz(cindex)"></el-button>
                        </div>
                      </div>
                    </div>
                    <el-checkbox-group v-model="citem.list">
                      <el-checkbox :label="item.bh" v-for="item in resource" :key="'icz' + item.bh"
                        :disabled="getDisabledCz(item.bh, cindex)"></el-checkbox>
                    </el-checkbox-group>
                  </el-card>
                </div>
              </template>
            </el-form-item>
          </el-col>
          <el-col :psna="24">
            <el-form-item>
              <div><el-checkbox v-model="formApprove.sfcc" @change="sfccChange">{{
                $t('page.business.study.studyFormFill.cc') }}</el-checkbox>
                <el-button type="primary" v-if="formApprove.sfcc" @click="addCc" style="margin-left: 10px;">{{
                  $t('page.business.study.studyFormFill.tjcc') }}</el-button>
              </div>
              <template v-if="formApprove.sfcc">
                <div v-if="cclist.length > 0">
                  <el-card class="box-card" v-for="(citem, cindex) in cclist" :key="'cz' + cindex"
                    style="margin-top: 10px;">
                    <div slot="header" class="clearfix">
                      <div style="display: flex;justify-content: space-between;">
                        <div style="display: flex;">
                          <div style="display: flex;">
                            <div> 存储位置：</div>
                            <div>
                              <el-select v-model="citem.ccwz" :placeholder="$t('form.placeholderSelect')"
                                @change="ccwzChange(cindex)" filterable>
                                <el-option v-for="item in ccwzlist" :key="item.id" :label="item.bh" :value="item.id" />
                              </el-select>
                            </div>
                          </div>
                          <div style="display: flex; margin-left: 10px;">
                            <div> 存储条件：</div>
                            <div style="display: flex;">
                              <BusinessSelect v-model="citem.cctj" @change="cctjChange(cindex)" dictType="business_cctj"
                                :multiple="true" style="width: 200px;">
                              </BusinessSelect>
                              <el-input type="text" v-if="citem.cctj == '其他'" v-model="citem.cctjqt" maxlength="50"
                                :placeholder="$t('form.placeholderInput')" style="width: 200px;" />
                            </div>
                          </div>
                        </div>
                        <div>
                          <el-button type="danger" icon="el-icon-delete" circle @click="delcc(cindex)"></el-button>
                        </div>
                      </div>
                    </div>
                    <el-checkbox-group v-model="citem.list">
                      <el-checkbox :label="item.bh" v-for="item in resource" :key="'icz' + item.bh"
                        :disabled="getDisabledCc(item.bh, cindex)"></el-checkbox>
                    </el-checkbox-group>
                  </el-card>
                </div>
              </template>
            </el-form-item>
          </el-col>
          <el-col :psna="24">
            <el-form-item>
              <div>{{ $t('page.business.study.studyFormFill.yltj') }}</div>
              <div style=" display: flex;  flex-wrap: wrap;  gap: 16px;">
                <div v-for="(item, index) in resource" :key="'r' + index" style=" flex: 0 0 calc(50% - 11px);">
                  <div style="display: flex;">
                    <div> {{ item.bh }}</div>
                    <div>
                      <el-input type="number" v-model="item.syl" maxlength="50"
                        style="width:100px; margin:0px 0px  0px 5px;" />
                    </div>
                    <div>
                      <div style="width: 70px;">
                        <BusinessSelect v-model="item.syldw" :showMax="false" dictType="business_tjdw">
                        </BusinessSelect>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <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">
              <div class="sbzdtcma"> <input type="text"></div>
              <el-input type="password" @contextmenu.native.prevent @copy.native.prevent @cut.native.prevent
                show-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="tj">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openApprove = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('page.business.study.studyFormFill.yjcc')" :visible.sync="openYjcc" width="1200px"
      append-to-body :close-on-click-modal="false">
      <el-form ref="formYjcc" :model="formYjcc" :rules="rulesYjcc" label-width="120px" v-if="openYjcc">
        <div class="sbzdtcma"> <input type="text"> <input type="password" show-password> </div>
        <el-row>
          <el-col :psna="24">
            <el-form-item>
              <div>
                <el-button type="primary" @click="addCcYj" style="margin-left: 10px;">{{
                  $t('page.business.study.studyFormFill.tjcc') }}</el-button>
              </div>
              <template>
                <div v-if="cclistYj.length > 0">
                  <el-card class="box-card" v-for="(citem, cindex) in cclistYj" :key="'cz' + cindex"
                    style="margin-top: 10px;">
                    <div slot="header" class="clearfix">
                      <div style="display: flex;justify-content: space-between;">
                        <div style="display: flex;">
                          <div style="display: flex;">
                            <div> 存储位置：</div>
                            <div>
                              <el-select v-model="citem.ccwz" :placeholder="$t('form.placeholderSelect')"
                                @change="ccwzYjChange(cindex)" filterable>
                                <el-option v-for="item in ccwzlist" :key="item.id" :label="item.bh" :value="item.id" />
                              </el-select>
                            </div>
                          </div>
                          <div style="display: flex; margin-left: 10px;">
                            <div> 存储条件：</div>
                            <div style="display: flex;">
                              <BusinessSelect v-model="citem.cctj" @change="cctjYjChange(cindex)"
                                dictType="business_cctj" :multiple="true" style="width: 200px;">
                              </BusinessSelect>
                              <el-input type="text" v-if="citem.cctj == '其他'" v-model="citem.cctjqt" maxlength="50"
                                :placeholder="$t('form.placeholderInput')" style="width: 200px;" />
                            </div>
                          </div>
                        </div>
                        <div>
                          <el-button type="danger" icon="el-icon-delete" v-if="cclistYj.length > 1" circle
                            @click="delccYj(cindex)"></el-button>
                        </div>
                      </div>
                    </div>
                    <el-checkbox-group v-model="citem.list">
                      <el-checkbox :label="item.bh" v-for="item in resourceYj" :key="'icz' + item.bh"
                        :disabled="getDisabledCcYj(item.bh, cindex)"></el-checkbox>
                    </el-checkbox-group>
                  </el-card>
                </div>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" :value="formYjcc.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="formYjcc.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">
              <div class="sbzdtcma"> <input type="text"></div>
              <el-input type="password" @contextmenu.native.prevent @copy.native.prevent @cut.native.prevent
                show-password v-model="formYjcc.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="yjcc">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openYjcc = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('page.business.study.studyFormFill.bcjl')" :visible.sync="openSave" width="600px"
      append-to-body :close-on-click-modal="false">
      <el-form ref="formSave" :model="formSave" :rules="rulesSave" label-width="120px" v-if="openSave">
        <div class="sbzdtcma"> <input type="text"> <input type="password" show-password> </div>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" :value="formSave.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="formSave.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">
              <div class="sbzdtcma"> <input type="text"></div>
              <el-input type="password" @contextmenu.native.prevent @copy.native.prevent @cut.native.prevent
                show-password v-model="formSave.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="saveNext">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openSave = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('page.business.study.studyFormFill.tjsh')" :visible.sync="openTjsh" width="600px"
      append-to-body :close-on-click-modal="false">
      <el-form ref="formTjsh" :model="formTjsh" :rules="rulesTjsh" label-width="120px" v-if="openTjsh">
        <div class="sbzdtcma"> <input type="text"> <input type="password" show-password> </div>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" :value="formTjsh.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="formTjsh.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">
              <div class="sbzdtcma"> <input type="text"></div>
              <el-input type="password" @contextmenu.native.prevent @copy.native.prevent @cut.native.prevent
                show-password v-model="formTjsh.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="saveTjsh">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openTjsh = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('template.common.bqdy')" :visible.sync="openBqdy" width="1200px" append-to-body
      :close-on-click-modal="false">
      <el-form ref="formBqdy" :model="formBqdy" :rules="rulesBqdy" label-width="120px" v-if="openBqdy">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('template.common.dayji')" prop="dyj">
              <el-select v-model="formBqdy.dyj" placeholder="请选择">
                <el-option label="打印机" value="打印机">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('template.common.label')" prop="printList">
              <!-- {{ JSON.stringify(formBqdy) }} -->
              <el-checkbox-group v-model="printCheck" @change="printCheckChange">
                <el-checkbox :label="item.bh" v-for="item in resourcePrint" :key="'print' + item.bh"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="printbq">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openBqdy = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listData } from "@/api/system/dict/data"
import { studyFormFill_bc, studyFormFill_yjccFilter, studyFormFill_yjcc, studyFormFill_tjblxjsh, studyFormFill_bcOnly, studyFormFill_updateBdnr, studyFormFill_tj, studyFormFill_info, studyFormFill_updateFhyjjl } from "@/api/business/study/studyFormFill"
import { study_info } from "@/api/business/study/study"
import { mapGetters } from 'vuex'
import SelectTemplate from "@/views/business/comps/select/SelectTemplate";
import SelectDeptUser from '@/views/business/comps/select/SelectDeptUser';
import TemplateTable from '@/views/business/comps/template/TemplateTable';
import BusinessSelect from '@/views/business/comps/select/BusinessSelect';
import { deepClone, caesarDecipher } from "@/utils/index";
import { EventBus } from "@/utils/eventBus";

export default {
  name: "Bj",
  components: { SelectTemplate, SelectDeptUser, TemplateTable, BusinessSelect },
  data() {
    return {
      showIndex: 1,
      printCheck: [],
      form: {},
      rules: {
        bdmc: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        templateId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        sfbl: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],

      },
      czlist: [],
      cclist: [],
      cclistYj: [],
      openSave: false,
      openTjsh: false,
      openApprove: false,
      openBqdy: false,
      openYjcc: false,
      formSave: {
        qmyy: this.$t('page.business.study.studyFormFill.txbbc'),
        remark: '',
        qmrmm: '',
      },
      formBqdy: {
        dyj: null,
        printList: null
      },
      formApprove: {
        id: null,
        qmyy: this.$t('page.business.study.studyFormFill.txbtjjl'),
        remark: '',
        bdnr: '',
        resource: '',
        yqResource: '',
        jcbList: '',
        xbxj: '',
        qmrmm: '',
        sfcz: false,
        czfs: '',
        czfsqt: '',
        czids: '',
        sfcc: false,
        cctjqt: '',
        cctj: '',
        ccwz: '',
        ccids: '',
      },
      formYjcc: {
        id: null,
        qmyy: this.$t('page.business.study.studyFormFill.yjcc'),
        remark: '',
        resource: '',
        qmrmm: '',
        sfcz: false,
        czfs: '',
        czfsqt: '',
        czids: '',
        sfcc: false,
        cctj: '',
        cctjqt: '',
        ccwz: '',
        ccids: '',
      },
      formTjsh: {
        id: null,
        qmyy: this.$t('page.business.study.studyFormFill.txbtjjl'),
        remark: '',
        bdnr: '',
        qmrmm: '',
      },
      resourceYj: [],
      resource: [],
      yqResource: [],
      jcbList: [],
      xbxj: [],
      bdmbTitle: this.$t('page.business.form.bdmb'),
      rulesBqdy: {
        dyj: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        printList: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      rulesApprove: {
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      rulesYjcc: {
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      rulesTjsh: {
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      rulesSave: {
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      templateData: {},
      studyType: null,
      studyFormType: null,
      ccwzlist: [],
      selectedDeptId: null,
      resourcePrint: []
    }
  },
  computed: {
    ...mapGetters([
      'nickName', 'name'
    ]),
  },
  created() {
    this.getInfo()
    this.getCcwz()
  },
  methods: {
    printCheckChange() {
      if (this.printCheck && this.printCheck.length > 0) {
        this.formBqdy.printList = this.printCheck.join(',')
      } else {
        this.formBqdy.printList = null
      }
    },
    validForm() {
      this.$refs["form"].validate(valid => {
        if (valid) { }
      })
    },
    getCcwz() {
      listData({
        pageNum: 1,
        pageSize: 999999,
        dictType: 'business_ccwz',
        status: 0
      }).then(response => {
        this.ccwzlist = response.rows
      })
    },
    czfsChange(cindex) {
      let that = this
      that.czlist[cindex].czfsqt = ''
    },
    cctjChange(cindex) {
      let that = this
      that.cclist[cindex].cctjqt = ''
    },
    cctjYjChange(cindex) {
      let that = this
      that.cclistYj[cindex].cctjqt = ''
    },
    ccwzChange(cindex) {
      let that = this
      let _index = _.findIndex(this.ccwzlist, function (item) {
        return item.id == that.cclist[cindex].ccwz
      })
      that.cclist[cindex].cctj = that.ccwzlist[_index].wc
    },
    ccwzYjChange(cindex) {
      let that = this
      let _index = _.findIndex(this.ccwzlist, function (item) {
        return item.id == that.cclistYj[cindex].ccwz
      })
      that.cclistYj[cindex].cctj = that.ccwzlist[_index].wc
    },
    onFillCallback(data) {
      console.log("data:" + JSON.stringify(data))
      console.log("formdata:" + JSON.stringify(this.$refs.templateTable.getFilledFormData()))
      if (data.type == 'fieldChanged') {
        studyFormFill_updateBdnr(
          {
            id: this.form.id,
            bdnr: JSON.stringify(this.$refs.templateTable.getFilledFormData()),
            zdxgjl: data.resourceList ? JSON.stringify(data.resourceList) : '',
            filedValue: data.newRecord ? JSON.stringify(data.newRecord) : '',
            submittedCodes: data.submittedCodes ? JSON.stringify(data.submittedCodes) : ''
          }
        ).then(response => {
        })
      } else if (data.type == 'reply') {
        studyFormFill_updateFhyjjl(
          {
            id: this.form.id,
            reply: JSON.stringify(data.newRecord),
            fhyjjl: JSON.stringify(data.resourceList)
          }
        ).then(response => {
        })
      }
    },
    delcc(index) {
      this.cclist.splice(index, 1)
    },
    delccYj(index) {
      this.cclistYj.splice(index, 1)
    },
    delcz(index) {
      this.czlist.splice(index, 1)
    },
    addCz() {
      this.czlist.push({
        list: [],
        czfs: '',
        czfsqt: ''
      })
    },
    addCc() {
      this.cclist.push({
        list: [],
        cctj: '',
        ccwz: '',
        cctjqt: '',
      })
    },
    addCcYj() {
      this.cclistYj.push({
        list: [],
        cctj: '',
        ccwz: '',
        cctjqt: ''
      })
    },
    getInfo() {
      let key = this.$route.params.key
      if (key && key != undefined && key != '') {
        let params = null
        try {
          params = JSON.parse(caesarDecipher(this.$route.params.key))
          this.studyType = params.studyType || null
          this.studyFormType = params.studyFormType || 5
          this.selectedDeptId = params.selectedDeptId || null
        } catch (e) {
          console.log('参数错误')
        }
        if (params) {
          this.reset()
          if (params.formId && params.formId != '') {

            this.$modal.loading()
            studyFormFill_info({ id: params.formId }).then(response => {
              this.form = response.data
              this.form.qmyy = this.$t('page.business.study.studyFormFill.zztjyzbd')
              this.templateData = deepClone(this.form)
              const obj = Object.assign({}, this.$route, { title: this.form.bdmc })
              this.$tab.updatePage(obj);
              if (params.fromYt) {
                this.showIndex = 1
                this.bdmbTitle = this.$t('page.business.study.studyFormFill.ytbd')
              } else {
                this.showIndex = 2
              }
              this.saveSimpleLog({ jcmc: '填报表单编辑', jcmcEn: 'Record Edit', name: this.form.bdmc + '（' + this.form.bdbh + '）', nameEn: this.form.bdmc + '（' + this.form.bdbh + '）' })
              this.$modal.closeLoading()
            })
          } else {
            this.showIndex = 1
            this.$modal.loading()
            study_info({ id: params.studyId }).then(response => {
              this.form = _.merge({}, this.form, { studySubjectId: params.studySubjectId, studyId: params.studyId, resourceStudy: response.data.resource, sftb: 1, studyMc: response.data.name, studySn: response.data.sn })
              this.templateData = deepClone(this.form)
              const obj = Object.assign({}, this.$route, { title: this.$t('page.business.study.studyFormFill.xzbd') })
              this.$tab.updatePage(obj);
              let name = this.studyType == 1 ? '试验' : (this.studyType == 5 ? '非试验表单' : '麻精药表单')
              let nameEn = this.studyType == 1 ? 'Study' : (this.studyType == 5 ? 'Non-study' : 'ontrolled Drug')
              this.saveSimpleLog({ jcmc: '填报表单新增', jcmcEn: 'Record Create', name: name, nameEn: nameEn })
              this.$modal.closeLoading()
            })
          }
        } else {
          this.$tab.closeOpenPage('/404')
        }
      }
    },
    getDisabledCz(bh, idx) {
      let mark = false
      for (var i = 0; i < this.czlist.length; i++) {
        if (idx != i) {
          for (var j = 0; j < this.czlist[i].list.length; j++) {
            if (this.czlist[i].list[j] == bh) {
              mark = true;
              break
            }
          }
        }
      }
      return mark
    },
    getDisabledCc(bh, idx) {
      let mark = false
      for (var i = 0; i < this.cclist.length; i++) {
        if (idx != i) {
          for (var j = 0; j < this.cclist[i].list.length; j++) {
            if (this.cclist[i].list[j] == bh) {
              mark = true;
              break
            }
          }
        }
      }
      return mark
    },
    getDisabledCcYj(bh, idx) {
      let mark = false
      for (var i = 0; i < this.cclistYj.length; i++) {
        if (idx != i) {
          for (var j = 0; j < this.cclistYj[i].list.length; j++) {
            if (this.cclistYj[i].list[j] == bh) {
              mark = true;
              break
            }
          }
        }
      }
      return mark
    },
    sfczChange() {
      if (!this.formApprove.sfcz) {
        this.czlist = []
      }
    },
    sfccChange() {
      if (!this.formApprove.sfcc) {
        this.cclist = []
      }
    },
    async showApprove() {
      let that = this
      let content = await that.$refs.templateTable.getFormData()
      if (content) {
        that.resetApprove()
        that.formApprove.id = that.form.id
        that.formApprove.bdnr = JSON.stringify(content)
        that.resource = that.$refs.templateTable.getResource()
        that.yqResource = that.$refs.templateTable.getYqResource()
        that.jcbList = that.$refs.templateTable.getJcbList()
        that.xbxj = that.$refs.templateTable.getXbxj()
        that.openApprove = true
      }
    },
    async showYjcc() {
      let that = this
      that.resetYjcc()
      that.formYjcc.id = that.form.id
      that.cclistYj = []
      that.addCcYj()
      that.$refs.templateTable.getFilledFormData()
      let resource = that.$refs.templateTable.getResource()
      if (resource.length > 0) {
        that.$modal.loading()
        studyFormFill_yjccFilter({ id: this.form.id, resource: JSON.stringify(resource) }).then(response => {
          let tmpResource = JSON.parse(response.msg)
          if (tmpResource.length > 0) {
            that.resourceYj = tmpResource
            that.openYjcc = true
          } else {
            that.$modal.msgError("没有可存储的药剂")
          }
        }).finally(() => {
          that.$modal.closeLoading()
        })
      } else {
        that.$modal.msgError("没有可存储的药剂")
      }
    },
    async showDy() {
      let that = this
      that.resetYjcc()
      that.formYjcc.id = that.form.id
      that.cclistYj = []
      that.addCcYj()
      that.$refs.templateTable.getFilledFormData()
      that.resourcePrint = _.filter(that.$refs.templateTable.getResource(), function (item) { return item.type == null || item.elnType && item.elnType != '' })
      if (that.resourcePrint.length > 0) {
        that.formBqdy.dyj=null
        that.formBqdy.printList=null
        that.printCheck=[]
        that.openBqdy = true
      } else {
        that.$modal.msgError("没有可打印的标签")
      }
    },
    printbq() {
      let that = this
      this.$refs["formBqdy"].validate(valid => {
        if (valid) {
          that.$modal.msgError("todo")
        }
      })
    },
    async showTjsh(qmyy) {
      let that = this
      let content = await this.$refs.templateTable.getFormData()
      if (content) {
        that.formTjsh.id = that.form.id
        that.formTjsh.remark = ''
        that.formTjsh.qmrmm = ''
        that.formTjsh.bdnr = ''
        that.openTjsh = true
      }
    },
    async saveTjsh() {
      let that = this
      this.$refs["formTjsh"].validate(valid => {
        if (valid) {
          this.formTjsh.bdnr = JSON.stringify(this.$refs.templateTable.getFilledFormData())
          that.$modal.loading()
          studyFormFill_tjblxjsh(that.formTjsh).then(response => {
            that.openTjsh = false
            that.cancelSave()
          }).finally(() => {
            that.$modal.closeLoading()
          })
        }
      })
    },
    resetYjcc() {
      this.formYjcc = {
        id: null,
        qmyy: this.$t('page.business.study.studyFormFill.yjcc'),
        remark: '',
        bdnr: '',
        resource: '',
        qmrmm: '',
        sfcz: false,
        czlist: '',
        sfcc: false,
        cclist: '',
      }
      this.resetForm("formYjcc")
    },
    resetApprove() {
      this.formApprove = {
        id: null,
        qmyy: this.$t('page.business.study.studyFormFill.txbtjjl'),
        remark: '',
        bdnr: '',
        resource: '',
        qmrmm: '',
        sfcz: false,
        czlist: '',
        sfcc: false,
        cclist: '',
      }
      this.resetForm("formApprove")
    },
    pre() {
      this.showIndex = 1
    },
    next() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.showIndex = 2
          this.$modal.loading()
          studyFormFill_bcOnly(this.form).then(response => {
            this.form = response.data
            this.templateData = deepClone(this.form)
            this.showIndex = 2
            const obj = Object.assign({}, this.$route, { title: this.form.bdmc })
            this.$tab.updatePage(obj);
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    selectTemplateChange(val) {
      this.form.bdnr = val.content
      if (this.$i18n.locale === 'zh_CN') {
        this.form.templateMc = val.name
        this.form.bdmc = val.name
      } else {
        this.form.templateMc = val.nameEn
        this.form.bdmc = val.nameEn
      }
      this.form.templateSn = val.sn
      this.templateData = deepClone(this.form)
      setTimeout(() => {
        this.$refs["form"].validate()
      }, 200)
    },
    cancel() {
      try {
        let key = caesarDecipher(this.$route.params.key)
        let data = JSON.parse(key)
        if (data && data != undefined && data != '') {
          const obj = { path: data.url }
          this.$tab.closeOpenPage(obj)
        }
      } catch (e) {
        this.$tab.closeOpenPage('/404')
      }
    },
    cancelSave() {
      try {
        // 类型1试验，5非试验表单，10麻精药表单
        EventBus.$emit('onRefreshStudyTbbdList', {});
        EventBus.$emit('onRefreshNonTrialTbbdList', {});
        EventBus.$emit('onRefreshDrugTbbdList', {});
        let key = caesarDecipher(this.$route.params.key)
        let data = JSON.parse(key)
        if (data && data != undefined && data != '') {
          const obj = { path: data.urlTj ? data.urlTj : data.url }
          this.$tab.closeOpenPage(obj)
        }
      } catch (e) {
        this.$tab.closeOpenPage('/404')
      }
    },
    reset() {
      this.form = {
        id: null,
        studyId: null,
        bdbh: null,
        bdmc: null,
        bdsm: null,
        templateId: null,
        templateMc: null,
        templateSn: null,
        bdnr: null,
        resource: null,
        resourceStudy: null,
        fzrsh: null,
        shryId: null,
        sfbl: null,
        shryMc: null,
      }
      this.resetForm("form")
    },
    selectDeptUserChange(val) {
      this.form.shryMc = val.name
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormFill_bcOnly(this.form).then(response => {
            this.$modal.closeLoading()
            this.cancelSave()
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    showSave() {
      this.formSave.remark = ''
      this.formSave.qmrmm = ''
      this.openSave = true
    },
    async saveNext() {
      this.$refs["formSave"].validate(valid => {
        if (valid) {
          this.form.bdnr = JSON.stringify(this.$refs.templateTable.getFilledFormData())
          this.form.qmrmm = this.formSave.qmrmm
          this.form.remark = this.formSave.remark
          this.$modal.loading()
          studyFormFill_bc(this.form).then(response => {
            this.$modal.closeLoading()
            this.cancelSave()
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    yjcc() {
      let that = this
      this.$refs["formYjcc"].validate(valid => {
        if (valid) {
          if (that.cclistYj.length == 0) {
            this.$modal.msgError("请选择要处置的药剂")
          } else {
            for (var i = 0; i < that.cclistYj.length; i++) {
              if (!that.cclistYj[i].ccwz || that.cclistYj[i].ccwz == '') {
                this.$modal.msgError('第' + (i + 1) + "个存储,存储位置不能为空")
                return
              }
              if (!that.cclistYj[i].cctj || that.cclistYj[i].cctj == '') {
                this.$modal.msgError('第' + (i + 1) + "个存储,存储条件不能为空")
                return
              }
              if (that.cclistYj[i].cctj == '其他' && that.cclistYj[i].cctjqt == '') {
                this.$modal.msgError('第' + (i + 1) + "个存储,存储条件不能为空")
                return
              }
            }
            that.$modal.loading()
            that.formYjcc.resource = JSON.stringify(that.resourceYj)
            that.formYjcc.cclist = JSON.stringify(that.cclistYj)
            studyFormFill_yjcc(that.formYjcc).then(response => {
              that.openYjcc = false
              that.$modal.closeLoading()
              that.cancelSave()
            }).finally(() => {
              that.$modal.closeLoading()
            })
          }
        }
      })
    },
    tj() {
      let that = this
      this.$refs["formApprove"].validate(valid => {
        if (valid) {
          that.formApprove.resource = JSON.stringify(that.resource)
          that.formApprove.yqResource = JSON.stringify(that.yqResource)
          that.formApprove.jcbList = JSON.stringify(that.jcbList)
          that.formApprove.xbxj = JSON.stringify(that.xbxj)
          if (that.czlist.length > 0) {
            that.formApprove.czlist = JSON.stringify(that.czlist)
          }
          if (that.cclist.length > 0) {
            that.formApprove.cclist = JSON.stringify(that.cclist)
          }
          for (var i = 0; i < that.czlist.length; i++) {
            if (!that.czlist[i].czfs || that.czlist[i].czfs == '') {
              this.$modal.msgError('第' + (i + 1) + "个处置,处置方式不能为空")
              return
            }
            if (that.czlist[i].czfs == '其他' && that.czlist[i].czfsqt == '') {
              this.$modal.msgError('第' + (i + 1) + "个存储,处置方式不能为空")
              return
            }
          }
          for (var i = 0; i < that.cclist.length; i++) {
            if (!that.cclist[i].ccwz || that.cclist[i].ccwz == '') {
              this.$modal.msgError('第' + (i + 1) + "个存储,存储位置不能为空")
              return
            }
            if (!that.cclist[i].cctj || that.cclist[i].cctj == '') {
              this.$modal.msgError('第' + (i + 1) + "个存储,存储条件不能为空")
              return
            }
            if (that.cclist[i].cctj == '其他' && that.cclist[i].cctjqt == '') {
              this.$modal.msgError('第' + (i + 1) + "个存储,存储条件不能为空")
              return
            }
          }
          debugger
          that.$modal.loading()
          studyFormFill_tj(that.formApprove).then(response => {
            that.openApprove = false
            that.$modal.closeLoading()
            that.cancelSave()
          }).finally(() => {
            that.$modal.closeLoading()
          })
        }
      })
    }
  }
}
</script>
