<template>
  <div class="tbbd-list">
    <div v-show="!showDetail && !showEdit && !showAudit && !showFh">
      <div class="tbbd-search">
        <el-form :model="searchForm" ref="searchForm" :inline="true" label-width="100px">
          <el-row>
            <el-col :span="5">
              <el-form-item :label="$t('page.business.form.bdmc')" prop="name" :label-width="$i18n.locale === 'zh_CN'?'100px':'120px'">
                <el-input v-model="searchForm.bdmc" :placeholder="$t('form.placeholderInput')" clearable
                  style="width: 160px" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item :label="$t('page.business.form.bdbh')" prop="sn">
                <el-input v-model="searchForm.bdbh" :placeholder="$t('form.placeholderInput')" clearable
                  style="width: 160px" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item :label="$t('page.business.form.gsr')" prop="userId">
                <SelectDeptUser v-model="searchForm.userId" style="width: 160px" :name="searchForm.userMc" key="userId"
                  @change="userChange" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <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-col :span="4">
              <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-form>
      </div>
      <div class="tbbd-content">
        <el-row class="add-box">
          <el-col>
            <el-button type="primary" icon="el-icon-plus" @click="add()" v-hasPermi="['business:studyFormPlan:xz']"
              v-if="id == leader">{{
                $t('page.business.study.studyFormPlan.xzpzjhb') }}</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.gsr')" align="center" prop="userMc" width="100" />
          <el-table-column :label="$t('page.business.form.status')" prop="status" align="center" width="180">
            <template slot-scope="scope">
              <span v-if="scope.row.bdzt === 1"><span class="status-circle tbz"></span>{{ $t('page.business.study.studyFormPlan.tbz') }}</span>
              <span v-if="scope.row.bdzt === 3"><span class="status-circle ytj"></span>{{ $t('page.business.study.studyFormPlan.ytj') }}</span>
              <span v-if="scope.row.bdzt === 5"><span class="status-circle ywcfh"></span>{{ $t('page.business.study.studyFormPlan.ywcfh') }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('form.operate')" align="left" fixed="right" width="200">
            <template slot-scope="scope">
              <!-- 表单状态：1：填报中；3：已提交；5：已完成（经复核）; -->
              <!-- 详情 -->
              <el-button type="text" @click="detail(scope.row)" v-hasPermi="['business:studyFormPlan:xq']">{{
                $t('form.detail') }}</el-button>
              <!-- 编辑 -->
              <el-button type="text" v-if="(scope.row.bdzt === 1 || scope.row.bdzt === 5) && id == scope.row.userId"
                @click="edit(scope.row)" v-hasPermi="['business:studyFormPlan:bj']">{{
                  $t('form.edit') }}</el-button>
              <!-- 复核 -->
              <el-button type="text" v-if="scope.row.bdzt === 3&&id!=scope.row.userId" @click="fh(scope.row)"
                v-hasPermi="['business:studyFormPlan:fh']">{{
                  $t('page.business.study.studyFormPlan.fh') }}</el-button>
              <!-- 更换归属人 -->
              <el-button type="text"
                v-if="(scope.row.bdzt === 1 || scope.row.bdzt === 3 || scope.row.bdzt === 5) && id == scope.row.subjectLeader"
                @click="ghgsr(scope.row)" v-hasPermi="['business:studyFormPlan:ghgsr']">{{
                  $t('page.business.study.studyFormPlan.ghgsr') }}</el-button>
              <!-- 审阅 -->
              <el-button type="text" v-if="scope.row.bdzt === 5 && (isQa || id == leader)" @click="sy(scope.row)"
                v-hasPermi="['business:studyFormPlan:sy']">{{
                  $t('page.business.study.studyFormPlan.sy') }}</el-button>
            </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" />
    <!-- 审阅 -->
    <Sy v-show="showAudit" key="Sy" ref="Sy" @close="syClose" />
    <!-- 复核 -->
    <Fh v-show="showFh" key="Fh" ref="Fh" @close="fhClose" />
    <!-- 更换归属人 -->
    <Ghgsr key="Ghgsr" ref="Ghgsr" @callback="search" />
    <SelectDeptUserDialog ref="selectDeptUserDialog" @change="ghgsrChange" />
  </div>

</template>

<script>
import { studySubject_isQa } from "@/api/business/study/studySubject"
import { studyFormPlan_list } from '@/api/business/study/studyFormPlan'
import { studyMethod_getReadAllMethodStatus } from '@/api/business/study/studyMethod'
import Bj from "./jhbd/Bj";
import Xq from "./jhbd/Xq";
import Sy from "./jhbd/Sy";
import Fh from "./jhbd/Fh";
import Ghgsr from "./jhbd/Ghgsr";
import SelectDeptUser from '@/views/business/comps/select/SelectDeptUser';
import SelectDeptUserDialog from '@/views/business/comps/select/SelectDeptUserDialog';
import { mapGetters } from 'vuex'
import moment from "moment";
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.leader = newVal.leader
        this.search()
      },
      immediate: true,
      deep: true
    }
  },
  components: { Ghgsr, Fh, Bj, Xq, Sy, SelectDeptUser, SelectDeptUserDialog },
  data() {
    return {
      isQa: false,
      leader: null,
      showFh: false,
      showAudit: false,
      showEdit: false,
      showDetail: false,
      daterange: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        studyId: '',
        bdbh: '',
        bdmc: '',
        studySn: '',
        studyMc: '',
        userId: '',
        userMc: '',
        cjsjjs: '',
        cjsjks: '',
      },
      loading: false,
      total: 0,
      list: [],
      currentRow: null
    }
  },
  created() { this.getIsQa() },
  methods: {
    getIsQa() {
      studySubject_isQa().then(response => {
        this.isQa = response.data
      })
    },
    ghgsr(row) {
      this.currentRow = row
      this.$refs.selectDeptUserDialog.show(null, null)
    },
    ghgsrChange(val) {
      this.$refs.Ghgsr.show({ id: this.currentRow.id, userId: val.id, userMc: val.name })
    },
    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.cjsjjs = ''
      this.searchForm.cjsjks = ''
      this.daterange = []
      this.search()
    },
    getList() {
      if (this.daterange != null && this.daterange.length > 0) {
        this.searchForm.cjsjks = this.daterange[0]
        this.searchForm.cjsjjs = moment().add(this.daterange[1], 'days').format('YYYY-MM-DD');
      } else {
        this.searchForm.cjsjks = ''
        this.searchForm.cjsjjs = ''
      }
      this.loading = true
      studyFormPlan_list(this.searchForm).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    add() {
      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({ studySn: this.searchForm.studySn, studyMc: this.searchForm.studyMc, studyId: this.searchForm.studyId, studySubjectId: this.searchForm.studySubjectId })
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    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(row)
      }).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()
    },
    syClose() {
      this.showAudit = false
      this.$emit('showDetail', this.showAudit)
      this.search()
    },
    fhClose() {
      this.showFh = false
      this.$emit('showDetail', this.showFh)
      this.search()
    },
    detail(row) {
      this.showDetail = true
      this.$emit('showDetail', this.showDetail)
      this.$refs.Xq.show(row)
    },
    sy(row) {
      this.showAudit = true
      this.$emit('showDetail', this.showAudit)
      this.$refs.Sy.show(row)
    },
    fh(row) {
      this.$modal.loading()
      studyMethod_getReadAllMethodStatus({ userId: this.id, studyId: this.searchForm.studyId, studySubjectId: this.searchForm.studySubjectId }).then(response => {
        this.showFh = true
        this.$emit('showDetail', this.showFh)
        this.$refs.Fh.show(row)
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.tbbd-list {


  .tbbd-search {
    background: #fff;
    padding: 10px;

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

  .tbbd-content {
    background: #fff;
    padding: 10px;

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