<template>
  <div class="flex auto">
    <section>
      <div class="flex-row">
        <div class="col-md-6">
          <checker-left v-ref:left></checker-left>
        </div>
        <div class="btn-group-vertical">
          <button class="btn btn-primary" @click="confirm()" >保存</button>
          <button class="btn btn-primary"  @click="addAllRight()">     &gt;     &gt; </button>
          <button class="btn btn-primary" @click="addRight()" >&gt;</button>
          <button class="btn btn-primary" @click="addLeft()" >&lt;</button>
          <button class="btn btn-primary" @click="addAllLeft()"> &lt; &lt; </button>

        </div>
        <div class="col-md-6">
          <checker-right v-ref:right></checker-right>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
  import CheckerLeft from './CheckerLeft'
  import CheckerRight from './CheckerRight'
  import co from 'co'

  // 保存更新内容
  let saveGen = function * (self) {
    self.model.plan_one_id = self.f_plan_id_a
    self.model.plan_two_id = self.f_plan_id_b
    self.model.checker_one_id = self.$refs.left.model.chosenOrg.id
    self.model.checker_two_id = self.$refs.right.model.chosenOrg.id
    self.model.checkerA = []
    for(let i=0; i< self.$refs.left.model.rows.length; i++) {
      let row = self.$refs.left.model.rows[i]
      if(row.sign == '1')
        self.model.checkerA.push(row)
    }
    self.model.checkerB = []
    for(let i=0; i< self.$refs.right.model.rows.length; i++) {
      let row = self.$refs.right.model.rows[i]
      if(row.sign == '1')
        self.model.checkerB.push(row)
    }
    yield self.$resetpost('api/af-safecheck/logic/ChangeCheckplan', {data: self.model}).then((respanse)=>{
      if(respanse.code == 200) {
      window.alert("计划生成成功！")
    }
  })
    self.$refs.left.$refs.paged.$refs.criteria.search()
    self.$refs.right.$refs.paged.$refs.criteria.search()
  }

  let select = function * (self) {
    let getGasman = yield self.$resetpost('/rs/search', {
      source: 'this.getParentByType($department$).getAllChildrens().where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1))',
      userid: self.$login.f.id
    }, {resolveMsg: null, rejectMsg: null})
    console.log('获取安检员', getGasman)
    getGasman.data.forEach((checker)=>{
      self.checkers.push({label:checker.name,value:checker})
    self.checkersid.push({label:checker.name,value:checker.id})
  })
    self.checkersid.push({label:'全部',value:''})
  }

  export default {
    title: '安检计划调整',
    props: ['f'],
    data () {
      return {
        model: {},
        checkers:[],
        checkersid:[],
        leftInfor: {},
        rightInfor: {},
        f_plan_id_a: '',
        f_plan_id_b: ''
      }
    },
    ready () {
      this.$refs.right.$refs.paged.$refs.criteria.model.f_plan_year = this.$refs.left.model.c_plan_year[0]
      this.$refs.right.$refs.paged.$refs.criteria.model.f_plan_month = this.$refs.left.model.c_plan_month[0]

      let gen = select(this)
      return co(gen)
    },
    methods: {
      // 向右边调整
      addRight () {
        if (this.$refs.left.selected !== null) {
          // 为调整的数据添加标记
          if (this.$refs.left.selected.sign === '1') {
            this.$refs.left.selected.sign = null
          } else {
            this.$refs.left.selected.sign = '1'
            this.f_plan_id_a = this.$refs.left.selected.f_plan_id
          }
          // 压入数组中
          if (this.$refs.left.selected !== this.leftInfor) {
            this.$refs.right.model.rows.push(this.$refs.left.selected)
            this.$refs.left.model.rows.$remove(this.$refs.left.selected)
            this.$refs.left.model.count -= 1
            this.$refs.right.model.count += 1
            this.leftInfor = this.$refs.left.selected
          }
        }
      },
      // 向左边调整
      addLeft () {
        // 为调整的数据添加标记
        if (this.$refs.right.selected !== null) {
          if (this.$refs.right.selected.sign === '1') {
            this.$refs.right.selected.sign = null
          } else {
            this.$refs.right.selected.sign = '1'
            this.f_plan_id_b = this.$refs.right.selected.f_plan_id
          }
          // 压入数组中
          if (this.$refs.right.selected !== this.rightInfor) {
            this.$refs.left.model.rows.push(this.$refs.right.selected)
            this.$refs.right.model.rows.$remove(this.$refs.right.selected)
            this.$refs.right.model.count -= 1
            this.$refs.left.model.count += 1
            this.rightInfor = this.$refs.right.selected
          }
        }
      },
      // 全部向右边调整
      addAllRight () {
        let condition = this.$refs.left.$refs.paged.$refs.criteria.condition
        this.$resetpost('api/af-safecheck/sql/根据年月安检员查询安检项?pageSize=9999999',{data:{condition:condition,f_plan_year: this.$refs.left.model.c_plan_year[0], f_plan_month: this.$refs.left.model.c_plan_month[0],f_subcompany:this.$refs.left.model.f_subcompany, f_checker_id: this.$refs.left.model.chosenOrg.id}},-1).then(
          (respanse)=> {
          if (this.$refs.left.model.rows.length != 0) {
          // 压入数组中
          for (let i = 0; i < respanse.data.length; i++) {
            let ret = true
            for (let n = 0; n < this.$refs.right.model.rows.length; n++) {
              if (this.$refs.right.model.rows[n].f_userinfoid == respanse.data[i].f_userinfoid) {
                ret = false
              }
            }
            if (ret == true) {
              respanse.data[i].sign = '1'
              this.$refs.right.model.rows.push(respanse.data[i])
              this.$refs.left.model.count -= 1
              this.$refs.right.model.count += 1
            }
          }
          this.$refs.left.model.rows = []
        }
      })
      },
      // 全部向左边调整
      addAllLeft () {
        console.log(this.$refs.right)
        let condition = this.$refs.right.$refs.paged.$refs.criteria.condition
        this.$resetpost('api/af-safecheck/sql/根据安检员查询安检项?pageSize=9999999',{data:{condition:condition,f_subcompany:this.$refs.right.model.f_subcompany, f_checker_id: this.$refs.right.model.chosenOrg.id}},-1).then(
          (respanse)=>{
          // 为调整的数据添加标记
          if (this.$refs.right.model.rows.length != 0) {
          // 压入数组中
          for(let j=0;j<respanse.data.length;j++) {
            let res = true
            for (let o = 0; o < this.$refs.left.model.rows.length-1; o++) {
              if (respanse.data[j].f_userinfoid == this.$refs.left.model.rows[o].f_userinfoid) {
                res = false
              }
            }
            if (res == true) {
              respanse.data[j].sign = '1'
              this.$refs.left.model.rows.push(respanse.data[j])
              this.$refs.right.model.count -= 1
              this.$refs.left.model.count += 1
            }
          }
        }
        this.$refs.right.model.rows =[]
      }
      )
      },
      confirm () {
        if(!this.f_plan_id_a)
          this.f_plan_id_a = this.$refs.left.model.rows[0].f_plan_id
        if(!this.f_plan_id_b)
          this.f_plan_id_b = this.$refs.right.model.rows[0].f_plan_id
        if(this.f_plan_id_a == this.f_plan_id_b) {
          window.alert('调整计划不能相同！')
          return
        }
        let gen = saveGen(this)
        return co(gen)
      }
    },
    components: { CheckerLeft, CheckerRight }
  }
</script>
