<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
            placeholder="请输入合同编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="供货方">
          <el-input
            size="small"
            v-model="searchForm.vendorName"
            clearable
            placeholder="请输入供货方"
          ></el-input>
        </el-form-item>
        <el-form-item label="采购方">
          <el-input
            size="small"
            v-model="searchForm.matCompanyName"
            clearable
            placeholder="请输入采购方"
          ></el-input>
        </el-form-item>
        <el-form-item label="签订日期">
          <el-date-picker
            size="small"
            v-model="searchForm.signData"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="请选择签订日期"
          ></el-date-picker>
        </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>
        </div> -->
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @row-dblclick="handleDetail"
            @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
              min-width="190px"
              fixed="right"
              align="center"
              label="操作"
            >
              <template v-slot="{row}">
                <template>
                  <el-button
                    icon="el-icon-thumb"
                    type="primary"
                    plain
                    :loading="signing"
                    v-if="row.status==5"
                    size="small"
                    @click="handleSign(row)"
                  >
                    签订合同
                  </el-button>
                  <el-tag type="info" v-if="row.status==2">签订中</el-tag>
                  <el-button type="primary" size="small" plain  v-if="row.status==4" @click="handleCulit(row)">审核</el-button>
                  <el-tag type="info" v-if="row.status==7">审核中</el-tag>
                  <el-button
                    type="primary"
                    :loading="submiting"
                    v-if="row.status==4"
                    plain 
                    @click="clickWithdraw(row)"
                    size="small"
                  >
                    撤回
                  </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,
        },
        sorter: [
          {
            direction: 'DESC',
            property: 'create_time_',
          },
        ],
      },
      searchForm: {
        status: ['2', '4', '5','7'],
      }, // 顶部搜索对象
      currentDataDel: [], // 删除时排查是否有在审核的数据
      deleteIds: [],
    }
  },
  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() {
      let obj = this.searchForm
      let querys = []
      for (let i in obj) {
        if (i == 'status') {
          querys.push({
            group: 'main',
            operation: 'IN',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i],
          })
        } 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: ['2', '4', '5','7'],
      }
      this.handleSearch()
    },
    // 签订合同
    handleSign(row){
      row.signing = true
      this.$http.post('${portal}/bizContract/v1/createContract?id='+row.id).then(res=>{
        if(res.data.state){
          this.$message.success('操作成功')
          this.handleSearch()
        }
        window.open(res.data.message,'_blank')
        row.signing = false
      })
    },
    // 审核合同
    handleCulit(row){
      row.signing = true
      this.$http
          .post(
            '${portal}/bizContract/v1/updateStatus?status=5' +
              '&ids=' +row.id
          )
          .then((res) => {
            row.signing = false
            if (res.data.state) {
              this.$message.success(res.data.message)
            }
            this.loadData()
          })
    },
    // 撤回合同
    clickWithdraw(row) {
      row.submiting = true
      this.$http
        .post(
          '${portal}/bizContract/v1/updateStatus?status=1' + '&ids=' + row.id
        )
        .then((res) => {
          row.submiting = false
          if (res.data.state) {
            this.$message.success(res.data.message)
          }
          this.loadData()
        })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'contractingDetail',
        query: {detailRows: rows, pageBean: this.params},
      })
    },
    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>
