<template>
  <div class="process-definition-index">
    <div>
      <div class="search" style="margin-bottom: 20px">
        <div class="left">
          <div>
            <el-button type="primary" @click="refresh">刷新</el-button>
          </div>
        </div>
        <div class="right"></div>
      </div>
      <div style="width: 100%">
        <cy-comm-table
          ref="commTable"
          v-loading="loading"
          :columns="columns"
          :table-options="tableOptions"
          :data="processData"
          :count="total"
        />
      </div>
    </div>
    <cy-dialog ref="setTimelimit" title="设置时限">
      <el-form
        ref="setTimelimitForm"
        v-loading="isLoading"
        :model="setTimelimitForm"
        :rules="setTimelimitRules"
        label-width="120px"
        label-position="left"
      >
        <el-form-item label="任务时限：" prop="timeLimit">
          <el-select
            v-model="setTimelimitForm.timeLimit"
            placeholder="请选择任务时限"
            clearable
          >
            <el-option
              v-for="item in constant.flowableTime"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="时限类型："
          prop="workingDay"
        >
          <el-select
            v-model="setTimelimitForm.workingDay"
            placeholder="请选择时限类型"
            clearable
          >
            <el-option label="工作日" :value="true" />
            <el-option label="自然日" :value="false" />
          </el-select>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="primary" @click="setTimelimit">确定</el-button>
        <el-button type="normal" @click="$refs.setTimelimit.show = false">关闭</el-button>
      </template>
    </cy-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import indexTable from './processDefinitionIndexTable'
import { doListFlowActDef, doSetTimeLimit } from '@/api/systemManage/processManage/processManage'
export default {
  name: 'ProcessManageProcessDefinitionIndex',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      isLoading: false,
      processData: [],
      page: 1,
      size: 10,
      total: 0,
      setTimelimitForm: {
        timeLimit: '',
        workingDay: ''
      },
      setTimelimitRules: {
        timeLimit: [
          { required: true, message: '请选择任务时限', trigger: 'change' }
        ],
        workingDay: [
          { required: true, message: '请选择时限类型', trigger: 'change' }
        ]
      },
      id: ''
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'constant'
    ])
  },
  created() {
    this.$store.dispatch('getConstant', ['flowableTime', 'code'])
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      const { id } = this.$route.query
      const params = {
        page: this.page,
        rows: this.size,
        procDefId: id
      }
      doListFlowActDef(params).then(response => {
        this.processData = response.rows
        this.loading = false
        this.total = response.records
      })
    },
    refresh() {
      this.fetchData()
    },
    handlerOperate(type, row) {
      this.$refs.setTimelimit.show = true
      this.id = row.id
    },
    setTimelimit() {
      const params = {
        id: this.id,
        timeLimit: this.setTimelimitForm.timeLimit,
        workingDay: this.setTimelimitForm.workingDay
      }
      if (!this.setTimelimitForm.timeLimit) {
        this.$message.warning('请选择任务时限')
        return
      }
      if (!this.setTimelimitForm.workingDay) {
        this.$message.warning('请选择时限类型')
        return
      }
      doSetTimeLimit(params).then(() => {
        this.$refs.setTimelimit.show = false
        this.fetchData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.process-definition-index {

}
</style>
