<template>
  <a-card :bordered="false" class="partnerManage-page-info">
    <!-- 搜索框 -->
    <div class="partnerManage-search">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="20">
          <a-col :md="6" :sm="24">
            <a-form-item label="商户名称"><a-input allowClear v-model.trim="queryParam.name" :maxLength="30" placeholder="请输入" id="OperateJournal-name"/></a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="负责人电话"><a-input allowClear v-model.trim="queryParam.contactPhone" :maxLength="30" placeholder="请输入" id="OperateJournal-name"/></a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button style="margin-right: 10px;" type="primary" @click="$refs.table.refresh(true)" id="OperateJournal-refreshTable">查询</a-button>
            <a-button type="" @click="resetForm" id="OperateJournal-resetForm">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="add"><a-button
      type="primary"
      icon="plus"
      class="addBtn"
      @click="showModal"
      id="bannerSetting-showModal"
      v-hasPermission="'B_partnerManage_add'">新增</a-button></div>
    <!-- table列表 -->
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <span slot="action" slot-scope="text, record">
        <a-icon
          v-if="$hasPermissions('B_partnerManage_edit') && record.state==0"
          type="edit"
          id="bannerSetting-handleEdit"
          title="编辑"
          class="actionBtn icon-blues"
          @click="handleEdit(record)" />

        <a-icon type="qrcode" title="查看二维码" @click="getQrCode(record)" :style="{ fontSize: '20px', color: '#e29e14', padding: '0 10px' }" />
        <!-- <span v-if="!($hasPermissions('B_partnerManage_edit') && record.state==0) && !$hasPermissions('B_partnerManage_QRcode') ">--</span> -->
      </span>
      <span slot="state" slot-scope="text, record">
        <a-switch
          v-hasPermission="'B_partnerManage_enable'"
          checkedChildren="启用"
          unCheckedChildren="禁用"
          id="bannerSetting-changeFlagHandle"
          :checked="record.state == 1 ? true : false"
          @change="changeFlagHandle(text, record)"
        />
        <span v-if="!$hasPermissions('B_partnerManage_enable')">--</span>
      </span>
    </s-table>
    <!-- 新增编辑弹窗 -->
    <add-partner-modal :itemId="itemId" :itemData="itemData" :openSupplierModal="openSupplierModal" @refresh="$refs.table.refresh(true)" @close="cancel"></add-partner-modal>
    <!-- 查看小程序码 -->
    <a-modal
      class="qrCodeModal"
      title="查看二维码"
      :width="500"
      :footer="null"
      :shName="shName"
      :destroyOnClose="true"
      @cancel="isQrCodeModal = false"
      v-model="isQrCodeModal">
      <img :src="qrCode" width="400" height="400" class="qrCode" />
      <a-button type="primary" class="downQrCode" @click="downloadCode">下载二维码</a-button>
      <!-- <a-button type="primary" class="downQrCode" @click="downloadCode">下载小程序码</a-button> -->
    </a-modal>
  </a-card>
</template>

<script>
import { Upload, STable, VSelect } from '@/components'
import { getSellerList, changeSellerStatus } from '@/api/businessManage.js'
import addPartnerModal from './addPartnerModal.vue'
import QRCode from 'qrcode'
export default {
  components: {
    STable,
    Upload,
    VSelect,
    addPartnerModal
  },
  data () {
    return {
      queryParam: {
        name: '',
        contactPhone: ''
      },
	  openSupplierModal: false, // 默认关闭弹窗
	  isQrCodeModal: false, //  查看小程序码   弹框展示状态
	  qrCode: '',
	  itemId: null, // 编辑行id
	  itemData: {}, // 编辑行数据
	  shName: '', // 商户名称
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'no',
          width: 60,
          align: 'center'
        },
        {
		  title: '创建时间',
		  width: 200,
		  dataIndex: 'createDate',
		  align: 'center'
        },
        {
          title: '商户名称',
          width: 100,
          dataIndex: 'name',
          align: 'center'
        },

        {
		  title: '负责人',
		  width: 100,
		  dataIndex: 'contactName',
		  align: 'center'
        },
        {
		  title: '负责人电话',
		  width: 100,
		  dataIndex: 'contactPhone',
		  align: 'center',
		  scopedSlots: {
		    customRender: 'position'
		  }
        },
        {
          title: '状态',
          width: 100,
          dataIndex: 'state',
          align: 'center',
          scopedSlots: {
            customRender: 'state'
          }
        },
        {
          title: '操作',
          align: 'center',
          width: 100,
          scopedSlots: {
            customRender: 'action'
          }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
		  const searchData = Object.assign(parameter, this.queryParam)
		  console.log(this.queryOrderDate, 'this.queryOrderDate')
        return getSellerList(
          searchData
        ).then(res => {
          const no = (res.data.pageNo - 1) * res.data.pageSize
		   if (res.status == 200) {
            for (let i = 0; i < res.data.list.length; i++) {
			  const _item = res.data.list[i]
			   _item.no = no + i + 1
            }
            return res.data
		   }
        })
      },
      formLayout: 'horizontal',
      hideRequiredMark: false, // 是否显示必填的* 默认显示
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 14
        }
      }
    }
  },
  methods: {
    showModal () {
      this.itemId = null
      this.itemData = {}
      this.openSupplierModal = true
    },
    // 编辑
    handleEdit (record) {
	  this.itemId = record.id
	  this.itemData = record
	  this.openSupplierModal = true
    },
    cancel () {
	  this.itemId = null
	  this.openSupplierModal = false
    },
    // 重置
    resetForm () {
      this.queryParam.name = ''
      this.queryParam.contactPhone = ''
      this.$refs.table.refresh(true)
    },
    // 更改启用禁用状态
    changeFlagHandle (text, record) {
      console.log(text)
      const _this = this
      const _data = {
    	  id: record.id,
    	  flag: record.state == 1 ? '0' : '1'
      }
      changeSellerStatus(_data).then(res => {
    	  if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$refs.table.refresh()
    	  } else {
          record.state = !record.state
    	  }
      })
    },
    // 查看二维码
    getQrCode (item) {
      const _this = this
      const opts = {
        errorCorrectionLevel: 'H',
        type: 'image/jpeg',
        quality: 0.3,
        margin: 1.5,
        width: 240,
        color: {
          dark: '#000000',
          light: '#ffffff'
        }
      }
      const url = 'officialPartnerNo=' + item.encryptedPartnerNo + '&codeType=hexiao'
      _this.shName = item.name
      console.log(item.encryptedPartnerNo)
      if (url) {
        QRCode.toDataURL(url, opts, function (err, url) {
          _this.qrCode = url
          _this.isQrCodeModal = true
        })
      }
    },
    // 查看小程序码
    //  getQrCode (item) {
	  // sellerQrCode({ officialPartnerNo: item.officialPartnerNo }).then(res => {
	  //   if (res.status == 200) {
	  //     this.qrCode = 'data:image/png;base64,' + res.data
	  //     this.isQrCodeModal = true
	  //   } else {
	  //     this.qrCode = ''
	  //   }
	  // })
    //  },
    //  下载二维码
    downloadCode (item) {
      const link = document.createElement('a')
      link.href = this.qrCode
      link.download = this.shName + '.png'
      link.click()
    }
  }
}
</script>

<style lang="less" scoped>
	.partnerManage-page-info{
		.partnerManage-search{
			margin-bottom: 15px;
		}
		.addBtn {
		  margin-bottom: 15px;
		}
	}
	// 查看小程序码  弹框
		.qrCodeModal {
			.qrCode {
				display: block;
				max-width: 100%;
				margin: 0 auto;
			}
			.downQrCode {
				display: block;
				margin: 40px auto 30px;
			}
		}
</style>
