<template>
  <page-layout>
  <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="税号">
              <a-input placeholder="请输入企业税号" v-model="queryParam.dutyParagraph"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="9" :sm="12">
            <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="合同情况">
                <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-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="this.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="'hlgcompany: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="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="'hlgcompany:set'">设置</a>
          <!--<template v-if="(!record.cpSettingId) && record.isOn>0">-->
           <!--<a-divider type="vertical" v-has="'hlgcompany:pay'"/>-->
          <!--<a @click="handleCompanyPay(record)" v-has="'hlgcompany:pay'">平台支付</a>-->
          <!--</template>-->
          <a-divider type="vertical" v-has="'hlgcompany:edit'"/>
          <a @click="handleEdit(record)" v-has="'hlgcompany:edit'">编辑</a>
             <template v-if="(!record.cpSettingId) || (record.cpSettingId && record.isOn==0)">
           <a-divider type="vertical" v-if="record.isLock == 0"  v-has="'hlgcompany:lock'"/>
            <a-divider type="vertical" v-if="record.isLock == 1" v-has="'hlgcompany:lock'" />
            <a @click="handleLock(record)" v-if="record.isLock == 0" v-has="'hlgcompany:lock'">锁定</a>
            <a @click="handleLock(record)" v-if="record.isLock == 1" v-has="'hlgcompany:lock'">解锁</a>
          <a-divider type="vertical" v-has="'hlgcompany:delete'"  />
           <a @click="handleDelete(record.id)" v-has="'hlgcompany:delete'" >删除</a>
             </template>
        </span>

      </a-table>
    </div>

    <hlgCompany-modal ref="modalForm" @ok="modalFormOk"></hlgCompany-modal>
    <hlg-company-subcontractor ref="modalCompanySubcontractorForm" @ok="modalFormOk"></hlg-company-subcontractor>
    <hlg-company-pay-modal  ref="companyPayForm" @ok="modalFormOk"></hlg-company-pay-modal>
  </a-card>
  </page-layout>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import HlgCompanyModal from './modules/HlgCompanyModal'
  import PageLayout from '@/components/page/PageLayout'
  import { getAction, deleteAction, putAction, postAction } from '@/api/manage'
  import data1 from '../tools/area.json'
  import { filterObj } from '@/utils/util';
  import HlgCompanySubcontractor from './modules/HlgCompanySubcontractor'
  import HlgCompanyPayModal from './modules/HlgCompanyPayModal'

  const deleteCompany = (params) => deleteAction('/hlgcompany/hlgCompany/delete', params)
  const lockCompany = (params) => putAction('/hlgcompany/hlgCompany/lockCompany', params)

  export default {
    name: "HlgCompanyList",
    mixins:[JeecgListMixin],
    components: {
      HlgCompanyPayModal,
      HlgCompanySubcontractor,
      HlgCompanyModal,
      PageLayout
    },
    data () {
      return {
        allowClear:false,
        // 查询条件
        queryParam: {
          createTimeRange:[],
        },
        description: 'hlg_company管理页面',
        // 表头
        columns: [
          {
            title:'企业名称',
            align:"left",
            dataIndex: 'companyName',
            scopedSlots: { customRender: 'action1' }
          },
          {
            title:'税号',
            align:"left",
            dataIndex: 'dutyParagraph'
          },
          {
            title:'所在地',
            align:"left",
            dataIndex: 'provinceCityCountry'
          },
          {
            title:'合同情况',
            align:"left",
            dataIndex: 'companySubcontractorStatus',
            customRender:function (text) {
              if(text==1){
                return "待上传"
              }else if(text==2){
                return "已上传"
              }else{
                return "--"
              }
            }

          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"left",
            scopedSlots: { customRender: 'action' },
          }
        ],
        isorter: {
          column: 'updateTime',
          order: 'desc'
        },
        url: {
          list: "/hlgcompany/hlgCompany/list",
          delete: "/hlgcompany/hlgCompany/delete",
          deleteBatch: "/hlgcompany/hlgCompany/deleteBatch",
          exportXlsUrl: "/hlgcompany/hlgCompany/exportXls",
          importExcelUrl: "hlgcompany/hlgCompany/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);
      },
      //物理删除
      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()
          }
        })
      },
      //锁定/解锁
      handleLock(record) {
        let content = ''
        let title = ''
        if (record.isLock == 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, isLock: record.isLock == 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()
          }
        })
      },
      //省市区改变
      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);
      },
      handleCompanyPay(record){
        this.$refs.companyPayForm.edit(record)
        this.$refs.companyPayForm.title = '平台支付权限'
        this.$refs.companyPayForm.disableSubmit = false
      },

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