<template>
  <div class="app-container">
        <div style="display:flex">
          <treeselect 
          v-model="listQuery.user__dept" 
          :multiple="false" 
          :options="deptOptions" 
          placeholder="所属组织" 
          :disable-branch-nodes="true" 
          @input="handleFilter" 
          style="width: 280px" clearable/>
          <el-select
            v-model="listQuery.is_onjob"
            placeholder="是否在职"
            clearable
            class="filter-item"
            style="margin-left:10px"
            @change="handleFilter"
          >
            <el-option label="是" value="true"/>
            <el-option label="否" value="false"/>
          </el-select>
          <el-select
            v-model="listQuery.is_fulltime"
            placeholder="是否全职"
            clearable
            class="filter-item"
            style="margin-left:10px"
            @change="handleFilter"
          >
            <el-option label="是" value="true"/>
            <el-option label="否" value="false"/>
          </el-select>
          <el-input
            v-model="listQuery.search"
            placeholder="姓名/易记码/编号/注册领域"
            style="width: 300px;margin-left:10px"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
            style="margin-left:10px"
          >搜索</el-button>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-refresh-left"
            @click="resetFilter"
          >刷新重置</el-button>
        </div>
        <div style="margin-top:6px">
        <el-button type="primary" icon="el-icon-plus" @click="handleCreate" >认证申请</el-button>
        </div>
        <el-table
          v-loading="listLoading"
          :data="tableData.results"
          style="width: 100%;margin-top:10px;"
          border
          fit
          stripe
          highlight-current-row
          max-height="600"
        >
          <el-table-column type="index" width="50" />
          <el-table-column label="申请单号">
            <template slot-scope="scope" v-if="scope.row.number">{{ scope.row.number }}</template>
          </el-table-column>
          <el-table-column label="申请方">
            <template slot-scope="scope">{{ scope.row.applicant_v }}</template>
          </el-table-column>
          <el-table-column label="认证领域">
            <template slot-scope="scope" v-if="scope.row.fields">
            <el-tag v-for="(item, index) in scope.row.fields.split(',')" :key="index" style="margin:2px" >{{item}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="当前状态">
            <template slot-scope="scope"><el-tag>{{ scope.row.status}}</el-tag><el-tag style="margin-left:2px" type="warning">{{ scope.row.stage_}}</el-tag></template>
          </el-table-column>
          <el-table-column label="接收部门">
            <template slot-scope="scope">{{ scope.row.belong_dept}}</template>
          </el-table-column>          
          <el-table-column label="创建日期">
            <template slot-scope="scope">
              <span>{{ scope.row.create_time }}</span>
            </template>
          </el-table-column>
                <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            :disabled="!checkPermission(['application_update'])"
            @click="handleContinue(scope)"
          >继续</el-button>
          <el-button
            type="danger"
            size="small"
            :disabled="!checkPermission(['application_delete'])"
            @click="handleDelete(scope)"
          >删除</el-button>
        </template>
                </el-table-column>
        </el-table>

        <pagination
          v-show="tableData.count>0"
          :total="tableData.count"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.page_size"
          @pagination="getList"
        />
  </div>
</template>
<script>
import { getApplicationList, deleteApplication } from "@/api/application"
import { getOrgList } from "@/api/org"
import { getDictList } from "@/api/dict"
import Pagination from "@/components/Pagination"
import checkPermission from '@/utils/permission'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { genTree } from '../../utils'
export default {
  components: { Pagination, Treeselect },
  data() {
    return {
      tableData: {count:0},
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20
      },
      deptOptions: [],
    };
  },
  created() {
    this.getList();
    this.getdeptOptions()
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getApplicationList(this.listQuery).then(response => {
        if (response.data) {
          this.tableData = response.data
        }
        this.listLoading = false
      });
    },
    getdeptOptions() {
      getOrgList().then(res=>{
        this.deptOptions = genTree(res.data)
      })
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20
      }
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({name:"ApplicationForm"})
    },
    handleContinue(scope) {
      this.$router.push({name:"ApplicationForm",query:{id:scope.row.id}})
    },
    handleDelete(scope) {
      deleteApplication(scope.row.id).then(res=>{
        this.$message.success('成功')
        this.getList()
      })
    }
  }
};
</script>
