<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="saveNext">{{ $t('page.business.study.studyFormFill.save') }}</el-button>
            <el-button type="primary" @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" @change="selectTemplateChange" :needPre="1" />
                </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')"
                    :disabled="form.id && form.id !== ''">
                    <el-option :label="$t('page.business.study.studyFormFill.yes')" :value="10" />
                    <el-option :label="$t('page.business.study.studyFormFill.no')" :value="1" />
                  </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">
          <TemplateTable ref="templateTable" :sn="form.templateSn" :templateData="form" 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">
        <div class="sbzdtcma"> <input type="text"> <input type="password"> </div>
        <el-row>
          <el-col :psna="24">
            <el-form-item>
              <el-checkbox v-model="showCz">处置</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :psna="24">
            <el-form-item>
              <el-checkbox v-model="showCc">存储</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :psna="24">
            <el-form-item>
              用量统计
            </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">
              <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="tj">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openApprove = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { studyFormFill_bc, studyFormFill_tj, studyFormFill_info } 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';

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

      },
      openApprove: false,
      formApprove: {},
      bdmbTitle: this.$t('page.business.form.bdmb'),
      rulesApprove: {
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
    }
  },
  computed: {
    ...mapGetters([
      'nickName', 'name'
    ]),
  },
  created() {
    this.getInfo()
  },
  methods: {
    getInfo() {
      let formId = this.$route.params.formId
      let studyId = this.$route.params.studyId
      let fromYt = this.$route.params.fromYt
      this.reset()
      if (parseInt(formId) > 0) {
        if (fromYt + '' == "10") {
          this.showIndex = 1
          this.bdmbTitle = this.$t('page.business.study.studyFormFill.ytbd')
        } else {
          this.showIndex = 2
        }
        this.$modal.loading()
        studyFormFill_info({ id: formId }).then(response => {
          this.form = response.data
          this.form.qmyy = '制作提交预制表单'
          this.$modal.closeLoading()
        })
      } else {
        this.showIndex = 1
        this.$modal.loading()
        study_info({ id: studyId }).then(response => {
          this.form = _.merge({}, this.form, { stydyId: studyId, resourceStudy: response.data.resource })
          this.$modal.closeLoading()
        })
      }
    },
    async showApprove() {
      let content = await this.$refs.templateTable.getFormData()
      if (content) {
        this.form.bdnr = JSON.stringify(content)
        this.resetApprove()
        this.formApprove.id = this.form.id
        this.openApprove = true
      }
    },
    resetApprove() {
      this.formApprove = {
        id: null,
        qmyy: '填写并提交记录',
        remark: '',
        bdnr: '',
        resource: '',
        qmrmm: '',
      }
      this.resetForm("formApprove")
    },
    pre() {
      this.showIndex = 1
    },
    next() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.showIndex = 2
        }
      })
    },
    selectTemplateChange(val) {
      this.form.bdnr = val.content
      this.form.templateMc = val.name
      this.form.bdmc = val.name
      this.form.templateSn = val.sn
    },
    cancel() {
      let data = localStorage.getItem(this.$route.params.key)
      if (data && data != '') {
        let params = JSON.parse(data)
        const obj = { path: params.url }
        this.$tab.closeOpenPage(obj)
      }
    },
    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_bc(this.form).then(response => {
            this.$modal.closeLoading()
            this.cancel()
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    async saveNext() {
      let content = await this.$refs.templateTable.getFormData()
      if (content) {
        this.form.bdnr = JSON.stringify(content)
        this.$modal.loading()
        studyFormFill_bc(this.form).then(response => {
          this.$modal.closeLoading()
          this.cancel()
        }).finally(() => {
          this.$modal.closeLoading()
        })
      }
    },
    tj() {
      this.$refs["formApprove"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormFill_tj(this.formApprove).then(response => {
            this.openApprove = false
            this.$modal.closeLoading()
            this.cancel()
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    }
  }
}
</script>
