<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :operation="operation()"
    :optionsEvensGroup="optionsEvensGroup"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
  >
    <!-- 密码记录 -->
    <el-dialog
      title="密码记录"
      :visible.sync="isShowDialog"
      :show-close="false"
      width="50%"
      :close-on-click-modal="false"
    >
      <div class="table" style="margin: 10px 0 20px">
        <el-table
          v-loading="dialogTable_listLoading"
          :data="dialogTable_dataList"
          element-loading-text="Loading"
          tooltip-effect="dark"
          style="width: 100%"
          max-height="270"
        >
          <el-table-column align="center" label="序号" type="index" width="50" />
          <el-table-column align="center" prop="printPassword" label="密码" />
          <el-table-column align="center" prop="createTime" label="打印时间" />
        </el-table>
      </div>
      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            :current-page="dialogTable_currentPage"
            :page-size="dialogTable_pageSize"
            background
            layout="prev, pager, next"
            :total="dialogTable_listTotal"
            @current-change="dialogTableCurrentChange"
          />
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">关 闭</el-button>
      </div>
    </el-dialog>

    <Popu v-if="isShowDetail">
      <HomeDetail ref="refDetail" :list-item="queryItem" @backListFormDetail="backList" />
    </Popu>
  </template-page>
</template>

<script>
import {
  getEnginList,
  getPassword,
  getPasswordRecord,
  invoiceListProjectV2,
  invoiceListProjectV2Export
} from '@/api/supply/deliver'
import { getCategoryList, getSalesmanList } from '@/api/common'
import HomeDetail from '@/views/supply/deliver/components/home_detail'

let that
import TemplatePage from '@/components/template/template-page-1.vue'
import Popu from '@/components/template/popu.vue'
export default {
  components: {
    HomeDetail,
    TemplatePage,
    Popu
  },
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],

      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: null, // 列表数据
      listLoading: false, // 列表加载loading
      screenForm: {
        // 筛选表单数据
        orderNum: '',
        orderNo: '',
        contractNo: '',
        jxsName: '',
        chName: '',
        model: '',
        date: [],
        jxsNum: '',
        status: '',
        salesMan: '',
        refRegionWork: ''
      },
      salesmanList: [],
      categoryList: [],
      isCollapse: true,
      isShowDialog: false, // 密码记录 - 弹窗
      dialogTable_dataList: null, // 密码记录 - 列表数据
      dialogTable_listLoading: true, // 密码记录 - 列表加载loading
      dialogTable_currentPage: 1, // 密码记录 - 当前页码
      dialogTable_pageSize: 10, // 密码记录 - 每页数量
      dialogTable_listTotal: 0, // 密码记录 - 列表总数

      queryItem: {}
    }
  },

  computed: {
    exParams() {
      return {
        id: this.screenForm.orderNum,
        enginOrderNo: this.screenForm.orderNo,
        refEnginRecordNo: this.screenForm.contractNo,
        customerNumber: this.screenForm.jxsNum,
        customerName: this.screenForm.jxsName,
        materialName: this.screenForm.chName,
        specification: this.screenForm.model,
        startTime: this.screenForm.date ? this.screenForm.date[0] : '',
        endTime: this.screenForm.date ? this.screenForm.date[1] : '',
        categoryName: this.screenForm.type,
        serviceId: this.screenForm.salesMan,
        refRegionWork: this.screenForm.refRegionWork,
        orderType: 2 // 2家用工程 3商用工程
      }
    },
    isShowDetail() {
      return this.queryItem.hasOwnProperty('id')
    }
  },

  beforeCreate() {
    that = this
  },

  created() {
    // this.getSalesmanList()
    // this.getCategoryList()
    // this.getList()
  },

  methods: {
    // 列表请求函数
    getList(...p) {
      let params = {
        ...p[0],
        orderType: 2
      }
      return invoiceListProjectV2(params)
    },
    // 列表导出函数
    // exportList: invoiceListProjectV2Export,
    exportList(...p) {
      let params = {
        ...p[0],
        orderType: 2
      }
      return invoiceListProjectV2Export(params, p[1])
    },
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            <div class="operation-btns">
              <el-button
                size="mini"
                type="text"
                onClick={async () => {
                  this.toDetail(row)
                }}
              >
                详情
              </el-button>
              <el-button
                size="mini"
                type="text"
                onClick={async () => {
                  this.getPassword(row.id)
                }}
              >
                获取密码
              </el-button>
              <el-button
                size="mini"
                type="text"
                onClick={async () => {
                  this.openShareDetail(row.id)
                }}
              >
                密码记录
              </el-button>
            </div>
          </div>
        )
      }
    },

    // 获取业务员列表
    getSalesmanList() {
      getSalesmanList({
        pageNum: 1,
        pageSize: -1,
        isCustomer: 0,
        status: true
      }).then(res => {
        this.salesmanList = res.data.records
      })
    },

    // 获取存货类别列表
    getCategoryList() {
      getCategoryList({
        pageNum: 1,
        pageSize: -1
      }).then(res => {
        this.categoryList = res.data.records
      })
    },

    // 查询列表
    // getList() {
    //   this.listLoading = true

    //   const params = {
    //     pageNum: this.currentPage,
    //     pageSize: this.pageSize,
    //     id: this.screenForm.orderNum,
    //     enginOrderNo: this.screenForm.orderNo,
    //     refEnginRecordNo: this.screenForm.contractNo,
    //     customerNumber: this.screenForm.jxsNum,
    //     customerName: this.screenForm.jxsName,
    //     materialName: this.screenForm.chName,
    //     specification: this.screenForm.model,
    //     startTime: this.screenForm.date ? this.screenForm.date[0] : '',
    //     endTime: this.screenForm.date ? this.screenForm.date[1] : '',
    //     categoryName: this.screenForm.type,
    //     refRegionWork: this.screenForm.refRegionWork,
    //     serviceId: this.screenForm.salesMan,
    //     orderType: 2 // 2家用工程 3商用工程
    //   }
    //   getEnginList(params).then(res => {
    //     res.data.records.forEach(item => {
    //       item.notOutNumber = item.salesStatus ? 0 : item.refundableQty
    //       item.sums1 = ['refundableQty', 'notOutNumber']
    //       item.sums2 = ['payAmount', 'afterTaxPrice', 'noTotalAmount']
    //     })
    //     this.dataList = res.data.records
    //     this.listTotal = res.data.total
    //     this.listLoading = false
    //   })
    // },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1
      this.getList()
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields()
      this.currentPage = 1
      this.getList()
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getList()
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 进入详情
    toDetail(item) {
      this.queryItem = item
      this.$nextTick(() => {
        this.$refs.refDetail.initPrint()
      })
    },

    backList() {
      this.queryItem = {}
    },

    // 获取密码
    getPassword(id) {
      getPassword({ id }).then(res => {
        this.$alert(res.data, '新密码', {
          confirmButtonText: '确定',
          callback: action => {}
        })
      })
    },

    // 密码记录 - 获取列表
    getPasswordRecord(id) {
      getPasswordRecord({
        pageNum: this.dialogTable_currentPage,
        pageSize: this.dialogTable_pageSize,
        id
      }).then(res => {
        this.dialogTable_dataList = res.data.records
        this.dialogTable_listTotal = res.data.total
        this.dialogTable_listLoading = false
      })
    },

    // 密码记录 - 打开弹窗
    openShareDetail(id) {
      this.isShowDialog = true
      this.dialogTable_currentPage = 1
      this.getPasswordRecord(id)
    },

    // 密码记录 - 更改列表当前页
    dialogTableCurrentChange(val) {
      this.dialogTable_currentPage = val
      this.getPasswordRecord()
    }
  }
}
</script>

<style lang="scss" scoped></style>
