<template>
  <a-card :bordered="false">
    <!-- 搜索框 -->
    <div class="table-page-search-wrapper" style="margin-bottom: 15px;">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="48">
          <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' }" v-hasPermission="'B_partnerManage_QRcode'" />
        <span v-if="!$hasPermissions('B_partnerManage_edit') && (!$hasPermissions('B_partnerManage_QRcode') && record.state==0)">--</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"
      :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-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import { Upload, STable, VSelect } from '@/components'
import { getSellerList, changeSellerStatus, sellerQrCode } from '@/api/businessManage.js'
import addPartnerModal from './addPartnerModal.vue'
export default {
  components: {
    STable,
    Upload,
    VSelect,
    addPartnerModal
  },
  data () {
    return {
      queryParam: {
        name: '',
        contactPhone: ''
      },
	  openSupplierModal: false, // 默认关闭弹窗
	  isQrCodeModal: false, //  查看小程序码   弹框展示状态
	  qrCode: '',
	  itemId: null, // 编辑行id
	  itemData: {}, // 编辑行数据
      // 表头
      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')
		  // if (this.queryOrderDate && this.queryOrderDate.length) {
		  //   searchData.beginDate = moment(this.queryOrderDate[0]).format('YYYY-MM-DD')
		  //   searchData.endDate = moment(this.queryOrderDate[1]).format('YYYY-MM-DD')
		  // } else {
		  //   searchData.beginDate = ''
		  //   searchData.endDate = ''
		  // }
        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: {
	  // 禁止不可选日期
	  // disabledDate (current) {
	  //   return current && current > moment().endOf('day')
	  // },
    showModal () {
      this.itemId = null
	  this.itemData = {}
      this.openSupplierModal = true
    },
    // 编辑
    handleEdit (record) {
	  this.itemId = record.id
	  this.itemData = record
	  console.log(this.itemId, '-------编辑')
	  this.openSupplierModal = true
    },
    cancel () {
	  this.itemId = null
	  this.openSupplierModal = false
    },
    // 重置
    resetForm () {
      // this.queryOrderDate = undefined
      this.queryParam.name = ''
	  // this.queryParam.beginDate = ''
	  // this.queryParam.endDate = ''
	  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) {
	  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 () {
	  const link = document.createElement('a')
	  link.href = this.qrCode
	  link.download = 'qrCode.png'
	  link.click()
    }
  }
}
</script>

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