<template>
  <div class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">客户名称</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_user_name" placeholder='客户名称'
                     condition="f_user_name = '{}'">
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">用户编号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_userinfo_code"
                     placeholder='用户编号'
                     condition="f_userinfo_code like '%{}%'">
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">打印类型</label>
              <v-select :value.sync="model.f_print_dh"
                        v-model="model.f_print_dh"
                        :disabled="false"
                        :value-single="true"
                        condition="f_print_dh like '%{}%'"
                        :options='$parent.$parent.dhoptions' placeholder='打印类型'
                        close-on-select></v-select>
            </div>

            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">打印状态</label>
              <v-select :value.sync="model.f_print_state"
                        v-model="model.f_print_state"
                        :disabled="false"
                        :value-single="true"
                        condition="f_print_state = '{}'"
                        :options='$parent.$parent.dhoptionss' placeholder='打印状态'
                        close-on-select></v-select>
            </div>


            <div class="span" style="float:right;">
              <button class="button_search" @click="$parent.$parent.search()" v-el:cx>查询</button>
              <button class="button_clear button_spacing" @click="clear">清空</button>
              <export-excel
                :data="{condition: $parent.$parent.condition}"
                :field="$parent.$parent.getfield"
                sqlurl="rs/logic/saleExport" progress="saleGetExportProgress" sql-name="showPrintList" template-name='打印数据导出'
                :choose-col="true">
              </export-excel>
              <div style="float: right" class="button_spacing"
                   :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                   @click="$parent.$parent.hidden()"></div>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">组织机构</label>
              <res-select restype='organization'
                          class="select select_list"
                          style="width: 60%"
                          @res-select="$parent.$parent.getorg"
                          :initresid='$parent.$parent.curorgid'>
              </res-select>
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">地址信息</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_address"
                     placeholder='地址信息'
                     condition="f_address like '%{}%'">
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">打印编号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_print_id_number"
                     placeholder='打印编号'
                     condition="f_print_id_number like '%{}%'">
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">用户类型</label>
              <v-select :value.sync="model.f_user_type"
                        :options='$parent.$parent.usertypes' placeholder='请选择'
                        v-model="model.f_user_type"
                        condition="f_user_type = '{}'"
                        close-on-select></v-select>
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">开始日期</label>
              <datepicker id="startDate" placeholder=""
                          v-model="model.f_start_date"
                          :value.sync="model.f_start_date"
                          :disabled-days-of-Week="[]"
                          style="width: 60%"
                          :format="'yyyy-MM-dd 00:00:00'"
                          condition="f_operate_date >= '{}'">
              </datepicker>
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">结束日期</label>
              <datepicker id="endDate" placeholder=""
                          v-model="model.f_end_date"
                          :value.sync="model.f_end_date"
                          style="width: 60%"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd 23:59:59'"
                          condition="f_operate_date <= '{}'">
              </datepicker>
            </div>
            <div :class="$parent.$parent.styles">
              <label class="font_normal_body">记录状态</label>
              <v-select :value.sync="model.f_state" :value-single="true"
                        :options='$parent.$parent.states' placeholder='请选择'
                        v-model="model.f_state"
                        condition="f_state = '{}'"
                        close-on-select></v-select>
            </div>
          </div>
        </div>

      </criteria>
      <data-grid :model="model" partial='list' class="list_area table_sy" v-ref:grid>
        <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 v-if="!$parent.$parent.$parent.isOper">
              <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_print_id_number}}</td>
          <td style="text-align: center;">{{row.f_userinfo_code}}</td>
          <td style="text-align: center;">{{row.f_user_name}}</td>
          <td style="text-align: center;">{{row.f_user_phone}}</td>
          <td style="text-align: center;" v-if="!$parent.$parent.$parent.isOper">{{row.f_address_detail ? row.f_address
            + row.f_address_detail : row.f_address}}
          </td>
          <td style="text-align: center;">{{row.f_user_type}}</td>
          <td style="text-align: center;">{{row.f_user_state}}</td>
          <td style="text-align: center;">{{row.f_printdate}}</td>
          <td style="text-align: center;">{{row.f_operate_date}}</td>
          <td style="text-align: center;">
            <button v-show="row.f_print_dh ==='受理单' && row.f_print_state=='未打印' && row.f_state == '有效'" class="btn button_export"
                    style="border:none;background:rgba(0,0,0,0)" @click="$parent.$parent.$parent.downFiles(1,row)"
                    v-if="this.$login.r.includes('受理单打印')">受理单打印
            </button>
            <button disabled v-show="row.f_print_dh ===null" class="btn button_export"
                    style="border:none;background:rgba(0,0,0,0)">未绑定打印单
            </button>
            <button disabled v-show="row.f_print_state=='已打印' || row.f_print_state=='是' ||  row.f_print_state=='否'"
                    class="btn button_export" style="border:none;background:rgba(0,0,0,0)">{{row.f_type}}已打印
            </button>
          </td>
          <td style="text-align: center;">
            {{row.f_print_state}}
          </td>
          <td style="text-align: center;">
            {{row.f_state}}
          </td>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
    <user-flow-sheet :show="printflag" :bill-config='configs' :bill-data='billData' :data='newdata' v-on:toggle="close"
                     @printok="printok" v-ref:printbill></user-flow-sheet>
  </div>

</template>

<script>
  import {PagedList} from 'vue-client'
  export default {
    data () {
      return {
        styles: 'col-sm-2 form-grop',
        model: new PagedList('rs/sql/showPrintList', 30, {orderitem: '"f_userinfo_id DESC"'}),
        show: false,
        // 公司下拉
        curorgid: [this.$login.f.orgid],
        row: {},
        f_orgid: '',
        condition: '',
        criteriaShow: false,
        billData: {
          url: 'rs/report/userignition',
          bill: ''
        },
        configs: {
          hasPrint: false,  // 默认打票
          hasBillManage: false  // 默认不启用发票管理
        },
        printflag: false,
        getfield: {},
        newdata: {},
        f_number: 0,
        dhoptions: [{label: '全部', value: ''}, ...this.$appdata.getParam('建档打印类型')],
        dhoptionss: [{label: '全部', value: ''}, {label: '未打印', value: '未打印'}, {label: '已打印', value: '已打印'}],
        states: [{label: '全部', value: ''}, {label: '有效', value: '有效'}, {label: '无效', value: '无效'}],
        usertypes: this.$appdata.getParam('用户类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')] : [],
        config: {
          excelHeaders: {
            'f_print_id_number': '打印编号',
            'f_userinfo_code': '客户编号',
            'f_user_name': '客户名称',
            'f_user_phone': '客户电话',
            'f_address': '地址信息',
            'f_user_type': '用户类型',
            'f_user_state': '用户状态',
            'f_print_dh': '打印类型',
            'f_print_state': '打印状态',
            'f_state': '记录状态'
          }
        }
      }
    },
    props: {
      orderitem: {
        type: String,
        default: 'id desc'
      },
      row: {},
      // config: {},
      ispartial: false,

      f_filialeid: {
        type: String
      }
    },
    ready () {
      this.getfield = this.config.excelHeaders
      this.$refs.paged.$refs.cri.model.f_state = '有效'
      this.search()
    },
    methods: {
      close () {
        this.printflag = false
        this.clean()
      },
      clean () {
        this.search()
      },
      async downFiles (index, row) {
        // 如果有流程单id则直接使用，如果没有则重新获取
        if (!row.f_print_id) {
          let param = {
            id: row.id,
            f_user_type: row.f_user_type,
            f_print_dh: row.f_print_dh,
            f_userinfo_id: row.f_userinfo_id,
            f_orgid: this.$login.f.orgid
          }
          this.$resetpost('rs/logic/getDectirNum', {data: param}, {rejectMsg: '获取流程编号失败'}).then((res) => {
            if (res.data) {
              if (res.data != '') {
                row.f_print_id = res.data
                row.f_print_id_number = res.data
                let temp = res.data + ''
                this.f_number = temp.substring(9)
                let url = ''

                if (index == 1) {
                  url = 'rs/report/userslinstall'
                  this.billData.url = url
                }
                row.f_name = this.$login.f.name
                this.newdata = row
                this.printflag = true
              } else {
                this.$showAlert('获取流程编号失败，请检查用户数据是否完整！', 'warning', 2000)
                this.close()
              }
            }
          })
        } else {
          let url = ''

          if (index == 1) {
            url = 'rs/report/userslinstall'
            this.billData.url = url
          }
          row.f_name = this.$login.f.name
          this.newdata = row
          this.printflag = true
        }
      },
      hidden () {
        this.criteriaShow = !this.criteriaShow
      },
      showhistory (row) {
        this.show = true
        this.row = row
      },
      operate (id, pricename) {
        this.$parent.usershow = true
        this.$parent.priceid = id
        this.$parent.pricename = pricename
      },
      search () {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('search')
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeid = val[0]
      },
      selfSearch (args) {
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        this.$refs.paged.$refs.grid.$el.scrollTop = 0
        args.condition = `${args.condition} and f_filialeid in ${this.f_orgid}`
        this.condition = args.condition
        this.model.search(args.condition, args.model)
      },
      add () {
        this.$dispatch('add')
      },
      printok (name, val) {
        this.confirmprintState(val)
        this.close()
      },
      confirmprintState (row) {
        console.log('===>' + JSON.stringify(row))
        let temp = {
          id: row.id,
          f_print_state: row.f_print_state,
          f_type: row.f_type,
          f_userinfo_id: row.f_userinfo_id,
          f_print_date: this.$login.toStandardTimeString(),
          f_state: '打印',
          f_number: row.f_number != null ? row.f_number : this.f_number
        }
        if (row.f_print_state == '否') {
          temp.f_state = '撤销'
        }
        if (row.f_print_state == '重置') {
          temp.f_state = '重置'
        }
        this.$resetpost('rs/logic/updatePrintSheet', {data: temp},
          {resolveMsg: null, rejectMsg: null}
        ).then((res) => {
          if (res.data && res.data.state) {
            if (res.data.state == 200) {
              this.$showAlert(res.data.msg, 'success', 2000)
            } else if (res.data.state == 500) {
              this.$showAlert(res.data.msg, 'warning', 2000)
            }
            this.close()
          }
        })
      }
    }
  }
</script>
<style scoped>
  .p1 {
    color: #28dc1f;
    font-weight: bold
  }

  .p {
    color: #5f37ff;
  }
</style>
