<template>
  <div class="page">
    <el-form v-if="searchVisible" :inline="true" class="query-form m-b-10"  ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!-- 搜索框-->
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="searchForm.projectName" placeholder="请输入项目名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="项目编号" prop="projectNumber">
        <el-input 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()" icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="jp-table top" style="">
      <vxe-toolbar :refresh="{query: refreshList}"  ref="toolbarRef" custom>
        <template #buttons>
          <!--          <el-button v-if="hasPermission('cwProjectRecords:add')" type="primary" icon="el-icon-plus" @click="add()">新建</el-button>-->
          <!--          <el-button v-if="hasPermission('klg:question:del')" type="danger" icon="el-icon-delete" @click="del()" :disabled="$refs.questionsTable && $refs.questionsTable.getCheckboxRecords().length === 0" plain>删除</el-button>-->
        </template>
		  <template #tools>
			  <vxe-button
				  text
				  type="primary"
				  :title="searchVisible ? '收起检索' : '展开检索'"
				  icon="vxe-icon-search"
				  class="tool-btn"
				  @click="searchVisible = !searchVisible"
			  ></vxe-button>
		  </template>
      </vxe-toolbar>
      <div style="height: calc(100% - 90px)">
        <vxe-table
          border="inner"
          auto-resize
          resizable
          height="auto"
          :loading="loading"
          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="60" ></vxe-column>-->
          <vxe-column min-width="230" align="center" title="项目名称" field="projectName">
            <template #default="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="150" align="center" title="项目编号" field="projectNumber"></vxe-column>
          <vxe-column min-width="230" align="center" title="合同名称" field="contractName">
            <template #default="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="80" align="center" title="成员数量" field="memberCount"></vxe-column>
          <vxe-column min-width="80" align="center" title="创建人" field="createBy.name"></vxe-column>
          <vxe-column min-width="120" align="center" title="创建时间" field="createTime"></vxe-column>
          <vxe-column title="操作" width="70px" fixed="right" align="center">
            <template  #default="scope">
<!--              项目创建人、项目组成员、管理员可编辑-->
<!--              <el-button v-if="hasPermission('cwProjectRecords:edit')&&(scope.row.createBy.id === $store.state.user.id||isAdmin||haveProjectIds.includes(scope.row.id))" type="text" @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
              )" text type="primary"  size="small" @click="edit(scope.row.id)">编辑</el-button>

            </template>
          </vxe-column>
        </vxe-table>
        <vxe-pager
          background
          :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 ProjectRecordsForm from './ProjectRecordsForm'
  import userService from '@/api/sys/UserService'
  import ContractNameForm from '../workContract/ContractNameForm'
  import ProjectRecordsAddForm from './ProjectRecordsAddForm'
  import ProjectMembersEditForm from './ProjectMembersEditForm'
  export default {
    data () {
      return {
		  searchVisible: true,
        searchForm: {
          projectMasterName: '',
          projectNumber: '',
          projectName: '',
          createDates: [],
          status: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false,
        processDefinitionAuditId: '',
        procDefAuditKey: '',
        isAdmin: false,
        haveProjectIds: ''
      }
    },
    created () {
    },
    components: {
      ProjectRecordsForm,
      ContractNameForm,
      ProjectRecordsAddForm,
      ProjectMembersEditForm
    },
    computed: {
      userName () {
        return this.$store.state.user.name
      },
      user () {
        this.createName = this.$store.state.user.name
        return this.$store.state.user
      }
    },
    mounted () {
		this.$nextTick(() => {
			//将表格和工具栏进行关联
			const $table = this.$refs.projectTable;
			const $toolbar = this.$refs.toolbarRef;
			$table.connect($toolbar);
		})
      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 () {
        userService.is().then((data) => {
          this.isAdmin = data
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        projectRecordsService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then((data) => {
          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>
