<template>
  <div class="common-page-content">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        :model="searchForm"
        label-width="auto"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="合同编号">
          <el-input
            size="small"
            v-model="searchForm.contrCode"
            clearable
            style="width: 130px"
            placeholder="请输入合同编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="供货方">
          <el-input
            size="small"
            style="width: 130px"
            v-model="searchForm.vendorName"
            clearable
            placeholder="请输入供货方"
          ></el-input>
        </el-form-item>
        <el-form-item label="采购方">
          <el-input
            size="small"
            style="width: 130px"
            v-model="searchForm.matCompanyName"
            clearable
            placeholder="请输入采购方"
          ></el-input>
        </el-form-item>
        <el-form-item label="签订日期">
          <el-date-picker
            size="small"
            style="width: 130px"
            v-model="searchForm.signData"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="请选择签订日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="合同状态">
          <el-select
            size="small"
            clearable
            v-model="contractStatus"
            placeholder="请选择合同状态"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="container-btn">
        <el-button
          size="small"
          icon="el-icon-search"
          type="danger"
          class="btn btn-plus"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          icon="el-icon-refresh-right"
          class="btn btn-plus"
          @click="clearSearchForm"
        >
          重置
        </el-button>
      </div>
    </div>
    <el-row
      class="common-table-container flex searchPanel"
      :style="SearchPanel"
    >
      <el-col :span="24" class="common-table-wrapper">
        <div class="common-btn-group">
          <!-- <el-button
            size="small"
            icon="el-icon-plus"
            type="primary"
            class="btn btn-plus"
            @click="handleAdd"
          >
            新增
          </el-button>
          <el-button
            size="small"
            slot="reference"
            icon="el-icon-delete"
            type="danger"
            class="btn btn-delete"
            @click="handleDelete"
          >
            {{ this.$t('common.delete') }}
          </el-button> -->
          <el-button v-if="contractStatus==3" icon="el-icon-thumb" size="small" type="primary" plain @click="downContract">下载合同</el-button>
        </div> 
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation">
          <el-table
            :border="true"
            stripe
            fitaaaaaaaaaaaaaaaaaa
            size="mini"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" min-width="5%" align="center">
            </el-table-column>
            <el-table-column
              prop="contrCode"
              min-width="110px"
              label="合同编号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="contrName"
              min-width="110px"
              label="合同名称"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorName"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="供货方"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorUserName"
              show-overflow-tooltip
              min-width="110px"
              align="center"
              label="供货方联系人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorUserPhone"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="供货方联系方式"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matCompanyName"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="采购方"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matCompanyUserName"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="采购方联系人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matCompanyUserPhone"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="采购方联系方式"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="signData"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="签订日期"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="contractStartDate"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="合同开始日期"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="contractEndDate"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="合同结束日期"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="contrMethod"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="合同类型"
              resizable
            >
              <template slot-scope="scope">
                <div v-if="scope.row.contrMethod == 1">固定单价合同</div>
                <div v-if="scope.row.contrMethod == 2">标准合同</div>
                <div v-if="scope.row.contrMethod == 3">固定数量合同</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="status"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="合同状态"
              resizable
            >
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.status == 3">已签订</el-tag>
                <el-tag type="info" v-if="scope.row.status == 2">签订中</el-tag>
              </template>
            </el-table-column>
            <el-table-column
              min-width="150px"
              fixed="right"
              align="center"
              label="操作"
            >
              <template v-slot="{row}">
                <template>
                  <el-button v-if="row.status==3||row.status==2" icon="el-icon-thumb" size="small" type="primary" plain @click="queryContract(row)">查看合同</el-button>
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="common-pagenation">
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParam.pageBean.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="queryParam.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryParam.pageBean.total"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import utils from '@/utils.js'
const portal = window.context.portal
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      currentPage: 1,
      tableMaxHeight: 400,
      loading: false,
      tableLoading: false,
      tableData: [],
      showApproval: false,
      multipleSelection: [],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      searchForm: {
        status:'3',
      }, // 顶部搜索对象
      currentDataDel: [], // 删除时排查是否有在审核的数据
      deleteIds: [],
      contractStatus: '3',
      options: [
        {label: '签订中', value: '2'},
        {label: '已签订', value: '3'},
      ],
    }
  },
  mounted() {},
  created() {
    this.handleSearch()
  },
  methods: {
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.$http
        .post('${portal}/bizContract/v1/pageByOrg', this.queryParam)
        .then((res) => {
          console.log(res, 'res')
          this.tableLoading = false
          this.tableData = res.data.value.rows
          this.queryParam.pageBean.page = res.data.value.page
          this.queryParam.pageBean.pageSize = res.data.value.pageSize
          this.queryParam.pageBean.total = res.data.value.total
          this.tableData.forEach((item) => {
            this.$set(item, 'submiting', false)
            this.$set(item, 'signing', false)
          })
        })
    },
    // 顶部搜索
    handleSearch() {
      console.log(this.contractStatus);
      let obj = this.searchForm
      let querys = []
      for (let i in obj) {
        if (i == 'status') {
          if (this.contractStatus == '3') {
            querys.push({
              group: 'main',
              operation: 'EQUAL',
              parentGroup: '',
              property: i,
              relation: 'AND',
              value: '3',
            })
          }
          if (this.contractStatus == '2') {
            querys.push({
              group: 'main',
              operation: 'EQUAL',
              parentGroup: '',
              property: i,
              relation: 'AND',
              value: '2',
            })
          }
          if (this.contractStatus == '') {
            querys.push({
              group: 'main',
              operation: 'IN',
              parentGroup: '',
              property: i,
              relation: 'AND',
              value: ['2', '3'],
            })
          }
        } else {
          querys.push({
            group: 'main',
            operation:
              i == 'contrCode' || i == 'vendorName' || i == 'matCompanyName'
                ? 'LIKE'
                : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i],
          })
        }
      }
      this.queryParam.querys = querys
      this.loadData()
    },
    clearSearchForm() {
      this.searchForm = {
        status:'3'
      }
      this.contractStatus = '3'
      this.handleSearch()
    },
    // 下载合同
    downContract(){
      if(this.currentDataDel.length>1){
        this.$message.warning('请选择单条数据下载')
        return
      }
      let ids = this.currentDataDel.map(item=>{
        return item.threeContractId
      })
      let portal = window.context.portal
      let query = {
        url: `${portal}/bizContract/v1/downBatchContract`,
        method: 'post',
        data: ids,
        headers: {
          'Content-type': 'application/json',
        },
        responseType: 'blob', //将文件流转成blob对象
        noErrorMsg: true,
      }
      this.exporting = true
      this.$http
        .request(query)
        .then((res) => {
          if (res.status == 200) {
            this.$message.success('导出成功')
            this.exporting = false
          } else {
            this.$message.error('导出失败')
            this.exporting = false
          }
        })
        .catch(() => {
          this.exporting = false
        })
    },
    // 查看合同
    queryContract(row){
      row.submiting = true
      this.$http.post('${portal}/bizContract/v1/browseContract?threeContrId='+row.threeContractId).then(res=>{
        row.submiting = false
        if(res.data.state){
          window.open(res.data.message,'_blank')
        }
      })
    },
    handleSelectionChange(currentData) {
      this.currentDataDel = currentData
      this.deleteIds = currentData.map((item) => {
        return item.id
      })
    },
    handleCurrentChange(page) {
      this.params.pageBean = {...this.params.pageBean, page}
      this.loadData(this.params)
    },
    handleSizeChange(pageSize) {
      this.params.pageBean = {...this.params.pageBean, pageSize}
      this.loadData(this.params)
    },
  },
  computed: {
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px',
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.cloumn-btn {
  border: 1px solid #2761ff;
  padding: 3px 10px;
  border-radius: 5px;
  &:nth-child(1) {
    margin-right: 5px;
  }
  &:nth-child(2) {
    margin-right: 5px;
  }
  &:nth-child(1):hover {
    background-color: #2761ff;
    color: #fff;
  }
  &:nth-child(2):hover {
    background-color: #2761ff;
    color: #fff;
  }
  /deep/ i,
  span {
    font-size: 16px;
  }
}

/deep/ .el-tree-node__content {
  padding: 5px 0;
}

/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
</style>
