<template>
  <div class="flex-row">
    <div class="basic-main">
      <criteria-paged :model="model" v-ref:paged @sort="sort">
        <criteria partial='criteria' class="search_area" @condition-changed='$parent.search' v-ref:criteria>
          <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="row">
              <div class="col-sm-4" style="width: 20%">
                <label class="font_normal_body">计划年份</label>
                <v-select :value.sync="model.f_safecheck_type" v-model='model.f_safecheck_type'
                          :value-single="true" style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.c_plan_year' placeholder='计划年份'
                          close-on-select
                          condition="f_plan_year = '{}'"></v-select>
              </div>
              <div class="col-sm-4" style="width: 20%">
                <label class="font_normal_body" style="margin-right: 2px">计划月份</label>
                <v-select :value.sync="model.f_check_type" v-model='model.f_check_type'
                          :value-single="true" style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.c_plan_month' placeholder='计划月份'
                          close-on-select
                          condition="f_plan_month = '{}'"></v-select>
              </div>
              <div class="col-sm-8" style="width: 40%">
                <role-selector
                  role-name="安检员"
                  role-lable="安&nbsp;检&nbsp;员&nbsp;"
                  @re-res="$parent.$parent.getRes"
                  :value.sync="model.f_checker_id"
                  v-model="model.f_checker_id"
                  condition="f_checker = '{}'">
                </role-selector>
              </div>
              <div class="col-sm-4" style="width: auto;margin-top:8px;float: right">
                <button class="button_export button_spacing" @click='$parent.$parent.exportexcel()'>导出Excel</button>
                <button class="button_spacing button_search" @click="search(),$dispatch('search')">查询</button>
              </div>
            </div>
          </div>
        </criteria>
        <data-grid class="list_area table_sy" style="overflow: hidden" :model="model" partial='list' v-ref:grid id="planid">
          <template partial='head'>
            <tr>
              <th>
                <nobr>序号</nobr>
              </th>
              <th>
                <nobr>计划名称</nobr>
              </th>
              <th>
                <nobr>计划年月</nobr>
              </th>
              <th>
                <nobr>计划类型</nobr>
              </th>
              <th>
                <nobr>安检员</nobr>
              </th>
              <th>
                <nobr>总计划数</nobr>
              </th>
              <th>
                <nobr>已检数</nobr>
              </th>
              <th>
                <nobr>未检数</nobr>
              </th>
              <th>
                <nobr>完成率</nobr>
              </th>
              <th>
                <nobr>创建时间</nobr>
              </th>
              <th>
                <nobr>创建人</nobr>
              </th>
              <th>
                <nobr>下发时间</nobr>
              </th>
              <th>
                <nobr>下发人员</nobr>
              </th>
            </tr>
          </template>
          <template partial='body'>

            <td style="text-align: center">{{$index+1}}</td>
            <td style="text-align: center">{{row.f_plan_name}}</td>
            <td style="text-align: center">{{row.f_plan_year}}-{{row.f_plan_month}}</td>
            <td style="text-align: center">{{row.f_safecheck_type}}</td>
            <td style="text-align: center">{{row.f_checker}}</td>
            <td style="text-align: center">{{row.plannumber?row.plannumber:0 }}</td>
            <td style="text-align: center">{{row.finalnumber?row.finalnumber:0}}</td>
            <td style="text-align: center">{{row.nofinalnumber?row.nofinalnumber:0}}</td>
            <td style="text-align: center"><safe-fin-info :numfin="$parent.$parent.$parent.getnum(row.finalnumber,row.plannumber)"></safe-fin-info></td>

            <td style="text-align: center">{{row.f_create_time}}</td>
            <td style="text-align: center">{{row.f_create_operator}}</td>
            <td style="text-align: center">{{row.f_issue_time?row.f_issue_time:'未下发'}}</td>
            <td style="text-align: center">{{row.f_send_operator}}</td>
            <!--<td style="text-align: center">-->
              <!--<button class="button_spacing button_search-1" @click='$parent.$parent.$parent.change(row)'>查看</button>-->
              <!--<button type="button" class="button_new-1 button_spacing" data-toggle="dropdown"-->
                      <!--@click="$parent.$parent.$parent.print(row)">-->
                <!--打印-->
              <!--</button>-->
            <!--</td>-->
          </template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'
  import Vue from 'vue'
  import co from 'co'
  import XLSX from 'xlsx';
  import AppData from '../../stores/AppData'
  import * as Util from '../Util'
  import getNowDate from "../../utils/getNowDate";
  import HttpResetClass from "vue-client/src/plugins/HttpResetClass";

  //获取json配置文件
  let asyncReady = async function (self) {
    // 获取配置信息
    try {
      let res = await Vue.resetget(`/phone/rs/vue`, {data: {}}, {resolveMsg: null, rejectMsg: null})
      Vue.config = res.data
    } catch (error) {
      // 忽略704,文件找不到异常R
      if (error.status !== 704) {
        throw error
      }
    }
  }

  let select = function* (self) {
    let http = new HttpResetClass()
    let getGasman = yield http.load('POST', '/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)
    self.checkersid.push({label: '全部', value: ''})
    getGasman.data.forEach((checker) => {
      self.checkersid.push({label: checker.name, value: checker.id})
    })
  }

  export default {
    title: '计划情况汇总',
    data() {
      return {
        c_plan_month:[],
        c_plan_year:[],
        f_filialeid:`('${this.$login.f.orgid}')`,
        model: new PagedList('api/af-safecheck/sql/GetPlanSumInfo', 9999),
        excelTitle:'安检结果查看导出'+Util.toStandardTimeString().substring(11,19).replace(new RegExp( ':' , "g" ),""),
        //searchData:searchData,
        // excelHeaders: {
        //   'f_userinfo_code': '客户编号',
        //   'f_user_name': '客户姓名',
        //   'f_user_phone': '客户电话',
        //   'f_check_type': '客户类型',
        //   'f_address': '客户地址',
        //   'f_entry_status': '安检状态',
        //   'f_offsite_time': '安检时间',
        //   'f_checker_name': '安检员'
        // },
        fun: this.$login.f,
        checkersid: [],
        check_types:[
          {label: "民用", value: "民用"},
          {label: "非民用", value: "非民用"}],
        safetype: [
          {label: "散户安检", value: "散户安检"},
          {label: "年度普检", value: "年度普检"},
          {label: "专项计划", value: "专项计划"}],
        criteriaShow: false,
        showModal: false,
        // f_approval_states: this.$appdata.getParam('安检单审核状态'),
        // f_entry_status: this.$appdata.getParam('安检状态'),
        // f_checkplans: this.$appdata.getParam('有无计划'),
        showcheckmsg: false,
        checkInfo: {},
        orderDefault: "'f_offsite_time'",
        orderFields: {
          f_offsite_time: 'no'
        },   label:String,
      }
    },
    methods: {
      exportexcel(){
        console.log("8888888888888888")
// 以上四行也可以直接一行搞定，如果不需要对表格数据进行修改的话
        let workbook = XLSX.utils.table_to_book(document.getElementById('planid'))
        try {
          XLSX.writeFile(workbook, '计划情况汇总导出.xlsx');
        } catch(e) {
          console.log(e, workbook);
        }
      },
      getnum(fin,sum){
        // debugger
        return sum?((fin/sum) * 100).toFixed(0):0
      },
      search(args) {
        // debugger
        var condition = this.$refs.paged.$refs.criteria.condition?this.$refs.paged.$refs.criteria.condition: `1=1`
        if(this.f_filialeid){
          condition += `and f_filialeid in ${this.f_filialeid}`
        }
        // this.searchData.condition = data
        // this.searchData = Object.assign(this.searchData, args.model)
        return this.model.search(condition, args.model)
      },
      getRes(obj) {
        this.f_filialeid = this.$login.convertToIn(obj.resids);
        // this.searchData.f_filialeid = this.$login.convertToIn(obj.resids);
      },
      change(row) {
        //Vue.$login = {jwt: row.f_checker_id}
        asyncReady(this).then(() => {
          row.f_upload_state = '已传'
          this.$goto('new-check-paper', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
        }).catch(
          (respanse) => {
            alert('获取配置文件失败')
          })
      },
      readConfig() {
        let gen = asyncReady(this)
        return co(gen)
      },
      showChooser() {
        this.showModal = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      refresh() {
        this.$refs.paged.$refs.criteria.search()
      },
      ok() {
        this.showModal = false
        if (this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id) {
          this.model.f_plan_id = this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id
        }
      },
      cancel() {
        this.showModal = false
        this.model.f_check_plan = {f_plan_name: '', id: ''}
      },
      print(row) {
        this.showcheckmsg = true
        this.checkInfo = row
      },
      // sort(field, rule) {
      //   // 将所有排序方式设为不排序，实现相互排斥
      //   for (let key in this.orderFields) {
      //     if (key === field) {
      //       this.orderFields[key] = rule
      //     } else {
      //       this.orderFields[key] = 'no'
      //     }
      //   }
      //   // 如果新规则不排序，还原为默认排序
      //   if (rule === 'no') {
      //     this.model.paramSource.orderitem = `'${this.orderDefault}'`
      //   } else {
      //     this.model.paramSource.orderitem = `'${field} ${rule}'`
      //   }
      //   this.search()
      // },

      // selfSearch(args) {
      //   console.log("9999999999999999")
      //   this.model.search(args.condition, args.model, args.condValue)
      // },
      // 获取计划年份、月份
      getYearMonth(){
        var options = []
        var year = (new Date()).getFullYear()
        for(let i = year-2 ; i <= year +2; i++ ){
          options.push({label: i+'', value: i+''})
        }

        this.c_plan_year = options
        var moptions = []
        for(let i=1; i<=12; i++){
          moptions.push({label: i + '', value: i + ''})
        }
        this.c_plan_month = moptions

      }
    },

    ready() {
      this.getYearMonth()
      // this.$refs.paged.$refs.criteria.model.f_check_end=Util.toStandardTimeString().substring(0,10)

      // this.$refs.paged.$refs.criteria.model.f_check_start=Util.getPreMonth(this.$refs.paged.$refs.criteria.model.f_check_end)
      this.function = this.f
      //this.remind()
      let gen = select(this)
      co(gen)
    },
    watch: {
      // 'f_filialeids'(val) {
      //   if (val) {
      //     this.searchData.f_filialeid = val
      //   }
      // },
    }

  }
</script>
