<template>
  <div class="page">
    <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!-- 搜索框-->
      <el-form-item label="项目名称" prop="projectName">
        <el-input size="small" v-model="searchForm.projectName" placeholder="请输入项目名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="项目编号" prop="projectNumber">
        <el-input size="small" v-model="searchForm.projectNumber" placeholder="请输入项目编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDates">
        <el-date-picker
          placement="bottom-start"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="searchForm.createDates"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="bg-white top" style="">
      <vxe-toolbar :refresh="{query: refreshList}" custom>
        <template #buttons>
          <!--          <el-button v-if="hasPermission('cwProjectRecords:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button>-->
          <!--          <el-button v-if="hasPermission('klg:question:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()" :disabled="$refs.questionsTable && $refs.questionsTable.getCheckboxRecords().length === 0" plain>删除</el-button>-->
        </template>
      </vxe-toolbar>
      <div style="height: calc(100% - 90px)">
        <vxe-table
          border="inner"
          auto-resize
          resizable
          height="auto"
          :loading="loading"
          size="small"
          ref="projectTable"
          show-header-overflow
          show-overflow
          highlight-hover-row
          :menu-config="{}"
          @sort-change="sortChangeHandle"
          :sort-config="{remote:true}"
          :data="dataList"
          :checkbox-config="{}">
          <vxe-column type="seq" width="60" title="序号"></vxe-column>
          <vxe-column type="checkbox" width="40" ></vxe-column>
          <vxe-column min-width="230" align="center" title="项目名称" field="projectName">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('cwProjectRecords:view')" @click="view(scope.row.id)">{{scope.row.projectName}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('cwProjectRecords:view')"  @click="view(scope.row.id,)">{{scope.row.projectName}}</el-link>
              <span v-else>{{scope.row.projectName}}</span>
            </template>
          </vxe-column>
          <vxe-column min-width="160" align="center" title="项目编号" field="projectNumber"></vxe-column>
          <vxe-column min-width="230" align="center" title="合同名称" field="contractName">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="commonJS.isNotEmpty(scope.row.contractId)&&commonJS.isNotEmpty(scope.row.contractName)" @click="viewContract(scope.row.contractId)">{{scope.row.contractName}}</el-link>
              <span v-else style="color: red">暂无合同</span>
            </template>
          </vxe-column>
          <vxe-column min-width="160" align="center" title="委托方" field="clientContactsName"></vxe-column>
          <vxe-column min-width="160" align="center" title="成员数量" field="memberCount"></vxe-column>
          <vxe-column min-width="160" align="center" title="创建人" field="createBy.name"></vxe-column>
          <vxe-column min-width="160" align="center" title="创建时间" field="createDate"></vxe-column>
          <vxe-column title="操作" width="70px" fixed="right" align="center">
            <template  slot-scope="scope">
<!--              项目创建人、项目组成员、管理员可编辑-->
<!--              <el-button v-if="hasPermission('cwProjectRecords:edit')&&(scope.row.createBy.id === $store.state.user.id||isAdmin||haveProjectIds.includes(scope.row.id))" type="text"  size="small" @click="edit(scope.row.id)">编辑</el-button>-->
<!--              项目创建人、管理员、项目现场负责人、项目经理1、项目经理2可编辑-->
              <el-button v-if="hasPermission('cwProjectRecords:edit')&&
              (
              isAdmin ||
              $store.state.user.id === scope.row.createBy.id ||
              $store.state.user.id === scope.row.projectLeaderId ||
              $store.state.user.id === scope.row.projectMasterId ||
              $store.state.user.id === scope.row.realHeader
              )"type="text"  size="small" @click="edit(scope.row.id)">编辑</el-button>

            </template>
          </vxe-column>
        </vxe-table>
        <vxe-pager
          background
          size="small"
          :current-page="tablePage.currentPage"
          :page-size="tablePage.pageSize"
          :total="tablePage.total"
          :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="currentChangeHandle">
        </vxe-pager>
      </div>
    </div>
    <ProjectRecordsForm ref="projectRecordsForm" @refreshList="refreshList"></ProjectRecordsForm>
    <ContractNameForm ref="contractNameForm"></ContractNameForm>
    <ProjectRecordsAddForm ref="projectRecordsAddForm" @refreshList="refreshList"></ProjectRecordsAddForm>
    <ProjectMembersEditForm ref="projectMembersEditForm" @refreshList="refreshList"></ProjectMembersEditForm>
  </div>
</template>

<script>
  import ProjectRecordsService from '@/api/cw/projectRecords/ProjectRecordsService'
  import TaskService from '@/api/flowable/TaskService'
  import ProcessService from '@/api/flowable/ProcessService'
  import ProjectRecordsForm from './ProjectRecordsForm'
  import UserService from '@/api/sys/UserService'
  import UserSelect from '@/components/userSelect'
  import ContractNameForm from '../workContract/ContractNameForm'
  import ProjectRecordsAddForm from './ProjectRecordsAddForm'
  import ProjectMembersEditForm from './ProjectMembersEditForm'
  export default {
    data () {
      return {
        searchForm: {
          projectMasterName: '',
          projectNumber: '',
          projectName: '',
          createDates: [],
          status: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false,
        processDefinitionAuditId: '',
        procDefAuditKey: '',
        isAdmin: false,
        haveProjectIds: ''
      }
    },
    projectRecordsService: null,
    taskService: null,
    processService: null,
    userService: null,
    created () {
      this.projectRecordsService = new ProjectRecordsService()
      this.taskService = new TaskService()
      this.processService = new ProcessService()
      this.userService = new UserService()
    },
    components: {
      ProjectRecordsForm,
      UserSelect,
      ContractNameForm,
      ProjectRecordsAddForm,
      ProjectMembersEditForm
    },
    computed: {
      userName () {
        return JSON.parse(localStorage.getItem('user')).name
      },
      user () {
        this.createName = JSON.parse(localStorage.getItem('user')).name
        return JSON.parse(localStorage.getItem('user'))
      }
    },
    mounted () {
      this.refreshList()
    },
    activated () {
      this.refreshList()
    },
    methods: {
      // 新增
      add () {
        this.$refs.projectRecordsAddForm.init('add', 'false')
      },
      // 修改
      edit (id) {
        id = id || this.$refs.projectTable.getCheckboxRecords().map(item => {
          return item.id
        })[0]
        this.$refs.projectMembersEditForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.projectRecordsForm.init('view', id)
      },
      viewContract (id) {
        this.$refs.contractNameForm.init('view', id)
      },
      // 查询当前用户是否是管理员用户
      checkIsAdmin () {
        this.userService.is().then(({data}) => {
          this.isAdmin = data
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.projectRecordsService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then(({data}) => {
          this.projectRecordsService.getHaveProjectIds().then(({data}) => {
            if (this.commonJS.isNotEmpty(data)) {
              this.haveProjectIds = data.map(i => {
                if (this.commonJS.isNotEmpty(i)) {
                  return i
                }
              }).join(',')
            }
          })
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
        this.checkIsAdmin()
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
