<template>
<div>
  <Modal v-model="showUserGroupModal" width="800">
    <p slot="header">
      {{ modalTitle }}
    </p>
    <Row>
      <!--查询条件-->
      <Col span="24">
        <Form id="test-item-form" :label-width="90" inline onsubmit="return false">
          <label class="label-sign" />
          <Form-item :label-width="70" label="委托单位" >
            <Input
              v-model="client"
              @on-enter="_pageChange(1)"
              placeholder="委托单位"
              style="width: 100%"
            />
          </Form-item>
          <Form-item label="合同编号">
            <Input
              v-model="salesCode"
              @on-enter="_pageChange(1)"
              placeholder="合同编号"
              style="width: 100%"
            />
          </Form-item>
          <Form-item class="search-btn">
            <Button @click="_pageChange(1)" type="primary">搜索</Button>
          </Form-item>
        </Form>
      </Col>
      <Col span="24" style="margin-bottom: 10px">
        <el-table
          :data="getPage"
          :height="tableHeight"
          @row-click="_rowChange"
          @row-dblclick="_dbClick"
          border
          size="small"
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column
            v-for="item in pageColumns"
            :key="item.key"
            :prop="item.key"
            :label="item.title"
            :min-width="item.width"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span v-if="item.key==='status'">
                {{ scope.row[item.key]?scope.row[item.key].display:'' }}
              </span>
              <span v-else>{{ scope.row[item.key] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </Col>
      <Col span="24">
        <Page
          :total="total"
          :page-size="size"
          @on-change="_pageChange"
          @on-page-size-change="_pageRows"
          placement="top"
          show-total
          show-elevator
          show-sizer
        />
        <div style="clear: both" />
      </Col>
    </Row>
    <div slot="footer" class="btn-width">
      <Button @click="_cancel" style="margin-left: 8px">
        取消
      </Button>
      <Button @click="_ok" type="primary">
        确定
      </Button>
    </div>
  </Modal>
</div>
</template>

<script>
// import { meterAptitude } from '../../../api'
import { meterContract } from '../../../api'
export default {
  name: 'EditModal',
  data() {
    return {
      formObj: {
        client: undefined,
        salesCode: undefined
      },
      loading: true,
      pageParams: { rows: 20 },
      total: 0,
      size: 0,
      pageColumns: [
        { title: '合同编号', key: 'salesCode', width: 120 },
        { title: '委托单位', key: 'client' },
        { title: '联系人', key: 'person' },
        { title: '联系电话', key: 'tel' },
        { title: '样品名称', key: 'samples', width: 140 }
        // { title: '传真', key: 'fax', width: 120 },
        // { title: '详细地址', key: 'address', width: 250 },
        // { title: '登记日期', key: 'edate', width: 120, date: true },
        // { title: '要求完成时间', key: 'odate', width: 120, date: true },
        // { title: '折扣', key: 'discount', width: 120 },
        // { title: '总价', key: 'totalPrice', width: 120 },
        // { title: '折扣价', key: 'discountPrice', width: 120 },
        // { title: '备注', key: 'remark' }
      ],
      getPage: [],
      showUserGroupModal: false,
      modalTitle: '',
      currentRowData: {},
      name: '',
      code: '',
      client: '',
      salesCode: ''
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    _open() {
      this.modalTitle = '选择合同'
      this.name = ''
      this.code = ''
      this._judge()
    },
    _judge() {
      this.showUserGroupModal = true
      this.currentRowData = {}
      this._page()
    },
    // _page: async function() {
    //   const result = await meterManage.page(this._searchParams())
    //   if (result) {
    //     this.getPage = result.records
    //     this.loading = false
    //   }
    // },
    _page: async function() {
      const result = await meterContract.pageContractForEntrust(
        this._searchParams()
      )
      if (result) {
        this.getPage = result.records
        this.total = result.total
        this.size = result.size
        console.log(this.getPage)
        this.loading = false
      }
    },
    _pageChange(page) {
      this.pageParams.page = page
      this._page()
    },
    _pageRows(rows) {
      this.pageParams.rows = rows
      this._page()
    },
    _searchParams() {
      const data = this.$serialize('test-item-form')
      if (this.name) {
        data.name = this.name
      }
      if (this.code) {
        data.code = this.code
      }
      if (this.client) {
        data.client = this.client
      }
      if (this.salesCode) {
        data.salesCode = this.salesCode
      }
      return this.$extend(data, this.pageParams)
    },
    _search() {
      this._page()
    },
    _rowChange(data) {
      this.currentRowData = data // 选中的行数据;
    },
    _cancel() {
      this.showUserGroupModal = false
    },
    _ok() {
      if (JSON.stringify(this.currentRowData) === '{}') {
        switch (this.modalTitle) {
          case '选择合同':
            this.$Message.warning('请选择一条合同！')
            break
        }
      } else {
        this.$emit('on-result-change', this.currentRowData)
        this.showUserGroupModal = false
      }
    },
    // 双击
    _dbClick(data, event) {
      this.currentRowData = data
      // 选中的行数据;
      this._ok()
    }
  }
}
</script>

<style scoped>
</style>
