<template>
  <div class="page-header-index-wide">
  <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.companyName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="企业认证">
              <j-dict-select-tag v-model="queryParam.authenticationStatus" placeholder="请选择认证状态" dictCode="authentication_status"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="所在地">
              <a-cascader :options="options" :value="arr" @change="onChange" placeholder="请选择所在地区"
                          changeOnSelect/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item label="注册时间">
                <a-range-picker
                  style="width: 100%"
                  v-model="queryParam.createTimeRange"
                  format="YYYY-MM-DD"
                  :placeholder="['开始时间', '结束时间']"
                  @change="onDateChange"
                  @ok="onDateOk"
                  :allowClear="allowClear"
                />
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="合同情况">
                <j-dict-select-tag :triggerChange="false" v-model="queryParam.companySubcontractorStatus" placeholder="请选择状态"
                                   dictCode="company_subcontractor_status" v-decorator="['companySubcontractorStatus', {}]"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a @click="handleToggleSearch">
                {{ toggleSearchStatus ? '取消' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
              <a-button ghost type='primary' @click="searchQuery" icon="search" style="margin-left: 8px">查询</a-button>
              <a-button ghost type='primary' @click="this.searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-has="'company:add'" type="primary" icon="plus">新增企业</a-button>
    </div>

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

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

        <span slot="action1" slot-scope="text, record">
           <a @click="handleDetail(record)">{{record.companyName}}</a>
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleSubcontractor(record)" v-has="'company:set'">设置</a>
          <a-divider type="vertical" v-has="'company:edit'"/>
            <a @click="handleEdit(record)" v-has="'company:edit'">编辑</a>
            <a-divider type="vertical" v-has="'company:authen'"/>
            <a @click="handleAuthen(record)" v-has="'company:authen'">认证</a>
            <a-divider type="vertical" v-if="record.status == 0" v-has="'company:lock'"/>
            <a-divider type="vertical" v-if="record.status == 1" v-has="'company:unlock'"/>
            <a @click="handleLock(record)" v-if="record.status == 0" v-has="'company:lock'">锁定</a>
            <a @click="handleLock(record)" v-if="record.status == 1" v-has="'company:unlock'">解锁</a>
            <a-divider type="vertical" v-has="'company:del'"/>
            <a @click="handleDelete(record.id)" v-has="'company:del'">删除</a>
        </span>

      </a-table>
    </div>

    <hwPlatformCompany-modal ref="modalForm" @ok="modalFormOk"></hwPlatformCompany-modal>
    <hwPlatformCompanyAuth-modal ref="modalAuthForm" @ok="modalFormOk"></hwPlatformCompanyAuth-modal>
    <hw-company-subcontractor  ref="modalCompanySubcontractorForm" @ok="modalFormOk"></hw-company-subcontractor>
  </a-card>
  </div>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import HwPlatformCompanyModal from './modules/HwPlatformCompanyModal'
  import HwPlatformCompanyAuthModal from './modules/HwPlatformCompanyAuthModal'
  import { getAction, deleteAction, putAction, postAction } from '@/api/manage'
  import data1 from '../tools/area.json'
  import { filterObj } from '@/utils/util';
  import HwCompanySubcontractor from './modules/HwCompanySubcontractor'

  const deleteCompany = (params) => putAction('/saasmanager/hwPlatformCompany/delete', params)
  const lockCompany = (params) => putAction('/saasmanager/hwPlatformCompany/lockCompany', params)


  export default {
    name: 'HwPlatformCompanyList',
    mixins: [JeecgListMixin],
    components: {
      HwCompanySubcontractor,
      HwPlatformCompanyModal,
      HwPlatformCompanyAuthModal
    },
    data() {
      return {
        allowClear:false,
        description: '企业信息管理页面',
        // 查询条件
        queryParam: {
          createTimeRange:[],
        },
        // 表头
        columns: [
          {
            title: '企业名称',
            align: 'left',
            dataIndex: 'companyName',
            scopedSlots: { customRender: 'action1' }
          },
          {
            title: '企业简称',
            align: 'left',
            dataIndex: 'companyAbbreviation'
          },
          {
            title: '所在地',
            align: 'left',
            dataIndex: 'provinceCityCountry'
          },
          {
            title: '认证状态',
            align: 'left',
            dataIndex: 'authenticationStatus_dictText'
          },
          {
            title:'合同情况',
            align:"left",
            dataIndex: 'companySubcontractorStatus',
            customRender:function (text) {
              if(text==1){
                return "待上传"
              }else if(text==2){
                return "已上传"
              }else{
                return "--"
              }
            }

          },
          {
            title: '注册时间',
            align: 'left',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'left',
            width: 280,
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        isorter: {
          column: 'updateTime',
          order: 'desc'
        },
        url: {
          list: '/saasmanager/hwPlatformCompany/list',
          delete: '/saasmanager/hwPlatformCompany/delete',
          deleteBatch: '/saasmanager/hwPlatformCompany/deleteBatch',
          exportXlsUrl: '/saasmanager/hwPlatformCompany/exportXls',
          importExcelUrl: 'saasmanager/hwPlatformCompany/importExcel'
        },
        dictOptions: {},
        options: [{
          value: '',
          label: '',
          isLeaf: false
        }],
        arr: [],  //存放回显默认省市区code
        provArray: [], //存放省
        cityArray: [],   //存放市
        countryArray: [],  //存放区
      }
    },
    created() {
      //省市区options完全拼接
      for (var i = 0; i < data1.length; i++) {
        this.cityArray = []
        for (let j = 0; j < data1[i].cityList.length; j++) {
          this.countryArray = []
          for (let k = 0; k < data1[i].cityList[j].areaList.length; k++) {
            var country = {
              value: data1[i].cityList[j].areaList[k].code,
              label: data1[i].cityList[j].areaList[k].name
            }
            this.countryArray.push(country)
          }
          var city = {
            value: data1[i].cityList[j].code,
            label: data1[i].cityList[j].name,
            children: this.countryArray
          }
          this.cityArray.push(city)
        }
        var prov = {
          value: data1[i].code,
          label: data1[i].name,
          children: this.cityArray
        }
        this.provArray.push(prov)
        this.options = this.provArray
      }
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      }
    },
    methods: {
      getQueryParams(){
        console.log(this.queryParam.createTimeRange)
        var param = Object.assign({}, this.queryParam,this.isorter);
        param.field = this.getQueryField();
        param.pageNo = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
        delete param.createTimeRange; // 时间参数不传递后台
        return filterObj(param);
      },
      initDictConfig() {
      },
      onDateChange: function (value, dateString) {
        console.log(dateString[0],dateString[1]);
        this.queryParam.createTime_begin=dateString[0]+" 00:00:00";
        this.queryParam.createTime_end=dateString[1] +" 23:59:59";
      },
      onDateOk(value) {
        console.log(value);
      },
      //锁定/解锁
      handleLock(record) {
        let content = ''
        let title = ''
        if (record.status == 0) {
          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() {
            lockCompany({ id: record.id, status: record.status == 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(id) {
        let that = this
        const modal = that.$confirm({
          title: '删除企业',
          closable: true,
          keyboard: false,
          maskClosable: true,
          content: '确定要删除该企业吗？',
          okText: '确定删除',
          cancelText: '取消',
          onOk: function() {
            deleteCompany({ id: id }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
              } else {
                that.$error({
                  title: '删除失败',
                  content: res.message,
                  okText: '确定'
                })
              }
            })
          },
          onCancel(e) {
            modal.destroy()
          }
        })
      },
      //认证
      handleAuthen(record) {
        this.$refs.modalAuthForm.edit(record)
        this.$refs.modalAuthForm.title = '企业认证'
        this.$refs.modalAuthForm.disableSubmit = false
      },
      //省市区改变
      onChange(value, label) {
        this.arr = value
        this.queryParam.provinceCode = value[0]
        this.queryParam.cityCode = value[1]
        this.queryParam.countryCode = value[2]

      },

      //查看服务商
      handleSubcontractor(record){
        this.$refs.modalCompanySubcontractorForm.edit(record)
        this.$refs.modalCompanySubcontractorForm.title = '服务商'
        this.$refs.modalCompanySubcontractorForm.disableSubmit = false
      },

      // 重置
      searchReset(){
        var that = this;
        var logType = that.queryParam.logType;
        that.queryParam = {}; //清空查询区域参数
        that.queryParam.logType = logType;
        this.arr=[]
        that.loadData(this.ipagination.current);
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  ::v-deep .ant-table-pagination{
    float: none;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>