<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="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.studyFormPlan.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.studyFormPlan.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.studyFormPlan.zc') }}</el-button>
            <el-button type="primary" @click="next">{{ $t('page.business.study.studyFormPlan.next') }}</el-button>
          </template>
          <template v-else>
            <el-button @click="saveNext">{{ $t('page.business.study.studyFormPlan.save') }}</el-button>
            <el-button type="primary" @click="showtijiao">{{ $t('page.business.study.studyFormPlan.submit')
            }}</el-button>
            <el-button type="primary" @click="pre">{{ $t('page.business.study.studyFormPlan.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="$t('page.business.form.bdmb')" prop="templateId">
                  <SelectTemplate v-model="form.templateId" :studyType="studyType" :studyFormType="studyFormType"
                    :needPre="1" :name="form.templateMc" @change="selectTemplateChange" />
                </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.studyFormPlan.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">
          <TemplateTable ref="templateTable" emitName="onPlanCallback" @onPlanCallback="onPlanCallback"
            :sn="form.templateSn" :templateData="templateData" fillType="actFill" />
        </div>
      </div>
    </div>
    <el-dialog :title="$t('page.business.study.studyFormPlan.tjjl')" :visible.sync="openSubmit" width="500px"
      append-to-body :close-on-click-modal="false">
      <el-form ref="formSubmit" :model="form" :rules="rulesApprove" label-width="120px" v-if="openSubmit">
        <div class="sbzdtcma"> <input type="text"> <input type="password" show-password> </div>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('page.business.study.studyFormPlan.shryId')" prop="fshryId">
              <SelectStudyUser v-model="form.fshryId" :query="{
                studyId: form.studyId,
                permit: 'business:studyFormPlan:fh'
              }" key="fshryId" @change="selectDeptUserChange" />
            </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="form.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="form.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" show-password v-model="form.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="openSubmit = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { studyFormPlan_bc, studyFormPlan_bcOnly, studyFormPlan_tj, studyFormPlan_info, studyFormPlan_updateBdnr, studyFormFill_updateFhyjjl } from "@/api/business/study/studyFormPlan"
import { mapGetters } from 'vuex'
import SelectTemplate from "@/views/business/comps/select/SelectTemplate";
import SelectStudyUser from '@/views/business/comps/select/SelectStudyUser';
import TemplateTable from '@/views/business/comps/template/TemplateTable';
import { deepClone } from "@/utils/index";

export default {
  name: "Bj",
  components: { SelectTemplate, SelectStudyUser, TemplateTable },
  data() {
    return {
      open: false,
      showIndex: 1,
      form: {},
      openSubmit: false,
      rulesApprove: {
        // fshryId: [{
        //   required: true,
        //   message: ' ',
        //   trigger: 'blur'
        // }],
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        fzrsh: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        shryId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      rules: {
        bdmc: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        templateId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],

      },
      templateData: {},
      studyType: null,
      studyFormType: null
    }
  },
  computed: {
    ...mapGetters([
      'nickName', 'name'
    ]),
  },
  created() {
  },
  methods: {
    onPlanCallback(data) {
      console.log("data:" + JSON.stringify(data))
      console.log("formdata:" + JSON.stringify(this.$refs.templateTable.getFilledFormData()))
      if (data.type == 'fieldChanged') {
        studyFormPlan_updateBdnr(
          {
            id: this.form.id,
            bdnr: JSON.stringify(this.$refs.templateTable.getFilledFormData()),
            zdxgjl: JSON.stringify(data.resourceList),
            filedValue: JSON.stringify(data.newRecord)
          }
        ).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 => {
        })
      }
    },
    pre() {
      this.showIndex = 1
    },
    next() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormPlan_bcOnly(this.form).then(response => {
            this.form = response.data
            this.templateData.bdmc = this.form.bdmc
            this.showIndex = 2
            this.$modal.closeLoading()
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    selectTemplateChange(val) {
      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.bdnr = val.content
      this.form.templateSn = val.sn
      this.templateData = deepClone(this.form)
    },
    cancel() {
      this.$emit('close')
      this.open = false
    },
    reset() {
      this.form = {
        id: null,
        studyId: null,
        bdbh: null,
        bdmc: this.$i18n.locale === 'zh_CN'?'试验物质配制计划表':'试验物质配制计划表',
        bdsm: null,
        templateId: 1,
        templateMc: this.$i18n.locale === 'zh_CN'?'试验物质配制计划表':'试验物质配制计划表',
        templateSn: 'SYWZPZJHB',
        bdnr: null,
        fzrsh: null,
        shryId: null,
        shryMc: null,
      }
      this.resetForm("form")
    },
    selectDeptUserChange(val) {
      this.form.fhryMc = val.name
    },
    edit(row) {
      this.reset()
      this.studyType = row.studyType || null
      this.studyFormType = row.studyFormType || null
      this.form.qmyy = this.$t('page.business.study.studyFormPlan.cjjl')
      if (row && row.id) {
        this.showIndex = 2
        this.$modal.loading()
        studyFormPlan_info({ id: row.id }).then(response => {
          this.form = response.data
          this.templateData = deepClone(this.form)
          this.saveSimpleLog({ jcmc: '计划表单编辑', jcmcEn: 'Plan Table Edit', name: this.form.bdmc + '（' + this.form.bdbh + '）', nameEn: this.form.bdmc + '（' + this.form.bdbh + '）' })
          this.open = true
        }).finally(() => {
          this.$modal.closeLoading()
        })
      } else {
        this.showIndex = 1
        this.form = _.merge({}, this.form, row)
        this.templateData = deepClone(this.form)
        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: 'Plan Table Create', name: name, nameEn: nameEn })
        this.open = true
      }
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormPlan_bc(this.form).then(response => {
            this.$emit('close')
            this.open = false
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    async saveNext() {
      this.form.bdnr = JSON.stringify(this.$refs.templateTable.getFilledFormData())
      this.$modal.loading()
      studyFormPlan_bc(this.form).then(response => {
        this.$emit('close')
        this.open = false
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    tj() {
      this.$refs["formSubmit"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormPlan_tj(this.form).then(response => {
            this.$emit('close')
            this.open = false
            this.openSubmit = false
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    async showtijiao() {
      let content = await this.$refs.templateTable.getFormData()
      if (content) {
        this.form.bdnr = JSON.stringify(content)
        this.openSubmit = true
      }
    }
  }
}
</script>
