<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @change="research" clearable standout="bg-primary text-white" v-model="msg.RuleName" label="规则名称"
            @clear="research" maxlength="20" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-column-table"
        separator="none" :data="dataList" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">OKR规则模板</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" @click="addModule(null)" label="新增模板" />
          </div>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.PageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props" style="width:200px;">
            <q-btn flat size="xs" icon="edit" color="red" v-if="props.row.Enable==1" style="font-weight:400"
              @click="getStart(props.row,2)" label="停用" />
            <q-btn flat size="xs" icon="edit" color="secondary" v-if="props.row.Enable==2" style="font-weight:400"
              @click="getStart(props.row,1)" label="启用" />
            <q-btn flat size="xs" icon="edit" color="negative" style="font-weight:400" @click="delOkrModule(props.row)"
              label="删除" />
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" @click="addModule(props.row)"
              label="编辑" />
            </q-btn>
          </q-td>
        </template>
      </q-table>
      <ruleseteditForm v-if="isShowsetForm" @close="closeruleset" :save-obj="ruleObj" @success="refreshRule">
      </ruleseteditForm>
    </div>
  </div>
</template>
<script>
  import {
    GetOKRRulePageList,
    SetOKRRuleState
  } from '../../api/okr/index';
  import ruleseteditForm from '../../components/okr/rulesetedit-form';
  export default {
    meta: {
      title: "规则设置"
    },
    components: {
      ruleseteditForm
    },
    data() {
      return {
        pageCount: 0,
        msg: {
          PageIndex: 1,
          PageSize: 12,
          rowsPerPage: 12,
          RuleName: '', //规则名称
          Enable: 0 //是否启用 1是 2否 0不限
        },
        loading: false,
        isShowsetForm: false,
        ruleObj: {}, //传入参数
        columns: [{
            name: 'RuleName',
            label: '规则名称',
            field: 'RuleName',
            align: 'left'
          },
          {
            name: 'DataBase',
            label: '关联库',
            field: 'DataBase',
            align: 'left',
          },
          {
            name: 'Table',
            label: '关联表',
            field: 'Table',
            align: 'left',
          },
          {
            name: 'Field',
            label: '关键字段',
            field: 'Field',
            align: 'left'
          },
          {
            name: 'Way',
            label: '计算方式',
            field: 'Way',
            align: 'left'
          },
          {
            name: 'Identity',
            label: '身份字段',
            field: 'Identity',
            align: 'left'
          },
          {
            name: 'Time',
            label: '操作时间字段',
            field: 'Time',
            align: 'left'
          },
          {
            name: 'Remark',
            label: '规则备注',
            field: 'Remark',
            align: 'left'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'QuestionId'
          }
        ],
        dataList: [],
      }
    },
    created() {},
    mounted() {
      this.getList();
    },
    methods: {
      changePage(val) {
        this.msg.PageIndex = val;
        this.getList();
      },
      //输入框改变
      research() {
        this.getList();
      },
      //获取数据
      getList() {
        GetOKRRulePageList(this.msg).then(res => {
          if (res.Code == 1) {
            this.dataList = res.Data.PageData;
            this.pageCount = res.Data.PageCount;
          }
        })
      },
      //关闭弹窗
      closeruleset() {
        this.isShowsetForm = false;
      },
      //刷新列表
      refreshRule() {
        this.getList();
      },
      //1启用 2禁用 3删除
      getStart(item, type) {
        let msg = {
          RuleId: item.Id,
          Type: type
        }
        SetOKRRuleState(msg).then(res => {
          if (res.Code == 1) {
            this.getList();
          }
        })
      },
      //删除模板
      delOkrModule(item) {
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          let msg = {
            RuleId: item.Id,
            Type: 3
          }
          SetOKRRuleState(msg).then(res => {
            if (res.Code == 1) {
              this.getList();
            }
          })
        }).onCancel(() => {

        });
      },
      //新增模板吧
      addModule(obj) {
        if (obj) {
          this.ruleObj = obj
        } else {
          this.ruleObj = null
        }
        this.isShowsetForm = true
      }
    }
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass')

</style>
