<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="登记人	">
              <j-input placeholder="请输入登记人	" v-model="queryParam.operator"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="登记部门">
              <j-input placeholder="请输入登记部门" v-model="queryParam.operatorDept"></j-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="登记日期">
                <j-date placeholder="请选择登记日期" v-model="queryParam.operateDate" style="width: 100%;"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="姓名">
                <j-input placeholder="请输入姓名" v-model="queryParam.realname"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="用户账号">
                <j-input placeholder="请输入用户账号" v-model="queryParam.username"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="性别">
                <a-select v-model="queryParam.gender" placeholder="请选择性别">
                  <a-select-option :value="1">男</a-select-option>
                  <a-select-option :value="2">女</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="职务">
              <j-select-position placeholder="请选择职务" :multiple="false" v-model="queryParam.post" />

              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="职级">
                <j-input placeholder="请输入职级" v-model="queryParam.postLevel"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="岗位类型">
                <a-select v-model="queryParam.positionType" placeholder="请选择岗位类型">
                  <a-select-option value="管理类">管理类</a-select-option>
                  <a-select-option value="专技类">专技类</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="部门">
                <j-select-depart v-model="queryParam.departIds" :trigger-change="true"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="showUserSelection" type="primary" icon="plus">添加员工</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('入职登记')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" -->


      <a-table
        ref="table"
        size="middle"
        :scroll="{x:1800}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)"  v-if="record.status==1" >编辑</a>
          <a-divider type="vertical"  v-if="record.status==2" />
          <a href="javascript:;"  v-if="record.status==2" @click="handleDetail(record,'detail')">详情</a>
          <a-divider type="vertical"  v-if="record.status==1" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)"  v-if="record.status==1">
              <a  v-if="record.status==1">删除</a>
          </a-popconfirm>
          <a-divider type="vertical"  v-if="record.status==1" />
          <a-popconfirm title="确定提交吗?" @confirm="() => handleSubmit(record.id)" v-if="record.status==1">
                  <a v-if="record.status==1">提交</a>
                </a-popconfirm>
          <!-- <a-divider type="vertical" /> -->
          <!-- <a-dropdown  v-if="record.status==1" >
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定提交吗?" @confirm="() => handleSubmit(record.id)">
                  <a>提交</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>

    <enrollment-modal ref="modalForm" @ok="modalFormOk"></enrollment-modal>

    
    <a-modal title="新增员工选择" :width="450" :visible="newUserVisible" :closable="false" :maskClosable="false">
      <template slot="footer">
        <a-button type="primary" @click="handleAddUser">确认</a-button>
        <a-button type="primary" @click="newUserVisible = false">取消</a-button>
      </template>

      <a-form>
        <a-form-item
          :labelCol="{ span: 4 }"
          :wrapperCol="{ span: 20 }"
          style="margin-bottom:10px"
        >
          <!-- <a-tooltip placement="topLeft">
            <template slot="title">
              <span>请选择最新添加的用户</span>
            </template>
            <a-avatar style="backgroundColor:#87d068" icon="gold" />
          </a-tooltip> -->
          <a-select
            @change="selectUser"
            placeholder="请选择需要添加的员工"
            style="margin-left:10px;width: 80%"
          >
            <a-icon slot="suffixIcon" type="gold" />
            <a-select-option v-for="d in newUserList" :key="d.user_id" :value="d.user_id">
              {{ d.user_cn_name }}  -- {{ d.dept_cn_name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {putAction,getFileAccessHttpUrl,getAction} from '@/api/manage';
  import EnrollmentModal from './modules/EnrollmentModal'
  import JDate from '@/components/jeecg/JDate.vue'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import JInput from '@/components/jeecg/JInput'

  export default {
    name: "EnrollmentList",
    mixins:[JeecgListMixin],
    components: {
      JDate,
      JInput,
      EnrollmentModal,
      JSelectDepart,
      JDictSelectTag,
      JSelectPosition
    },
    data () {
      return {
        description: '入职登记管理页面',
        newUserVisible: false,
        newUserList: [],
        userSelected:{},
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'部门',
            align:"center",
            dataIndex: 'departIds_dictText'
          },
          {
            title:'姓名',
            align:"center",
            dataIndex: 'realname'
          },
          {
            title:'用户账号',
            align:"center",
            dataIndex: 'username'
          },
          {
            title:'入职日期',
            align:"center",
            dataIndex: 'joinDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'职务',
            align:"center",
            dataIndex: 'post'
          },
          {
            title:'职级',
            align:"center",
            dataIndex: 'postLevel'
          },
          {
            title:'岗位类型',
            align:"center",
            dataIndex: 'positionType'
          },
          {
            title:'手机号码',
            align:"center",
            dataIndex: 'mobile'
          },
          {
            title:'座机',
            align:"center",
            dataIndex: 'phone'
          },
          {
            title:'联系地址',
            align:"center",
            dataIndex: 'address'
          },
          {
            title:'合同起始日期',
            align:"center",
            dataIndex: 'contractStart',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'合同终止日期',
            align:"center",
            dataIndex: 'contractEnd',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'登记人	',
            align:"center",
            dataIndex: 'operator'
          },
          {
            title:'登记部门',
            align:"center",
            dataIndex: 'operatorDept'
          },
          {
            title:'登记日期',
            align:"center",
            dataIndex: 'operateDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:150,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/oa/enrollment/list",
          delete: "/oa/enrollment/delete",
          submit: "/oa/enrollment/submit",
          deleteBatch: "/oa/enrollment/deleteBatch",
          exportXlsUrl: "/oa/enrollment/exportXls",
          importExcelUrl: "oa/enrollment/importExcel",
        },
        dictOptions:{},
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
       
      showUserSelection(){
        getAction(`/sys/user/recently/added`)
          .then(res => {
            if (res.success) {
              this.newUserList = res.result
              // this.newUserList = [
              //   {
              //   "user_id": "zhangds5",
              //   "user_cn_name": "张大山",
              //   "active_state": "1",
              //   "update_time": "2022-12-22 10:35:55",
              //   "dept_code": "c01",
              //   "dept_cn_name": "办公室",
              //   "contact": "",
              //   "email": "",
              //   "registry_date": "2022-12-19 10:35:55"
              //   }
              // ]
              this.newUserVisible = true;
            } else {
              this.$message.error(res.message)
              this.newUserVisible = false;
            }
          })
          .catch(() => {
            this.newUserVisible = false;
          })
      },
      selectUser (value) {
        this.newUserList.forEach(u=>{
          if(u.user_id == value)
          this.userSelected = u;
        })
      },
      handleAddUser(){
        console.log(this.userSelected);
        if(this.userSelected.user_id){
          let u = this.userSelected;
          let user = {
            username : u.user_id,
            realname: u.user_cn_name,
          }
          this.handleEdit(user,'新增');
          this.newUserVisible = false;
        }else{
          this.$message.warning('请先选择员工')
        }
      },
      handleSubmit(id){
        let that = this;
        putAction(this.url.submit,{"id":id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
            that.onClearSelected();
          } else {
            
            console.log(res.message);
            that.$message.warning("提交失败");
          }
        }).error((err)=>{
          console.log(res.message);
          that.$message.error("提交失败");
        })
      }
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  /deep/ .table-page-search-wrapper .ant-form-inline .ant-form-item>.ant-form-item-label{
    width: 26% ;
  }
</style>