<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
    >
      <el-form-item label="任务名称" >
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入部门名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="申请人">
          <el-input
            v-model="queryParams.applyUserName"
            placeholder="请输入申请人"
            clearable
          />
      </el-form-item>
      <el-form-item label="创建日期" >
        <el-date-picker
          v-model="value1"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
        >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
        >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <div class="table">
      <el-button type="primary" style="margin-bottom: 10px" size="small" @click="subimt">批量审核</el-button>
      <el-table
        v-loading="loading"
        :data="deptList"
        row-key="deptId"
        max-height="350px"
        highlight-current-row
        @selection-change="handleSelectionChange"
        :header-cell-class-name="cellClass"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="selection"
          :selectable="selectableStatus"
          width="55">
        </el-table-column>
        <el-table-column
          prop="seriervs"
          label="序号"
          min-width="30"
        ></el-table-column>
        <el-table-column
          prop="taskName"
          label="任务名称"
          min-width="180"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="{row}" >
            <span @click="appNoDidet(row)" style="color: #00afff;cursor: pointer">{{row.taskName}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="申请人"
          align="center"
          prop="applyUserName"
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
        >
        </el-table-column>
        <el-table-column prop="status" label="状态" >
          <template slot-scope="scope">
             <span>待办</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="display: flex;justify-content: flex-end ;margin: 15px 0px 0px 0px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" />
    </div>
    <el-dialog title="确认" :visible.sync="nextAuditVs" append-to-body width="50%" >
      <el-form :model="ruleForm"  :rules="rules" ref="ruleFormdt"  class="demo-ruleForm" >
        <el-row :gutter="24">
          <el-col :span="11" v-if="munlist.currentNode!==10" >
            <el-form-item label="下一岗人员" prop="nextAuditUser" label-width="100" style="display: flex">
              <el-select v-model="ruleForm.nextAuditUser" filterable placeholder="请选择">
                <el-option
                  v-for="item in list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="munlist.currentNode==10">
            <el-form-item label="购买方财务人员" prop="nextAuditUser" style="display: flex"  >
              <el-select v-model="ruleForm.nextAuditUser" filterable placeholder="请选择">
                <el-option
                  v-for="item in list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="munlist.currentNode==10">
            <el-form-item label="销售方财务人员" prop="nextAuditUser" style="display: flex">
              <el-select v-model="ruleForm.nextAuditUser2" filterable placeholder="请选择">
                <el-option
                  v-for="item in list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="display:flex;justify-content: center">
        <el-button type="primary" @click="sub('ruleForm')" >提交</el-button>
      </div>
    </el-dialog>
    <modal ref="modal" @updata="getList"/>
  </div>
</template>

<script>
import {
  todoPage,
} from "@/api/system/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import  moment from 'moment'
import modal from './modal'
import {batchAudit, getUserList, Invoiceaudit} from "@/api/utils";

export default {
  provide() {
    return {
      getList: this.getList
    };
  },
  components:{
    modal,
  },
  data() {
    return {
      templateSelection: null,
      value1:null,
      // 遮罩层
      loading: false,
      deptList:[],
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        taskName: undefined,
        applyUserName: undefined,
        startDate:null,
        endDate:null,
      },
      pageNo:1,
      pageSize:10,
      total:0,
      multipleSelection:null,
      munlist:{},
      rules: {
        nextAuditUser: [
          { required: true, message: '请选择', trigger: 'change' },
        ],
        nextAuditUser2: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
      },
      ruleForm:{
        nextAuditUser:'',
        nextAuditUser2:'',
      },
      nextAuditVs:false,
      list:[],
    };
  },
  created() {
    this.getList();
    this.getUser()
  },
  methods: {
    moment,
    async getUser(){
      let res= await getUserList()
      let data=[]
      res.data.forEach(item=>{
        data.push({
          label:item.userName+'_'+item.nickName,
          value:item.userName
        })
      })
      this.list=data
    },
    sub(){
      this.$refs['ruleFormdt'].validate(async (valid) => {
        if (valid) {
          let list =[]
          this.multipleSelection.forEach(item=>{
            list.push(item.id)
          })
          let data={
            taskIds:list,
            nextAuditUser:this.ruleForm.nextAuditUser,
            nextAuditUser2:this.ruleForm.nextAuditUser2,
          }
          let res = await batchAudit(data)
          if(res.code==200){
            this.$message.success('提交成功')
            this.nextAuditVs=false
            this.getList()
          }else {
            this.$message.error('提交失败')
          }
        } else {
          return false;
        }
      });
    },
    subimt(){
      if(!this.multipleSelection || this.multipleSelection.length<1){
        this.$message.info('请至少选择一条数据')
        return
      }
      if(this.multipleSelection[0].currentNode===12){
        this.$message.info('当前勾选节点不支持批量审核，请单个审核！')
        return
      }
      this.nextAuditVs=true
      this.ruleForm={
        nextAuditUser:'',
        nextAuditUser2:'',
      }
      this.$refs['ruleFormdt'].resetFields();
    },
    //为多选表头添加类名
    cellClass(row){
      if (row.columnIndex === 0) {
        return 'disabledCheck'
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      if(val.length===1){
        this.munlist=val[0]
        this.selectableStatus(val[0])
      }
      if(val.length<=0){
        this.munlist={}
      }
    },
    selectableStatus(row) {
      console.log('666666666666')
      // 返回false禁用多选
      if(Object.keys(this.munlist).length==0){
        console.log('666666666666')
        return true
      }
      if(row.currentNode!==this.munlist.currentNode){
        console.log('-----1------')
        return false
      }else {
        console.log('-----2------')
        return true
      }

      // return true;
    },
    appNoDidet(row){
       this.$refs.modal.handle(row)
    },
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      let data={
        pageNo:this.pageNo,
        pageSize:this.pageSize,
      }
      if(this.value1){
        this.queryParams.startDate=moment(this.value1[0]).format('YYYY-MM-DD')
        this.queryParams.endDate=moment(this.value1[1]).format('YYYY-MM-DD')
      }else {
        this.queryParams.startDate=''
        this.queryParams.endDate=''
      }
      todoPage(this.queryParams,data).then((response) => {
        this.loading = false;
        if(response && response.data && response.data.records){
          this.deptList = response.data.records;
          this.total=response.data.total
          this.deptList.forEach((item,index)=>{
            item.seriervs=index+1
          })
        }else {
          this.loading = false;
        }
      });
    },
    handleSizeChange(val) {
      this.pageSize=val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pageNo=val
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
          taskName: undefined,
          applyUserName: undefined,
          startDate:null,
          endDate:null,
      },
        this.value1=null
    },

  },
};
</script>
<style lang="scss" scoped>
.table {
  height: calc(100% - 80px);
}
::v-deep {
  .el-table {
    height: 100%;
    width: 100%;
    .el-table__body-wrapper {
      width: 100% !important;
      height: calc(100% - 50px) !important; // 表格高度减去表头的高度
      overflow-y: scroll;
    }
  }
}
::v-deep .el-table .disabledCheck .cell .el-checkbox__inner{
  display: none !important;
}
/* 去掉全选按钮 */
::v-deep .el-table .disabledCheck .cell::before{
  content: '';
  text-align: center;
}

</style>
