<template>
  <div>
    <div class="ytbd-list" v-show="!showDetail && !showEdit && !showAudit">
      <div class="ytbd-search">
        <el-form :model="searchForm" ref="searchForm" :inline="true" label-width="110px">
          <el-row>
            <el-col :span="6">
              <el-form-item :label="$t('page.business.form.bdmc')" prop="name" >
                <el-input v-model="searchForm.bdmc" :placeholder="$t('form.placeholderInput')" clearable
                  style="width: 200px"  />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('page.business.form.bdbh')" prop="sn">
                <el-input v-model="searchForm.bdbh" :placeholder="$t('form.placeholderInput')" clearable
                  style="width: 200px"  />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('page.business.form.gsr')" prop="userId">
                <SelectDeptUser v-model="searchForm.userId" :name="searchForm.userMc" key="userId"
                  @change="userChange" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="search">{{ $t('form.search') }}</el-button>
                <el-button icon="el-icon-refresh" @click="reset">{{ $t('form.reset') }}</el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item :label="$t('page.business.form.xtmbmc')" prop="templateName">
                <el-input v-model="searchForm.templateMc" :placeholder="$t('form.placeholderInput')" clearable
                  style="width: 200px"  />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('page.business.form.cjsj')">
                <el-date-picker v-model="daterange" clearable type="daterange" range-separator="-"
                  :start-placeholder="$t('page.business.form.startDate')"
                  :end-placeholder="$t('page.business.form.endDate')" value-format="yyyy-MM-dd" 
                  style="width: 200px" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="ytbd-content">
        <el-row class="add-box">
          <el-col>
            <el-button type="primary" icon="el-icon-plus" v-if="isMatchSubject" @click="showAdd()"
              v-hasPermi="['business:studyFormPre:xz']">{{
                $t('page.business.study.studyFormPre.xzbd') }}</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="list">
          <el-table-column :label="$t('page.business.form.bh')" prop="bdbh" :show-overflow-tooltip="true" />
          <el-table-column :label="$t('page.business.form.bdmc')" prop="bdmc" :show-overflow-tooltip="true" />
          <el-table-column :label="$t('page.business.form.xtmbmc')" prop="templateMc" :show-overflow-tooltip="true" />
          <el-table-column :label="$t('page.business.form.cjsj')" align="center" prop="createTime" width="140" />
          <el-table-column :label="$t('page.business.form.cjr')" align="center" prop="userMc" width="100" />

          <el-table-column :label="$t('page.business.form.status')" prop="status" width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.bdzt === 1"><span class="status-circle tbz"></span>{{ $t('page.business.form.statusTbz') }}</span>
              <span v-if="scope.row.bdzt === 3"><span class="status-circle ytj"></span>{{ $t('page.business.form.statusYtj') }}</span>
              <span v-if="scope.row.bdzt === 5"><span class="status-circle ytg"></span>{{ $t('page.business.form.statusYtg') }}</span>
              <span v-if="scope.row.bdzt === 7"><span class="status-circle wtg"></span>{{ $t('page.business.form.statusWtg') }}</span>
              <span v-if="scope.row.bdzt === 9"><span class="status-circle dgb"></span>{{ $t('page.business.form.statusDgb') }}</span>
              <span v-if="scope.row.bdzt === 11"><span class="status-circle tgb"></span>{{ $t('page.business.form.statusYgb') }}</span>
            </template>
          </el-table-column>


          <el-table-column :label="$t('form.operate')" fixed="right" align="center" width="200">
            <template slot-scope="scope">
              <!-- 详情 -->
              <el-button type="text" v-hasPermi="['business:studyFormPre:xq']" @click="detail(scope.row)">{{
                $t('form.detail') }}</el-button>
              <!-- 编辑 -->
              <el-button type="text" v-hasPermi="['business:studyFormPre:bj']" @click="edit(scope.row)"
                v-if="(scope.row.bdzt === 1 || scope.row.bdzt === 7) && id == scope.row.userId">{{
                  $t('form.edit') }}</el-button>
              <template v-if="scope.row.fzrsh == 10">
                <template v-if="scope.row.fzrshzt == 5">
                  <!-- 负责人审核 -->
                  <el-button type="text" @click="audit(scope.row)" v-hasPermi="['business:studyFormPre:sh']"
                    v-if="scope.row.bdzt === 3 && id == leader">{{
                      $t('page.business.form.audit') }}</el-button>
                </template>
                <template>
                  <!-- 有权限审核 -->
                  <el-button type="text" @click="audit(scope.row)" v-hasPermi="['business:studyFormPre:sh']"
                    v-if="scope.row.bdzt === 3 && isMatchSubjectt&&id!=scope.row.userId">{{ $t('page.business.form.audit') }}</el-button>
                </template>
              </template>
              <template v-else>
                <!-- 有权限审核 -->
                <el-button type="text" @click="audit(scope.row)" v-hasPermi="['business:studyFormPre:sh']"
                  v-if="scope.row.bdzt === 3 && isMatchSubject&&id!=scope.row.userId">{{ $t('page.business.form.audit') }}</el-button>
              </template>
              <template v-if="scope.row.bdzt === 5 && id == scope.row.userId">
                <el-button type="text" @click="gb(scope.row)">{{ $t('page.business.study.studyFormPre.guanbi')
                }}</el-button>
              </template>
              <template v-if="scope.row.bdzt === 9 && id == scope.row.sjshryId">
                <el-button type="text" @click="qrgb(scope.row)">{{ $t('page.business.study.studyFormPre.qrgb')
                }}</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize"
          @pagination="getList" />
      </div>
    </div>
    <!-- 编辑 -->
    <Bj v-show="showEdit" key="Bj" ref="Bj" @close="bjClose" />
    <!-- 详情 -->
    <Xq v-show="showDetail" key="Xq" ref="Xq" @close="xqClose" />
    <!-- 审核 -->
    <Sh v-show="showAudit" key="Sh" ref="Sh" @close="shClose" />
    <!-- 关闭 -->
    <Gb key="Gb" ref="Gb" @callback="search" />
    <!-- 确认关闭 -->
    <Qrgb key="Qrgb" ref="Qrgb" @callback="search" />

    <el-dialog :title="$t('page.business.study.studyFormPre.xjbd')" :visible.sync="open" width="500px" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" label-width="120px">
        <el-row>
          <el-col :span="24" style="text-align: center; margin-top: 50px;">
            <el-button type="primary" style="width: 80%; height: 40px;" @click="add" plain icon="el-icon-circle-plus">{{
              $t('page.business.study.studyFormPre.cjxbd') }}</el-button>
          </el-col>
          <el-col :span="24" style="text-align: center; margin: 50px 0px;">
            <el-button type="primary" style="width: 80%;height: 40px;" @click="copy" plain
              icon="el-icon-document-copy">{{
                $t('page.business.study.studyFormPre.fzyybd') }}</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :title="$t('page.business.study.studyFormPre.fzbdxz')" :visible.sync="openXz" width="500px"
      append-to-body :close-on-click-modal="false">
      <el-form ref="formXz" :model="formXz" :rules="rulesXz" label-width="180px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('page.business.study.studyFormPre.sssyfsy')" prop="studyId">
              <el-select v-model="formXz.studyId" :placeholder="$t('form.placeholderSelect')" @change="studyChange">
                <el-option :label="item.sn" :value="item.id" v-for="(item, index) in studyList" :key="index" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('page.business.study.studyFormPre.fzbd')" prop="formPreId">
              <el-select v-model="formXz.formPreId" :placeholder="$t('form.placeholderSelect')">
                <el-option :label="item.bdmc" :value="item.id" v-for="(item, index) in formPreList" :key="index" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <el-button type="primary" @click="previewFzbd" :disabled="!(formXz.formPreId)">{{
                $t('page.business.study.studyFormPre.yulan') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveCopy">{{ $t('page.business.study.studyFormPre.fzbj') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('page.business.study.studyFormPre.yulan')" :visible.sync="openPreview" width="80%"
      append-to-body :close-on-click-modal="false">
      <TemplateTable ref="templateTable" :sn="formDatePreview.templateSn" v-if="openPreview"
        :templateData="formDatePreview" fillType="detail" />
    </el-dialog>
  </div>

</template>

<script>
import { studySubject_isMatchSubject } from "@/api/business/study/studySubject"
import { studyMethod_getReadAllMethodStatus } from '@/api/business/study/studyMethod'
import { studyFormPre_list, studyFormPre_info, studyFormPre_studyList, studyFormPre_studyFormPreList } from '@/api/business/study/studyFormPre'
import TemplateTable from '@/views/business/comps/template/TemplateTable';
import Bj from "./ytbd/Bj";
import Xq from "./ytbd/Xq";
import Sh from "./ytbd/Sh";
import Gb from "./ytbd/Gb";
import Qrgb from "./ytbd/Qrgb";
import SelectDeptUser from '@/views/business/comps/select/SelectDeptUser';
import moment from "moment";
import { mapGetters } from 'vuex'
export default {
  name: 'YtbdList',
  props: {
    study: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ]),
  },
  watch: {
    study: {
      handler(newVal) {
        this.searchForm.studySn = newVal.sn
        this.searchForm.studyMc = newVal.name
        this.searchForm.studyId = newVal.id
        this.searchForm.studySubjectId = newVal.studySubjectId
        this.leader = newVal.leader
        this.search()
        this.getIsMatchSubject()
      },
      immediate: true,
      deep: true
    }
  },
  components: { Gb, Qrgb, Bj, Xq, Sh, SelectDeptUser, TemplateTable },
  data() {
    return {
      isMatchSubject: false,
      formDatePreview: {},
      openPreview: false,
      openXz: false,
      open: false,
      leader: null,
      showAudit: false,
      showEdit: false,
      showDetail: false,
      daterange: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        studyId: '',
        studySn: '',
        studyMc: '',
        studySubjectId: '',
        bdbh: '',
        bdmc: '',
        userId: '',
        userMc: '',
        templateMc: '',
        startDate: '',
        endDate: '',
      },
      loading: false,
      total: 0,
      list: [],
      studyList: [],
      formPreList: [],
      formXz: {
        studyId: null,
        formPreId: null,
      },
      rulesXz: {
        studyId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        formPreId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
    }
  },
  created() {
  },
  methods: {
    getIsMatchSubject() {
      studySubject_isMatchSubject({ studySubjectId: this.searchForm.studySubjectId }).then(response => {
        this.isMatchSubject = response.data
      })
    },
    copy() {
      this.$modal.loading()
      studyFormPre_studyList().then(response => {
        this.studyList = response.data
        this.formXz.studyId = null
        this.formXz.formPreId = null
        this.open = false
        this.openXz = true
        this.$modal.closeLoading()
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    previewFzbd() {
      this.$modal.loading()
      studyFormPre_info({ id: this.formXz.formPreId }).then(response => {
        this.formDatePreview = _.merge({}, response.data, {
          studySn: this.searchForm.studySn,
          studyMc: this.searchForm.studyMc,
          studyId: this.searchForm.studyId,
        })
        this.openPreview = true
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    saveCopy() {
      let that = this
      this.$refs["formXz"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormPre_info({ id: that.formXz.formPreId }).then(response => {
            let tmp = response.data
            this.$modal.closeLoading()
            this.openXz = false
            this.openPreview = false
            this.showEdit = true
            this.$emit('showDetail', this.showEdit)
            this.$refs.Bj.edit({
              templateId: tmp.templateId,
              templateMc: tmp.templateMc,
              templateMcEn: tmp.templateMcEn,
              templateShowSn: tmp.templateShowSn,
              templateSn: tmp.templateSn,
              bdmc: tmp.bdmc,
              bdnr: tmp.bdnr,
              studySn: this.searchForm.studySn,
              studyMc: this.searchForm.studyMc,
              studyId: this.searchForm.studyId,
              permitForSecectUser: 'business:studyFormPre:sh'
            })
          }).finally(() => {
            this.$modal.closeLoading()
          })

        }
      })
    },
    studyChange() {
      this.$modal.loading()
      studyFormPre_studyFormPreList({ studyId: this.formXz.studyId }).then(response => {
        this.formXz.formPre = null
        this.formPreList = response.data
        this.$modal.closeLoading()
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    showAdd() {
      this.$modal.loading()
      studyMethod_getReadAllMethodStatus({ userId: this.id, studyId: this.searchForm.studyId, studySubjectId: this.searchForm.studySubjectId }).then(response => {
        this.open = true
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    userChange(val) {
      this.searchForm.userId = val.id
      this.searchForm.userMc = val.name
    },
    search() {
      this.searchForm.pageNum = 1
      this.getList()
    },
    reset() {
      this.searchForm.bdbh = ''
      this.searchForm.bdmc = ''
      this.searchForm.userId = ''
      this.searchForm.userMc = ' '
      this.searchForm.templateMc = ''
      this.searchForm.startDate = ''
      this.searchForm.endDate = ''
      this.daterange=[]
      this.search()
    },
    getList() {
      if (this.daterange != null && this.daterange.length > 0) {
        this.searchForm.startDate = this.daterange[0]
        this.searchForm.endDate = moment().add(this.daterange[1], 'days').format('YYYY-MM-DD');
      } else {
        this.searchForm.startDate = ''
        this.searchForm.endDate = ''
      }
      this.loading = true
      studyFormPre_list(this.searchForm).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    add() {
      this.open = false
      this.showEdit = true
      this.$emit('showDetail', this.showEdit)
      this.$refs.Bj.edit({
        studySn: this.searchForm.studySn,
        studyMc: this.searchForm.studyMc,
        studyId: this.searchForm.studyId,
        studySubjectId: this.searchForm.studySubjectId,
        permitForSecectUser: 'business:studyFormPre:sh'
      })
    },
    edit(row) {
      this.$modal.loading()
      studyMethod_getReadAllMethodStatus({ userId: this.id, studyId: this.searchForm.studyId, studySubjectId: this.searchForm.studySubjectId }).then(response => {
        this.showEdit = true
        this.$emit('showDetail', this.showEdit)
        this.$refs.Bj.edit(_.merge({}, row, { permitForSecectUser: 'business:studyFormPre:sh' }))
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    bjClose() {
      this.showEdit = false
      this.$emit('showDetail', this.showEdit)
      this.search()
    },
    xqClose() {
      this.showDetail = false
      this.$emit('showDetail', this.showDetail)
      this.search()
    },
    shClose() {
      this.showAudit = false
      this.$emit('showDetail', this.showAudit)
      this.search()
    },
    detail(row) {
      this.showDetail = true
      this.$emit('showDetail', this.showDetail)
      this.$refs.Xq.show(row, false)
    },
    gb(row) {
      this.$modal.loading()
      studyMethod_getReadAllMethodStatus({ userId: this.id, studyId: this.searchForm.studyId, studySubjectId: this.searchForm.studySubjectId }).then(response => {
        this.$refs.Gb.show(row)
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    qrgb(row) {
      this.$modal.loading()
      studyMethod_getReadAllMethodStatus({ userId: this.id, studyId: this.searchForm.studyId, studySubjectId: this.searchForm.studySubjectId }).then(response => {
        this.$refs.Qrgb.show(row)
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    audit(row) {
      this.$modal.loading()
      studyMethod_getReadAllMethodStatus({ userId: this.id, studyId: this.searchForm.studyId, studySubjectId: this.searchForm.studySubjectId }).then(response => {
        this.showAudit = true
        this.$emit('showDetail', this.showAudit)
        this.$refs.Sh.show(row)
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.ytbd-list {
  .ytbd-search {
    background: #fff;
    padding: 20px;
    margin-bottom: 10px;

    .right-btn {
      text-align: right
    }
  }

  .ytbd-content {
    padding: 20px;
    background: #fff;

    .add-box {
      margin-bottom: 10px;
    }
  }
}
</style>
