<template>
  <div class="app-container">
    <el-form :model="queryParams" size="mini" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px" class="zform">
<!--      <el-form-item label="组织架构" prop="orgId">-->
<!--        <el-input-->
<!--          v-model="queryParams.orgId"-->
<!--          placeholder="请输入部门id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item class="treeselectForm" label="组织架构">
        <tree @treeselectFn="treeselectFn" :treeval="queryParams.orgIds" :ismultiple="true"></tree>
      </el-form-item>
<!--      <el-form-item label="任务组id" prop="storeGroupId">-->
<!--        <el-input-->
<!--          v-model="queryParams.storeGroupId"-->
<!--          placeholder="请输入任务组id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="任务" prop="taskId">
        <div style="position:relative;">
        <div @click="addTaskSearch" class="tableSearchInput">
            <p class="text"  v-if="taskName.length==0">请选择任务类型</p>
          <p class="text1" v-if="taskName.length>0"><span style="width: 132px;" class="lableSpan">{{taskName[0]}}</span><span class="lableSpan" v-if="taskName.length>1">+{{taskName.length-1}}</span></p>
        </div>
          <i v-if="taskName.length>0" class="el-icon-circle-close tableSearchInputclear" @click="clearTast"></i>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</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:photo: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:photo: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:photo: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:photo:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="photoList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="组织架构" align="center" prop="orgName" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
<!--      <el-table-column label="任务组id" align="center" prop="storeGroupId" />-->
<!--      <el-table-column label="任务id" align="center" prop="taskId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sfa:photo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:photo: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="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="部门id" prop="orgId">-->
<!--          <el-input v-model="form.orgId" placeholder="请输入部门id" />-->
<!--        </el-form-item>-->
        <el-form-item class="treeselectForm" label="组织架构" prop="orgIds">
          <tree @treeselectFn="treeselectform" :treeval="form.orgIds" :ismultiple="true"></tree>
        </el-form-item>
<!--        <el-form-item label="任务id" prop="taskId" style="display: none">-->
<!--          <el-input v-model="form.taskId" placeholder="请输入任务id" />-->
<!--        </el-form-item>-->
        <div>
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handlephotoAdd"
          >添加任务</el-button><span style="color:red;">(*任务项必填)</span>
        </div>
        <br>
        <el-table v-loading="loading" :data="photoLists">
          <el-table-column label="任务编码" align="center" prop="taskCode" />
          <el-table-column label="任务名称" align="center" prop="taskName" />
          <el-table-column label="是否必填" align="center" prop="isMust">
          <template slot-scope="scope">
            <span v-if="scope.row.isMust==0">是</span>
            <span v-else>否</span>
          </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="60">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="taskDelete(scope.row)"
                class="text-danger"
              >删除</el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="任务列表" :visible.sync="taskShow" width="760px" append-to-body>
      <el-form label-width="80px">
        <el-form-item label="是否选中"   style="height: 37px;" v-if="searchType">
          <el-radio-group size="mini"  v-model="queryTaskParams.checks" @change="isMustChange">
            <el-radio :label="0" >未选中</el-radio>
            <el-radio :label="1" >选中</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-table v-loading="loading" :data="tasksList">
          <el-table-column label="任务编码" align="center" prop="taskCode" />
          <el-table-column label="任务名称" align="center" prop="taskName" />
          <el-table-column label="是否必填" align="center" prop="isMust">
            <template slot-scope="scope">
              <span v-if="scope.row.isMust==0">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="60">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                v-if="queryTaskParams.checks==0"
                @click="addTask(scope.row)"
              >添加</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                v-else
                @click="taskDelete(scope.row)"
                class="text-danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="taskTotal>0"
          :total="taskTotal"
          :page.sync="queryTaskParams.pageNum"
          :limit.sync="queryTaskParams.pageSize"
          @pagination="getTaskList"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="taskShow=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import tree from "@/views/components/sfa/treeselectParent2";
import { listPhoto, getPhoto, delPhoto, addPhoto, updatePhoto,getTasks } from "@/api/sfa/photo";
import user from "@/views/components/sfa/user";

export default {
  name: "Photo",
  components: { tree },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      taskTotal:0,
      taskName:[],
      // 汇报照片审查表格数据
      photoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgIds:[],
        orgId: null,
        storeGroupId: null,
        taskId: null,
      },
      queryTaskParams: {
        pageNum: 1,
        pageSize: 10,
        orgIds:[],
        orgId: null,
        check: 0,
        checks:0,
        taskId:null
      },
      taskShow:false,
      tasksList:[],
      photoLists:[],
      taskArr:[],
      taskAddList:[],
      taskAddListsearch:[],
      // 表单参数
      form: {
        orgIds:[]
      },
      searchType:false,
      // 表单校验
      rules: {
        orgIds:[{ required: true, message: '请选择组织架构', trigger: 'blur' }]
      }
    };
  },
  // watch:{
  //   "form.orgIds": {
  //     immediate: true,
  //     deep: true
  //   },
  //   "queryParams.orgIds": {
  //     immediate: true,
  //     deep: true
  //   },
  // },
  created() {
    this.getList();
  },
  methods: {
    clearTast(){
      this.taskAddList=[]
      this.taskAddListsearch=[]
      this.taskName=[]
    },
    // 删除任务组中的任务项
    taskDelete(row){
      // this.form.sfaTaskList.forEach((item,index)=>{
      //   if(item.taskId == row.taskId){
      //     this.form.sfaTaskList.splice(index,1);
      //     // this.$modal.msgSuccess("删除成功");
      //   }
      // })
      if(this.searchType){
        this.taskAddListsearch.forEach((item,index)=>{
          if(item == row.taskId){
            this.taskAddListsearch.splice(index,1);
            this.taskName.splice(index,1);
          }
        })
      }else{
      this.taskAddList.forEach((item,index)=>{
        if(item == row.taskId){
          this.taskAddList.splice(index,1);
        }
      })}

      this.getTasks(1);
      this.queryTaskParams.check=0
    },
    addTask(row){
      let that = this;
      let task = [];
      row.taskId ? task.push(row) : task = this.taskArr;
      const taskIds = row.taskCode || this.taskIds;
      this.$modal.confirm('是否确认添加选中信息？').then(function() {
        let arr = [];
        task.forEach((item,index)=>{
          if(that.searchType){
            that.taskAddListsearch.push(item.taskId);
            that.taskName.push(item.taskName)
          }else{
            that.taskAddList.push(item.taskId);
          }
          // that.form.sfaTaskList.forEach((rw,ind)=>{
          //   rw.taskId != item.taskId ? arr.push(false): arr.push(true);
          // })
          // arr.indexOf(true) == -1 ? that.form.sfaTaskList.push(item) : that.form.sfaTaskList;
        })

      }).then(() => {
          if(this.searchType){
            this.getTasks()
          }else{
            this.getTasks(0);
            this.getTasks(1);
            this.queryTaskParams.check=0
          }
        that.$modal.msgSuccess("添加成功");
      }).catch(() => {});
    },
    isMustChange(){
      this.queryTaskParams.check=this.queryTaskParams.checks
      if(this.queryTaskParams.check=="0"){
        this.getTasks(0);
      }else{
        this.getTasks(1);
      }
    },
    handlephotoAdd(){
      this.searchType=false
      this.queryTaskParams.check=0
      this.queryTaskParams.pageNum=1
    if(this.form.orgId!=""&&this.form.orgId!=null){
      this.taskShow=true;
      this.getTasks(0)
      }else{
      this.$modal.alert("请选择组织架构");
    }
    },
    addTaskSearch(){
      this.searchType=true
      this.queryTaskParams.pageNum=1
      this.queryTaskParams.check=0
      if(this.queryParams.orgId!=""&&this.queryParams.orgId!=null){
        this.taskShow=true;
        this.getTasks(0)
      }else{
        this.$modal.alert("请选择组织架构");
      }
    },
    getTaskList(){
      if(!this.searchType){
        this.queryTaskParams.taskIds = this.taskAddList;
      }else{
        this.queryTaskParams.taskIds = this.taskAddListsearch;
      }
      getTasks(this.queryTaskParams).then(response => {
        if(this.searchType){
          this.tasksList = response.rows;
          this.taskTotal=response.total
        }else{
          if(this.queryTaskParams.check==0){
            this.tasksList = response.rows;
            this.taskTotal=response.total
          }else{
            this.photoLists = response.rows;
          }
        }
      });
    },
    getTasks(type){
      if(!this.searchType){
        this.queryTaskParams.check=type
        this.queryTaskParams.taskIds = this.taskAddList;
      }else{
        this.queryTaskParams.taskIds = this.taskAddListsearch;
      }

      getTasks(this.queryTaskParams).then(response => {
        if(this.searchType){
          this.tasksList = response.rows;
          this.taskTotal=response.total
        }else{
          if(type==0){
            this.tasksList = response.rows;
            this.taskTotal=response.total
          }else{
            this.photoLists = response.rows;
          }
        }
      });
    },
    treeselectform(val){
      console.log(val)
      this.form.orgIds = val;
      this.form.orgId = val.join(",");
      this.queryTaskParams.orgId= val.join(",");
    },
    treeselectFn(val){
      this.queryParams.orgIds = val;
      this.queryParams.orgId = val.join(",");
      this.queryTaskParams.orgId= val.join(",");
      this.getList();
      // this.getTasks(this.queryParams.orgId)
    },
    /** 查询汇报照片审查列表 */
    getList() {
      this.loading = true;
      listPhoto(this.queryParams).then(response => {
        this.photoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orgId: null,
        storeGroupId: null,
        taskId: null,
        orgIds:[],
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.taskAddList=[]
      this.photoLists=[]
      this.open = true;
      this.title = "添加汇报照片审查";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.queryTaskParams.check=0
      this.searchType=false
      this.form.orgId=row.orgId
      this.queryTaskParams.orgId=row.orgId
      this.form.orgIds=row.orgId.split(",").map(Number)
      this.form.taskId=row.taskId
      this.form.id=row.id
      this.taskAddList=row.taskId.split(",")
      this.getTasks(1)
      this.open = true;
      this.title = "修改汇报照片审查";
    },
    /** 提交按钮 */
    submitForm() {
      var taskId=[]
      for(var i=0;i<this.photoLists.length;i++){
        taskId.push(this.photoLists[i].taskId)
      }
      this.form.taskId=taskId.join(",")
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePhoto(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("修改成功");
                this.searchType=false
              this.open = false;
              this.getList();
              }
            });
          } else {
            addPhoto(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("新增成功");
                this.searchType=false
              this.open = false;
              this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除汇报照片审查编号为"' + ids + '"的数据项？').then(function() {
        return delPhoto(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/photo/export', {
        ...this.queryParams
      }, `photo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.calendarNum{
  position: absolute;
  top: 6px;
  right: 6px;
}
.calendarDay{
  font-size: 16px;
  /*color: #0072C6;*/
  font-weight: bold;
  position: absolute;
  left: 8px;
  bottom: 8px;
}
.tips{
  color: #666;
}
</style>
<style lang="scss">
.dateCell{
  height: 50px;
  padding: 8px;
  box-sizing: border-box;
}
.el-calendar-table td.is-selected{
  background: rgb(64 158 255);
  color: #FFF;
}
.el-calendar-table td.is-today{
  /*color: #FFF;*/
}
.el-calendar-table .el-calendar-day{
  padding: 0;
}
.el-calendar-table .el-calendar-day:hover {
  cursor: pointer;
  background-color: rgb(64 158 255);
  color: #FFF;
}
</style>
<style>
.tableSearchInput{
  color:#039be5;border: 1px solid #ccc;border-radius:5px;width: 192px;
  padding: 0 10px;}
.tableSearchInput .text{
  color: #ccc;font-size: 12px;
  margin: 0;
  padding-left: 6px;
}
.tableSearchInput .text1{
  font-size: 12px;
  margin: 0;
  padding-left: 0px;
  line-height: 12px;
  padding: 4px 0;
}
.tableSearchInput .lableSpan{
  background-color: #e3f2fd;display: inline-block;
  padding: 0px 4px;
  border-radius: 2px;
  margin-right: 2px;
  font-size: 12px;
  line-height: 18px;
  overflow:hidden; /*超出的部分隐藏起来。*/
  white-space:nowrap;/*不显示的地方用省略号...代替*/
  text-overflow:ellipsis;/* 支持 IE */
}
.searchIndex .el-input--suffix .el-input__inner{padding-right: 20px;}
.tableSearchInputclear{position: absolute;right: 8px;top:8px;}
</style>
