<template>
  <div>
    <div class="mrg-b">
      <el-row>
        <el-col :span="21">
          <el-button v-auth="50600020105" type="primary" @click="handleAdd">新增</el-button>
          <el-button v-auth="50600020106" type="primary" @click="handleDeleteRows">删除</el-button>
          <el-button v-auth="50600020113" type="primary" @click="handleCopy">复制</el-button>
          <el-button v-auth="50600020107" type="primary" @click="handleAudit">审核</el-button>
          <el-button v-auth="50600020108" type="primary" @click="handleCancelAudit">取消审核</el-button>
          <el-button v-auth="50600020109" type="primary" @click="handleAgain">重新计费</el-button>
        </el-col>
        <el-col :span="3">
          <el-col :span="24" class="mrg-l">
            <el-button v-auth="50600020102" type="primary" @click="handleMoreSearch">更多</el-button>
            <el-button
              v-auth="50600020101"
              icon="el-icon-refresh"
              class="query__item"
              type="primary"
              @click="handleRefresh"
              plain
              circle
            ></el-button>
             <el-button
             v-if="false"
              icon="el-icon-download"
              type="primary"
              @click="handleExcel"
              plain
              circle
            ></el-button>
          </el-col>
        </el-col>
      </el-row>
      <div class="inputBox">
        <el-form inline :model="query" ref="query" label-position="left">
          <el-form-item label="合同编号" prop="contractNo">
            <el-input
              v-model="query.rateCode"
              @keyup.enter.native="handleSearch"
              placeholder="快捷查询(合同编号)"
            ></el-input>
          </el-form-item>
          <el-form-item label="合同名称" prop="contractName">
            <el-input v-model="query.contractName" @keyup.enter.native="handleSearch"></el-input>
          </el-form-item>
          <el-form-item
              label="合同类型"
              prop="contractType">
              <el-select multiple v-model="contractType" @change="handleSelected">
                <el-option label="客户" value="CUSTOMER"></el-option>
                <el-option label="外租仓库" value="WH"></el-option>
                <el-option label="装卸队" value="HANDING"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="客户"
              prop="customerCode">
              <PublicQuery
              title="客户"
              baseUrl="/chg_web/chgweb"
              cfgKey="chgWmsCustomerByOwner"
              @change="handleSearch"
              :valueField.sync="query.customerCode">
              </PublicQuery>
            </el-form-item>
        </el-form>
      </div>
    </div>
    <more-query
      :moreQueryVisible="moreQueryVisible"
      @close="moreQueryVisible=false"
      @toGetList="getQueryList"
    ></more-query>
  </div>
</template>
<script>
import MoreQuery from './more-query'
export default {
  components: {
    MoreQuery
  },
  data () {
    return {
      query: {},
      moreQueryVisible: false,
      contractType: [],
      moreData: {}
    }
  },
  props: {
    selectIds: Array,
    rows: Array
  },
  methods: {

    // 检查是否选中行
    checkSelections () {
      if (!this.selectIds.length) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },

    // 新增
    handleAdd () {
      this.$emit('handleOpenForm', 'create')
    },
    handleSelected (val) {
      this.query.contractType = val.join(',')
      this.handleSearch()
    },

    handleRefresh () {
      this.contractType = []
      this.$refs.query.resetFields()
      this.$emit('getList')
    },
    // 搜索
    handleSearch () {
      const query = { ...this.query, contractNo: `%${this.query.contractNo || ''}%`, contractName: `%${this.query.contractName || ''}%` }
      this.moreData = query
      this.$emit('getList', query)
    },

    // 更多查询数据
    getQueryList (query) {
      this.moreQueryVisible = false
      this.moreData = query
      this.$emit('getList', query)
    },

    // 更多搜索查询
    handleMoreSearch () {
      this.moreQueryVisible = true
    },

    // 删除
    handleDeleteRows () {
      const url = this.$API.charging.costGroup.delList
      const param = { contractNos: JSON.stringify(this.selectIds) }
      if (!this.checkSelections()) return
      this.$confirm(`您已选中${this.selectIds.length}条数据,是否删除？`, '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 请求
    getData (urlkey, text) {
      const url = this.$API.charging.contract[urlkey]
      const param = { contractNos: JSON.stringify(this.selectIds) }
      if (!this.checkSelections()) return
      this.$confirm(`是否执行${text}操作？`, `${text}`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList', {}, this.type)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消${text}`
        })
      })
    },

    // 审核
    handleAudit () {
      const urlkey = 'auditContractEntity'
      this.getData(urlkey, '审核')
    },

    // 取消审核
    handleCancelAudit () {
      const urlkey = 'cancelAuditContractEntity'
      this.getData(urlkey, '取消审核')
    },

    // 重新计费
    handleAgain () {
      const urlkey = 'chargeAgain'
      this.getData(urlkey, '重新计费')
    },

    // 复制
    handleCopy () {
      if (!this.rows.length) return this.$message.warning('请选择数据')
      if (this.rows.length > 1) return this.$message.warning('请选择一条数据')
      const row = { ...this.rows[0] }
      console.log(row)
      this.$emit('handleOpenForm', 'copy', row)
    },
    handleExcel () {
      const url = this.$API.charging.contract.getList
      const options = this.moreData
      this.$exportExcel(url, options)
    },

  }
}
</script>
<style lang="scss" scoped>
.inputBox {
  width: 100%;
  border: 1px solid #d1d5db;
  margin-top: 10px;
  margin-bottom: 10px;
  border-radius: 10px;
  padding-bottom: 10px;
  padding-left: 10px;
  .el-form-item {
    margin-bottom: 0px;
    margin-top: 10px;
  }
}
.mrg-b {
  margin-bottom: 20px;
}
.mrg-l {
  margin-left: 10px;
}
.query {
  &__operations {
    float: left;
  }
  &__retrieve {
    float: right;
    .el-input {
      max-width: 180px;
    }
  }
  &__item {
    margin-left: 10px;
  }
}
</style>
