<template>
  <div class="app-container">
    <el-form  size="mini"  :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库类型" prop="warehouseTypeList">
        <el-select   size="mini" v-model="queryParams.warehouseTypeList" placeholder="请选择仓库类型" collapse-tags style="width: 100%" multiple  @keyup.enter.native="handleQuery" clearable>
          <el-option :label="item.dictLabel" :value="item.dictValue" v-for="item in wlwarehousetype"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="treeselectForm" label="组织架构" prop="orgIdsName">
        <tree @treeselectFn="treeselectFn" :treeval="queryParams.deptIds" :ismultiple="true"></tree>
      </el-form-item>
      <el-form-item label="销售员" prop="userId">
        <user :userId="queryParams.userId" :orgIds="queryParams.orgIds" @userIdFn="userIdFn"></user>
      </el-form-item>
<!--      <el-form-item label="部门id" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="部门权限" prop="deptAuth">-->
<!--        <el-select   size="small" v-model="queryParams.deptAuth" placeholder="请选择岗位名称" style="width: 100%" multiple  @keyup.enter.native="handleQuery" clearable>-->
<!--          <el-option label="上级" value="1"></el-option>-->
<!--          <el-option label="本级" value="2"></el-option>-->
<!--          <el-option label="下级" value="3"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="岗位名称" prop="postionNameList">
        <el-select  size="mini"  v-model="queryParams.postionNameList" placeholder="请选择岗位名称" collapse-tags style="width: 100%" multiple  @keyup.enter.native="handleQuery" clearable>
          <el-option :label="item.dictLabel" :value="item.dictLabel" v-for="item in userpostrole"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="岗位反选" prop="postitionReverseSelect">-->
<!--        <el-select   size="small" v-model="queryParams.postitionReverseSelect" placeholder="请选择岗位名称" style="width: 100%" multiple  @keyup.enter.native="handleQuery" clearable>-->
<!--          <el-option label="非反选" value="0"></el-option>-->
<!--          <el-option label="反选" value="1"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sfa:requisition_auth_config:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['sfa:requisition_auth_config:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['sfa:requisition_auth_config:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['sfa:requisition_auth_config:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="requisition_auth_configList" @selection-change="handleSelectionChange">
      <el-table-column label="仓库类型" align="center" prop="authId"  :show-overflow-tooltip="true" width="160px">
        <template slot-scope="scope">
          {{storeCategoryFormatter(scope.row,scope.column)}}
        </template>
      </el-table-column>
      <el-table-column label="部门名称"  prop="deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="部门权限" align="center" prop="deptAuth" width="100px" >
        <template slot-scope="scope">
          <span v-if="scope.row.deptAuth==1">上级</span>
          <span v-if="scope.row.deptAuth==2">本级</span>
          <span v-if="scope.row.deptAuth==3">下級</span>
        </template>
      </el-table-column>
      <el-table-column label="部门级别" align="center" prop="deptLevel" width="100px" >
        <template slot-scope="scope">
          <span v-if="scope.row.deptLevel==1">公司</span>
          <span v-if="scope.row.deptLevel==2">大区</span>
          <span v-if="scope.row.deptLevel==3">销售部</span>
        </template>
      </el-table-column>
      <el-table-column label="岗位名称"  prop="postionName" :show-overflow-tooltip="true"/>
      <el-table-column label="岗位反选" align="center" prop="carShop" width="100px" >
        <template slot-scope="scope">
          <span v-if="scope.row.postionReverseSelect==1">反选</span>
          <span v-if="scope.row.postionReverseSelect==0">非反选</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handlevUpdate(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sfa:requisition_auth_config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:requisition_auth_config:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料领用权限配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form  ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12" style="height: 40px">
            <el-form-item label="仓库类型" prop="warehouseType">
              <el-select v-model="form.warehouseType" placeholder="请选择仓库类型" style="width: 100%"   :disabled="title=='物料领用权限配置'">
                <el-option  v-for="item in wlwarehousetype"
                            :key="item.dictValue"
                            :label="item.dictLabel"
                            :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="height: 40px">
            <el-form-item :class="['treeselectForm treeselectFormsuinibian',{borderRed:!validTree}]" label="部门" prop="deptId" style="width: 100%">
              <treeselect
                class="treePopup"
                v-model="form.deptId"
                :multiple="false"
                value-consists-of="LEAF_PRIORITY"
                :options="menuOptions"
                :normalizer="normalizer1"
                placeholder="选择部门"
                noOptionsText="暂无数据"
                :show-count="true"
                :disabled="title=='物料领用权限配置'"
                @select="treesuinibianFn"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门权限" prop="deptAuth" >
              <el-radio-group v-model="form.deptAuth" :disabled="title=='物料领用权限配置'" @change="deptAuthYQLJ">
                <el-radio label="1">上级</el-radio>
                <el-radio label="2">本级</el-radio>
                <el-radio label="3">下级</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门级别" prop="deptLevel" >
              <el-radio-group v-model="form.deptLevel" :disabled="title=='物料领用权限配置'||form.deptAuth==2">
                <el-radio label="1">公司</el-radio>
                <el-radio label="2">大区</el-radio>
                <el-radio label="3">销售部</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位名称" prop="postionNameList" >
              <el-select v-model="form.postionNameList" placeholder="请选择岗位名称" collapse-tags style="width: 100%" multiple :disabled="title=='物料领用权限配置'">
                <el-option :label="item.dictLabel" :value="item.dictLabel" v-for="item in userpostrole"></el-option>
              </el-select>
              <el-tooltip class="item" effect="dark" :content="form.postionName" placement="top">
                <div class="suiyibianlj" v-if="title=='物料领用权限配置'">{{form.postionName}}</div>
              </el-tooltip>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位反选" prop="postionReverseSelect" >
              <el-radio-group v-model="form.postionReverseSelect" size="mini" :disabled="title=='物料领用权限配置'">
                <el-radio :label="0">非反选</el-radio>
                <el-radio :label="1">反选</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-button
          v-if="title!='物料领用权限配置'"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openFN"
        >添加用户</el-button>
        <br    v-if="title!='物料领用权限配置'">
        <br    v-if="title!='物料领用权限配置'">
        <el-table :data="suibianxieList"  height="380">
          <el-table-column label="销售员" align="center" key="nickName" prop="nickName"  :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="108" />
          <el-table-column label="职位" align="center" key="postName" prop="postName"  :show-overflow-tooltip="true" width="160"/>
          <el-table-column label="部门" align="center" prop="deptName"width="260" :show-overflow-tooltip="true"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width"     v-if="title!='物料领用权限配置'">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handlesuinibianDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"     v-if="title!='物料领用权限配置'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--  Run quickly  -->
    <el-dialog title="销售员信息" :visible.sync="opensuibianxie" width="900px" append-to-body>
      <el-form :model="querysuibinaxieParams" ref="querysuibinaxieParams" :inline="true" label-width="68px">
        <el-form-item class="treeselectForm" label="组织架构" prop="deptIdNames">
          <tree @treeselectFn="treeselectFns" :treeval="querysuibinaxieParams.deptIds" :ismultiple="true"></tree>
        </el-form-item>
        <el-form-item label="销售员" prop="nickName">
          <el-input
            v-model="querysuibinaxieParams.nickName"
            placeholder="请输入销售员"
            clearable
            size="small"
            style="width: 240px"
          />
        </el-form-item>
<!--        <el-form-item label="手机号码" prop="phonenumber">-->
<!--          <el-input-->
<!--            v-model="querysuibinaxieParams.phonenumber"-->
<!--            placeholder="请输入手机号码"-->
<!--            clearable-->
<!--            size="small"-->
<!--            style="width: 240px"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="工号" prop="workCode">-->
<!--          <el-input-->
<!--            v-model="querysuibinaxieParams.workCode"-->
<!--            placeholder="请输入工号"-->
<!--            clearable-->
<!--            size="small"-->
<!--            style="width: 240px"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="职位" prop="postName">-->
<!--          <el-input-->
<!--            v-model="querysuibinaxieParams.postName"-->
<!--            placeholder="请输入职位"-->
<!--            clearable-->
<!--            size="small"-->
<!--            style="width: 240px"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handlesuibianxieQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetsuibianxieQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="userList" @selection-change="handlesuibianxieSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
<!--        <el-table-column label="工号" align="center" key="workCode" prop="workCode" width="134"/>-->
        <el-table-column label="销售员" align="center" key="nickName" prop="nickName"  :show-overflow-tooltip="true" />
<!--        <el-table-column label="角色" align="center" key="roles"  :show-overflow-tooltip="true" width="180">-->
<!--          <template slot-scope="scope">-->
<!--            <span>{{ rolesFormater(scope.row) }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="部门" align="center" key="deptName" v-if="columns[2].visible" :show-overflow-tooltip="true" width="180">-->
<!--          <template slot-scope="scope">-->
<!--            <span>{{ deptNameFormater(scope.row)}}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="108" />
        <el-table-column label="职位" align="center" key="postName" prop="postName"  :show-overflow-tooltip="true" width="160"/>
        <el-table-column label="部门" align="center"  prop="deptName"width="260" :show-overflow-tooltip="true"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handlesuinibianUpdate(scope.row)"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="suibianxietotal>0"
        :total="suibianxietotal"
        :page.sync="querysuibinaxieParams.pageNum"
        :limit.sync="querysuibinaxieParams.pageSize"
        @pagination="getsuibianxieList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitsuibianxieForm">保 存</el-button>
        <el-button @click="opensuibianxie=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRequisition_auth_config,
  getRequisition_auth_config,
  delRequisition_auth_config,
  addRequisition_auth_config,
  updateRequisition_auth_config,
  getUserInfolist
} from "@/api/sfa/requisition_auth_config";
import Treeselect from "@riophae/vue-treeselect";
import {listDept} from "@/api/system/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listUser} from "@/api/system/user";
import tree from "@/views/components/sfa/treeselectParentNo.vue";
import user from "@/views/components/sfa/user.vue";
export default {
  name: "Requisition_auth_config",
  components: {user, tree, Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      opensuibianxie:false,
      suibianxieList:[],
      suibianxieLists:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      validTree:true,
      // 总条数
      total: 0,
      // 物料领用权限配置表格数据
      requisition_auth_configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        deptAuth: null,
        positionName: null,
        postitionReverseSelect: null,
        deptIds:undefined,
        userId: null,
      },
      querysuibinaxieParams:{
        status:0,
        pageNum: 1,
        pageSize: 10,
        deptIds:undefined,
        nickName:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseType:[ { required: true, message: '请选择仓库类型', trigger: 'change' }],
      },
      wlwarehousetype:[],
      menuOptions: [],
      suibianxietotal:0,
      userList:[],
      userpostrole:[]
    };
  },
  created() {
    this.getDicts("wl_warehouse_type").then(response => {
      this.wlwarehousetype = response.data;
    });
    this.getDicts("user_post_role").then(response => {
      this.userpostrole = response.data;
    });
    this.getList();
    this.getTreeselect()
  },
  methods: {
    deptAuthYQLJ(val){
      if(val==2){
        this.form.deptLevel=undefined
      }
    },
    treesuinibianFn(){
      if(this.form.deptId!=undefined){
        this.ids=[]
        this.suibianxieList=[]
        this.suibianxieLists=[]
      }
    },
    handlesuinibianDelete(row){
      var list=[]
      for(var k=0;k<this.suibianxieList.length;k++){
       if(this.suibianxieList[k].userId!=row.userId){
         list.push(this.suibianxieList[k])
       }
      }
      this.suibianxieList=list
    },
    openFN(){
      this.opensuibianxie=true
      this.resetsuibianxieQuery()
    },
    userIdFn(val){
      this.queryParams.userId = val;
    },
    treeselectFn(val){
      this.queryParams.deptIds = val;
    },
    treeselectFns(val){
      this.querysuibinaxieParams.deptIds = val;
    },
    submitsuibianxieForm(){
      this.suibianxieList=this.suibianxieList.concat(this.suibianxieLists)
      this.getsuibianxieList()
    },
    handlesuinibianUpdate(row){
      this.suibianxieList.push(row)
      this.getsuibianxieList()
    },
    handlesuibianxieSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.suibianxieLists=selection
      this.multiple = !selection.length;
    },
    getsuibianxieList() {
      this.loading = true;
      var userIdList=[]
      for(var i=0;i<this.suibianxieList.length;i++){
        userIdList.push(this.suibianxieList[i].userId)
      }
      this.querysuibinaxieParams.userIdList=userIdList;
      // if(this.form.deptId!=null){
      //   this.querysuibinaxieParams.deptIds=[this.form.deptId];
      // }
      getUserInfolist(this.querysuibinaxieParams).then(response => {
          this.userList = response.rows;
          this.suibianxietotal = response.total;
          this.loading = false;
        }
      );
    },
    storeCategoryFormatter(row,column){
      return this.selectDictLabel(this.wlwarehousetype, row.warehouseType);
    },
    normalizer1(node) {
      let that = this;
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
        isDisabled:that.isDisabled
      };
    },
    getTreeselect() {
      listDept().then(response => {
        this.menuOptions = this.handleTree(response.data, "deptId", "parentId");
        if(this.menuOptions.length == 1 && (this.menuOptions[0].children == null || this.menuOptions[0].children == undefined||
          (this.menuOptions[0].children != undefined && this.menuOptions[0].children != null && this.menuOptions[0].children.length == 0))){
          this.form.orgId = this.menuOptions[0].deptId;
        }
      });

    },
    /** 查询物料领用权限配置列表 */
    getList() {
      this.loading = true;
      listRequisition_auth_config(this.queryParams).then(response => {
        this.requisition_auth_configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();

    },
    // 表单重置
    reset() {
      this.form = {
        authId: null,
        deptId: null,
        deptAuth: null,
        positionName: null,
        postitionReverseSelect: null,
        userId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        warehouseType:null,
        postionReverseSelect:0
      };
      this.resetForm("form");
      this.querysuibinaxieParams={
        status:0,
        pageNum: 1,
        pageSize: 10,
        deptIds:undefined,
        nickName:undefined
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handlesuibianxieQuery(){
      this.querysuibinaxieParams.pageNum = 1;
      this.getsuibianxieList();
    },
    resetsuibianxieQuery() {
      this.resetForm("querysuibinaxieParams");
      this.querysuibinaxieParams.deptIds=undefined
      this.handlesuibianxieQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.authId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料领用权限配置";
      this.suibianxieList =[]

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const authId = row.authId || this.ids
      getRequisition_auth_config({authId:authId}).then(response => {
        this.form = response.data;
        if(this.form.userList!=null){
          this.suibianxieList =this.form.userList
        }else{
          this.suibianxieList =[]
        }
        this.open = true;
        this.title = "修改物料领用权限配置";
      });
    },
    handlevUpdate(row) {
      this.reset();
      const authId = row.authId || this.ids
      getRequisition_auth_config({authId:authId}).then(response => {
        this.form = response.data;
        if(this.form.userList!=null){
          this.suibianxieList =this.form.userList
        }else{
          this.suibianxieList =[]
        }
        this.open = true;
        this.title = "物料领用权限配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          var userIdLists=[]
          for(var s=0;s<this.suibianxieList.length;s++){
            userIdLists.push(this.suibianxieList[s].userId)
          }
          this.form.userIdList=userIdLists
          if (this.form.authId != null) {
            updateRequisition_auth_config(this.form).then(response => {
              if(response.code==200){
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addRequisition_auth_config(this.form).then(response => {
              if(response.code==200){
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const authIds = row.authId || this.ids;
      this.$modal.confirm('是否确认删除物料领用仓库为"' + row.warehouseName + '"的数据项？').then(function() {
        return delRequisition_auth_config({authId:authIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/requisition_auth_config/export', {
        ...this.queryParams
      }, `requisition_auth_config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
