<template>
  <a-modal
    :title="title"
    :width="modelStyle.width"
    :visible="visible"
    :bodyStyle="bodyStyle"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :maskClosable="false"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="8">
                <a-form-item label="服务商">
                  <a-input placeholder="请输入服务商" v-model="queryParam.subcontractorName"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!--<a @click="handleToggleSearch" style="margin-left: 8px">-->
                <!--{{ toggleSearchStatus ? '收起' : '展开' }}-->
                <!--<a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
              <!--</a>-->
            </span>
              </a-col>

            </a-row>
          </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button @click="handleAdd()" type="primary" icon="plus" v-has="'platform:subcontractor:add'">添加</a-button>
        </div>

        <!-- table区域-begin -->
        <div>

          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            @change="handleTableChange">

            <span slot="action" slot-scope="text, record">
              <a @click="handleIsOn(record)" v-has="'platform:subcontractor:ison'" v-if="record.isOn === 1">停用</a>
              <a @click="handleIsOn(record)" v-has="'platform:subcontractor:ison'"  v-if="record.isOn === 0">启用</a>
              <a-divider type="vertical" v-has="'platform:subcontractor:delete'"/>
              <a @click="handledelete(record)" v-has="'platform:subcontractor:delete'">删除</a>
        </span>

          </a-table>
        </div>

        <hlgPlatformSubcontractor-modal ref="modalForm" @ok="modalFormOk"></hlgPlatformSubcontractor-modal>
      </a-card>
    </a-spin>
  </a-modal>
</template>

<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { httpAction ,getAction,putAction,deleteAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import { filterObj } from '@/utils/util'
  import HlgPlatformSubcontractorModal from './HlgPlatformSubcontractorModal'
  const isOnSubcontractor = (params) => putAction('/hlgplatform/hlgPlatformSubcontractor/isOnSubcontractor', params)
  const deletePlatformSubcontractor = (params) => putAction('/hlgplatform/hlgPlatformSubcontractor/deletePlatformSubcontractor', params)

  export default {
    name: "HlgPlatformSubcontractor",
    mixins: [JeecgListMixin],
    components: {
      HlgPlatformSubcontractorModal
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        bodyStyle: {
          padding: '0',
          height: (window.innerHeight * 0.66) + 'px',
          'overflow-y': 'auto'
        },
        modelStyle: {
          width: '76%',
          style: { top: '20px' },
          fullScreen: false
        },
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        options: [{
          value: '',
          label: '',
          isLeaf: false
        }],
        // 查询条件
        queryParam: {
          subcontractorName: '',
        },
        // 表头
        columns: [
          {
            title: '服务商',
            align: 'left',
            dataIndex: 'subcontractorName'
          },
          {
            title: '状态',
            align: 'left',
            dataIndex: 'isOn_dictText'
          },
          {
            title: '操作',
            align: 'left',
            scopedSlots: { customRender: 'action' }
          },
        ],
        platformId: 0,
        confirmLoading: false,
        url: {
          list: "/hlgplatform/hlgPlatformSubcontractor/list",
        }
     
      }
    },
    created () {

    },
    methods: {
      getQueryParams() {
        var param = Object.assign({}, this.queryParam, this.isorter)
        param.field = this.getQueryField()
        param.pageNo = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
        param.platformId = this.platformId;
        return filterObj(param)
      },
      edit (record) {
        this.model = Object.assign({}, record)
        this.platformId = record.id
        this.visible = true
        this.loadData(1)
      },
      loadData(arg) {
        if (!this.url.list) {
          this.$message.error('请设置url.list属性!')
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams()//查询条件
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total;
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        this.close()
      },
      handleCancel () {
        this.close()
      },
      modalFormOk(){
        this.loadData(1)
      },
      //添加服务商
      handleAdd(){
        this.$refs.modalForm.add(this.platformId);
        this.$refs.modalForm.title = "添加服务商";
        this.$refs.modalForm.disableSubmit = false;
      },
      //启停服务商
      handleIsOn(record){
        let content = ''
        let title = ''
        if (record.isOn === 1) {
          content = '停用后平台下所有企业都无法进行充值和支付，确认要停用该服务商吗'
          title = '停用服务商'
        } else {
          content = '确定要启用该服务商吗？'
          title = '启用服务商'
        }
        let that = this
        const modal = that.$confirm({
          title: title,
          closable: true,
          keyboard: false,
          maskClosable: true,
          content: content,
          okText: '确定',
          cancelText: '取消',
          onOk: function() {
            isOnSubcontractor({ id: record.id, isOn: record.isOn === 0 ? 1 : 0 }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
          onCancel(e) {
            modal.destroy()
          }
        })
      },
      //删除服务商
      handledelete(record){
        let that = this
        const modal = that.$confirm({
          title: '删除服务商',
          closable: true,
          keyboard: false,
          maskClosable: true,
          content: '确认要删除该配置吗?',
          okText: '确定',
          cancelText: '取消',
          onOk: function() {
            deletePlatformSubcontractor({ id: record.id}).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
              } else {
                that.$error({
                  title: '删除失败',
                  content: res.message,
                  okText: '确定',
                  centered:true
                })
              }
            })
          },
          onCancel(e) {
            modal.destroy()
          }
        })
      },
    }
  }
</script>
<style>
  .drawer-bootom-button {
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
  .avatar-uploader > .ant-upload {
    width: 104px;
    height: 104px;
  }

  .ant-upload-select-picture-card i {
    font-size: 49px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
</style>