<template>
  <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.shopName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="入驻开始时间">
              <a-date-picker placeholder="入驻开始时间" valueFormat="YYYY-MM-DD" v-model="queryParam.beginDate"></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="入驻结束时间">
              <a-date-picker placeholder="入驻结束时间" valueFormat="YYYY-MM-DD" v-model="queryParam.endDate"></a-date-picker>
            </a-form-item>
          </a-col>
          
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="8" :sm="24">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="shopDetails(1)" type="primary" icon="plus">添加</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
       <span slot="buildingName" slot-scope="text, record">
              <span>{{`${record.buildingName}-${record.unitName}-${record.roomName}`}}</span>
            </span>
        <span slot="shopStatus" slot-scope="text, record">
              <span v-if="record.shopStatus=='normal'" class="success">正常</span>
              <span v-if="record.shopStatus=='freeze'" class="error">冻结</span>
            </span>
        <span slot="action" slot-scope="text, record">
           <a href="javascript:;"
              @click="onShopStatus(record)">{{record.shopStatus === 'normal' ? '冻结' : '解冻'}}</a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">

 <a-menu-item v-if="record.auditStatus === 'auditPass'">
                <a @click="handlePerssion(record.roleId)">授权</a>
              </a-menu-item>

           <a-menu-item>
                <a @click="shopDetails(4,record.id )">详情</a>
              </a-menu-item>
               <a-menu-item v-if="record.auditStatus != 'auditPass'">
                <a @click="shopDetails(3, record.id)">编辑</a>
              </a-menu-item>
               <a-menu-item v-if="record.auditStatus != 'auditPass'">
                   <a @click="shopDetails(2, record.id)">审核</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDeletes(record.shopCode)" placement="topLeft">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <!-- 字符串超长截取省略号显示 -->

      </a-table>

    </div>
    <!-- table区域-end -->


    <!-- 右侧的角色权限配置 -->
    <user-role-modal ref="modalUserRole"></user-role-modal>
  </a-card>
</template>

<script>
import {freezeOrThawApi, shopDeleteApi, shopAuditApi} from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import JUpload from '@/components/jeecg/JUpload'
import UserRoleModal from './modules/UserRoleModal'
import {filterDictTextByCache} from '@/components/dict/JDictSelectUtil'

const columns = [
  {
    title: '商铺名称',
    dataIndex: 'shopName',
    key: 'shopName',
    align: 'center',
  },
  {
    title: '营业执照编码',
    dataIndex: 'creditCode',
    key: 'creditCode',
    align: 'center',
  },
  {
    title: '法人',
    dataIndex: 'legalName',
    key: 'legalName',
    align: 'center',
  },
  {
    title: '电话',
    dataIndex: 'contactPhone',
    key: 'contactPhone',
    align: 'center',
  },
  {
    title: '房屋编号',
    dataIndex: 'buildingName',
    key: 'buildingName',
    align: 'center',
    scopedSlots: { customRender: 'buildingName' },
  },
  {
    title: '添加时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'center',
  },
  {
    title: '审核状态',
    dataIndex: 'auditStatus',
    key: 'auditStatus',
    align: 'center',
    customRender: function(text) {
      return filterDictTextByCache('auditStatus', text);
    }
  },
  {
    title: '冻结/解冻',
    dataIndex: 'shopStatus',
    key: 'shopStatus',
    align: 'center',
    scopedSlots: { customRender: 'shopStatus' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    width: 150
  }
]

export default {
  name: 'PermissionListAsync',
  mixins: [JeecgListMixin],
  components: {
    JEllipsis,
    JUpload,
    UserRoleModal
  },
  data() {
    return {
      // 表头
      columns: columns,
      loading: false,
      url: {
        list: "/property-central/shop/shopInfo/list",
        audit: "/property-central/shop/shopInfo/audit",
      },
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
      other: '',
      uploadDisabled: false
    }
  },
  methods: {
    onLoadDetail(record, type) {
      record['registAdress'] = [record.provinceCode, record.cityCode, record.countyCode]
      if(type === 'edit') {
        this.handleEdit(record)
      } else {
        this.handleDetail(record)
      }
    },
    shopDetails(type, id) {
      if (type == 1) {
        this.$router.push({
          path: '/shops/ShopDetail?type=add',
        })
      } else if (type == 2) {
        this.$router.push({
          path: '/shops/ShopDetail?type=audit&id=' + id,
        })
      }else if (type == 3) {
        this.$router.push({
          path: '/shops/ShopDetail?type=edit&id=' + id,
        })
      }else if (type == 4) {
        this.$router.push({
          path: '/shops/ShopDetail?type=detail&id=' + id,
        })
      }
    },
    handleDeletes(id) {
      shopDeleteApi({shopCode:id}).then(res=> {
        this.$message.success(res.message);
        this.loadData()
      })
    },
    onShopStatus(record) {
      let that = this
      this.$confirm({
        title: `确认${record.shopStatus === 'normal' ? '冻结' : '解冻'}?`,
        closable: true,
        okText: `${record.shopStatus === 'normal' ? '冻结' : '解冻'}`,
        onOk() {
          return freezeOrThawApi({
            id: record.id,
            status: `${record.shopStatus === 'normal' ? 'freeze' : 'normal'}`,
          }).then((res) => {
            that.searchQuery()
          })
        },
        onCancel() {},
      })
    },
    handlePerssion(roleId) {
      this.$refs.modalUserRole.show(roleId)
    },
    onDateChange: function (value, dateString) {
      console.log(dateString[0],dateString[1]);
      this.queryParam.beginDate=dateString[0];
      this.queryParam.endDate=dateString[1];
    },
    onDateOk(value) {
      console.log(value);
    },

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

</style>
<style>
.success{
  color: #52c41a;
}
.warning{
  color: #fa8c16;
}
.error{
  color: #f5222d;
}
</style>