<template>
  <div>
    <at-table
      @load="loadData"
      :data="data"
      :page-result="pageResult"
      ref="AtTable"
      :cellClassNameFn="cellClassNameFn"
      @select-all="handleSelect"
      @select="handleSelect"
      @row-dblclick="handleRowDBClick"
      :justShowSearch="justShowSearch"
    >
      <template v-slot:searchInput>
        <el-form
          ref="searchForm"
          :model="searchForm"
          label-width="80px"
          class="search-top"
        >
          <el-select
            v-if="isShow"
            v-model="searchForm.createTime"
            clearable
            placeholder="请选择年份"
            @change="handleSearch"
          >
            <el-option
              v-for="item in optionYears"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <dict-select
            v-if="isShow"
            v-model="searchForm.projectType"
            dictCode="xmgl-xmlx"
            validate="required"
            placeholder="请选择项目类型"
            :clearable="true"
            @input="handleSearch"
          />
          <el-input
            placeholder="请输入项目名称"
            v-if="isShow"
            v-model="searchForm.projectName"
            style="width: 100%"
            @input="handleSearch"
          ></el-input>
          <dict-select
            v-model="searchForm.changeType"
            dictCode="gcgl-bglx"
            validate="required"
            placeholder="请选择变更类型"
            :clearable="true"
            @input="handleSearch"
          />
          <el-select
            v-model="searchForm.changePrice"
            placeholder="请选择变更金额区间"
            clearable
            @change="handleSearch"
          >
            <el-option
              v-for="item in AmountOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-button
            size="small"
            @click="handleSearch"
            type="primary"
            class="el-icon-search search-btn"
            >查询</el-button
          >
          <el-button
            size="small"
            @click="handleReset"
            type="primary"
            plain
            class="el-icon-refresh"
            >重置</el-button
          >
          <el-button
            size="small"
            type="primary"
            @click="handleExport"
            :loading="loading"
            icon="el-icon-download"
            plain
            style="margin-left: 10px"
            >导出</el-button
          >
        </el-form>
      </template>
      <template v-slot:seacrhMoreInput v-if="isShow">
        <div class="moreInput">
          <el-date-picker
            unlink-panels
            v-model="searchForm.yearMonth"
            value-format="yyyy-MM"
            type="monthrange"
            range-separator="至"
            start-placeholder="变更时间"
            end-placeholder="变更时间"
            @change="handleSearch"
          >
          </el-date-picker>
        </div>
      </template>
      <template>
        <!-- <el-table-column type="selection" width="55" align="center">
        </el-table-column> -->
        <el-table-column type="index" label="序号" width="55" align="center">
        </el-table-column>
        <el-table-column
          prop="projectInfo.projectName"
          label="项目名称"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <!-- <el-table-column
          prop="changeName"
          label="变更名称"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <ht-table-column
          prop="changeType"
          label="变更类型"
          align="center"
          width="130"
          :show-overflow-tooltip="true"
          :filters="getDictData('gcgl-bglx')"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.changeType !== '----'">
              {{ getDictName('gcgl-bglx', scope.row.changeType) }}
            </span>
            <span v-else>----</span>
          </template>
        </ht-table-column>
        <el-table-column
          prop="changeCode"
          label="变更编号"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="changePrice"
          label="变更投资金额(万元)"
          align="center"
          width="170"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="changeDate"
          label="变更时间"
          align="center"
          width="130"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <!-- <el-table-column
          prop="changeUnit"
          label="审批机构"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <el-table-column
          prop="changeContent"
          label="变更内容"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="remarks"
          label="备注"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
      </template>
      <!-- 弹框 -->
      <!-- <template v-slot:dialogContent> </template> -->
    </at-table>
    <el-dialog
      title="详情"
      :visible.sync="dialogVisible"
      width="1000px"
      top="8vh"
    >
      <div class="mainBox">
        <!-- <el-descriptions :column="2" :size="size" border>
          <el-descriptions-item label="项目名称">
            {{ details.projectName }}
          </el-descriptions-item>
          <el-descriptions-item label="变更编号">
            {{ details.changeCode }}
          </el-descriptions-item>
          <el-descriptions-item label="变更名称">
            {{ details.changeName }}
          </el-descriptions-item>
          <el-descriptions-item label="变更类型">
            {{ details.changeType }}
          </el-descriptions-item>
          <el-descriptions-item label="变更时间">
            {{ details.changeTime }}
          </el-descriptions-item>
          <el-descriptions-item label="变更金额(万元)">
            {{ details.changeMoney }}
          </el-descriptions-item>
          <el-descriptions-item label="审批机构">
            {{ details.examineUnit }}
          </el-descriptions-item>
          <el-descriptions-item label="备注">
            {{ details.remark }}
          </el-descriptions-item>
          <el-descriptions-item label="变更内容简述">
            {{ details.changeContent }}
          </el-descriptions-item>
        </el-descriptions> -->
        <table>
          <tr>
            <td class="bgColor">项目名称</td>
            <td style="width: 240px">{{ details.projectInfo.projectName }}</td>
            <td class="bgColor">变更编号</td>
            <td style="width: 240px">{{ details.changeCode }}</td>
          </tr>
          <tr>
            <td class="bgColor">变更类型</td>
            <td>{{ getDictName('gcgl-bglx', details.changeType) }}</td>
            <td class="bgColor">变更时间</td>
            <td>{{ details.changeDate }}</td>
          </tr>
          <tr>
            <td class="bgColor">变更金额(万元)</td>
            <td>{{ details.changePrice }}</td>
            <td class="bgColor">审批机构</td>
            <td>{{ details.changeUnit }}</td>
          </tr>
          <tr>
            <td class="bgColor">变更内容</td>
            <td :colspan="3">
              {{ details.changeContent }}
            </td>
          </tr>
          <tr>
            <td class="bgColor">备注</td>
            <td :colspan="3">{{ details.remark }}</td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false"
          >关闭</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import req from '@/request.js'
const BigNumber = require('bignumber.js')
export default {
  name: 'contractChangeAccount',
  props: {
    pid: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: true,
    },
    alias: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      total: {
        projectInfo: {
          projectName: '合计',
        },
        changeType: '----',
        changeCode: '----',
        changePrice: 0,
        changeDate: '----',
        changeContent: '----',
        remarks: '----',
      },
      justShowSearch: this.isShow,
      url: '',
      size: 'medium',
      loading: false,
      searchForm: {
        projectName: '',
        changeCode: '',
        changeType: '',
        createTime: new Date().getFullYear(), //当前年份
        projectType: '', //项目类型
        changePrice: '',
      },
      optionYears: [], //顶部搜索年份选择数组
      details: {
        projectInfo: {},
      },
      data: [],
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      form: {},
      dialogVisible: false,
      AmountOptions: [
        {
          label: '小于20万',
          value: 'less,20',
        },
        {
          label: '50 ~ 100万',
          value: '50,100',
        },
        {
          label: '100 ~ 200万',
          value: '100,200',
        },
        {
          label: '200 ~ 400万',
          value: '200,400',
        },
        {
          label: '400万以上',
          value: 'great,400',
        },
      ],
    }
  },
  mounted() {
    this.getYear()
    if (this.isShow) {
      this.$refs.projectInfo.getProjectData()
    }
  },
  methods: {
    //获取顶部搜索的年份区间
    getYear() {
      const myDate = new Date()
      const startYear = 2006 //起始年份
      const endYear = myDate.getFullYear() //结束年份
      for (let i = endYear; i >= startYear; i--) {
        this.optionYears.push({
          label: i + '年',
          value: i,
        })
      }
    },
    handleReset() {
      this.searchForm.changePrice = ''
      this.searchForm.projectName = ''
      this.searchForm.changeCode = ''
      this.searchForm.changeType = ''
      this.searchForm.projectType = ''
      this.searchForm.createTime = ''
      this.searchForm.yearMonth = []
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0,
      }
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    //导出
    handleExport() {
      this.loading = true
      let query = []
      let search = {
        'pm.PROJECT_NAME_': this.searchForm.projectName,
        change_code_: this.searchForm.changeCode,
        change_type_: this.searchForm.changeType,
        project_type_: this.searchForm.projectType,
        CREATE_TIME_: this.searchForm.createTime,
        year_month_: this.searchForm.yearMonth,
        change_price_: this.searchForm.changePrice,
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
        ;(search.purpose_ = this.searchForm.purpose),
          // search.change_price_=this.searchForm.changePrice,
          delete search.CREATE_TIME_
      }
      for (let i in search) {
        if (search[i]) {
          if (i == 'CREATE_TIME_') {
            query.push(
              {
                group: 'createTimeGroup',
                operation: 'BETWEEN',
                parentGroup: '',
                property: 'commencement_time_',
                relation: 'OR',
                value: [
                  `${search[i]}-01-01 00:00:00`,
                  `${search[i]}-12-31 23:59:59`,
                ],
              }
              // {
              //   group: 'createTimeSubGroup',
              //   operation: 'LESS_EQUAL',
              //   parentGroup: 'createTimeGroup',
              //   property: 'commencement_time_',
              //   relation: 'AND',
              //   value: `${search[i]}-12-31 23:59:59`,
              // },
              // {
              //   group: 'createTimeSubGroup',
              //   operation: 'LESS',
              //   parentGroup: 'createTimeGroup',
              //   property: 'PROJECT_STATUS_',
              //   relation: 'AND',
              //   value: '12',
              // }
            )
          } else if (i == 'pm.PROJECT_NAME_') {
            query.push({
              group: 'advance',
              operation: 'LIKE',
              property: i,
              relation: 'AND',
              value: search[i],
            })
          } else if (i == 'change_price_') {
            let arr = search[i].split(',')
            if (arr[0] == 'less') {
              query.push({
                group: 'advance',
                operation: 'LESS',
                property: i,
                relation: 'AND',
                value: arr[1],
              })
            } else if (arr[0] == 'great') {
              query.push({
                group: 'advance',
                operation: 'GREAT',
                property: i,
                relation: 'AND',
                value: arr[1],
              })
            } else {
              query.push(
                {
                  group: 'advance',
                  operation: 'GREAT_EQUAL',
                  property: i,
                  relation: 'AND',
                  value: arr[0],
                },
                {
                  group: 'advance',
                  operation: 'LESS_EQUAL',
                  property: i,
                  relation: 'AND',
                  value: arr[1],
                }
              )
            }
          } else if (i == 'year_month_') {
            if (search[i].length) {
              let lastDate = search[i][1]
              let str = lastDate.split('-')
              let year = parseInt(str[0])
              let month = parseInt(str[1])
              let date = new Date(year, month, 0).getDate()
              let arr = [`${search[i][0]}-01`, `${search[i][1]}-${date}`]
              query.push({
                group: 'createTimeGroup',
                operation: 'BETWEEN',
                parentGroup: '',
                property: 'change_date_',
                relation: 'OR',
                value: arr,
              })
            }
          } else {
            query.push({
              group: 'advance',
              operation: 'LIKE',
              property: i,
              relation: 'AND',
              value: search[i],
            })
          }
        }
      }
      let param = {
        pageBean: {
          page: 1,
          pageSize: -1,
          showTotal: true,
        },

        querys: query,
        sorter: [],
      }
      req
        .downloadPostCopy(
          '${cqxy}/biz/processManagement/changeManagement/v1/exportDataToExcel',
          param
        )
        .then((res) => {
          if (res.status == 200) {
            this.loading = false
            this.$message.success('导出成功')
          } else {
            this.loading = false
            this.$message.error('导出失败')
          }
        })
    },
    //查询
    handleSearch() {
      this.$refs.AtTable.load()
    },
    handleRowDBClick(data) {
      this.dialogVisible = true
      this.details = data
    },

    handleSelect(selection) {},
    loadData(param, cb) {
      let query = []
      let search = {
        'pm.PROJECT_NAME_': this.searchForm.projectName,
        change_code_: this.searchForm.changeCode,
        change_type_: this.searchForm.changeType,
        project_type_: this.searchForm.projectType,
        CREATE_TIME_: this.searchForm.createTime,
        year_month_: this.searchForm.yearMonth,
        change_price_: this.searchForm.changePrice,
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
        // search.change_price_=this.searchForm.changePrice,
        ;(search.purpose_ = this.searchForm.purpose), delete search.CREATE_TIME_
      }
      for (let i in search) {
        if (search[i]) {
          if (i == 'CREATE_TIME_') {
            query.push(
              {
                group: 'createTimeGroup',
                operation: 'BETWEEN',
                parentGroup: '',
                property: 'commencement_time_',
                relation: 'OR',
                value: [
                  `${search[i]}-01-01 00:00:00`,
                  `${search[i]}-12-31 23:59:59`,
                ],
              }
              // {
              //   group: 'createTimeSubGroup',
              //   operation: 'LESS_EQUAL',
              //   parentGroup: 'createTimeGroup',
              //   property: 'commencement_time_',
              //   relation: 'AND',
              //   value: `${search[i]}-12-31 23:59:59`,
              // },
              // {
              //   group: 'createTimeSubGroup',
              //   operation: 'LESS',
              //   parentGroup: 'createTimeGroup',
              //   property: 'PROJECT_STATUS_',
              //   relation: 'AND',
              //   value: '12',
              // }
            )
          } else if (i == 'pm.PROJECT_NAME_') {
            query.push({
              group: 'advance',
              operation: 'LIKE',
              property: i,
              relation: 'AND',
              value: search[i],
            })
          } else if (i == 'change_price_') {
            let arr = search[i].split(',')
            if (arr[0] == 'less') {
              query.push({
                group: 'advance',
                operation: 'LESS',
                property: i,
                relation: 'AND',
                value: arr[1],
              })
            } else if (arr[0] == 'great') {
              query.push({
                group: 'advance',
                operation: 'GREAT',
                property: i,
                relation: 'AND',
                value: arr[1],
              })
            } else {
              query.push(
                {
                  group: 'advance',
                  operation: 'GREAT_EQUAL',
                  property: i,
                  relation: 'AND',
                  value: arr[0],
                },
                {
                  group: 'advance',
                  operation: 'LESS_EQUAL',
                  property: i,
                  relation: 'AND',
                  value: arr[1],
                }
              )
            }
          } else if (i == 'year_month_') {
            if (search[i].length) {
              let lastDate = search[i][1]
              let str = lastDate.split('-')
              let year = parseInt(str[0])
              let month = parseInt(str[1])
              let date = new Date(year, month, 0).getDate()
              let arr = [`${search[i][0]}-01`, `${search[i][1]}-${date}`]
              query.push({
                group: 'createTimeGroup',
                operation: 'BETWEEN',
                parentGroup: '',
                property: 'change_date_',
                relation: 'OR',
                value: arr,
              })
            }
          } else {
            query.push({
              group: 'advance',
              operation: 'EQUAL',
              property: i,
              relation: 'AND',
              value: search[i],
            })
          }
        }
      }
      if (param.querys && param.querys.length) {
        param.querys.push(...query)
      } else {
        param.querys = query
      }
      this.$http
        .post('${cqxy}/biz/processManagement/changeManagement/v1/page', param)
        .then(
          (resp) => {
            let response = resp.data.value
            let data = response.rows
            let total = this.calculationTotal(data, 'changePrice')
            this.total.changePrice = total
            if (data.length !== 0) {
              data.push(this.total)
            }
            this.data = data
            this.pageResult = {
              page: response.page,
              pageSize: response.pageSize,
              total: response.total,
            }
          },
          (error) => {
            reject(error)
          }
        )
        .finally(() => cb())
    },
    // 计算列表合同金额合计金额
    calculationTotal(data, key) {
      let total = new BigNumber(0)
      if (key && data && data.length !== 0) {
        data.forEach((item) => {
          if (item[key]) {
            total = total.plus(item[key])
          } else {
            total = total.plus(0)
          }
        })
      }
      return this.moneyFormatter(total)
    },
    cellClassNameFn(row) {
      // 列表项目名称为【合计】隐藏复选框
      if (row.row.projectInfo.projectName === '合计') {
        return 'table-column-hidden'
      }
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';

/deep/ .el-input__suffix > .el-input__icon {
  display: none;
}
/deep/ .el-table--border th .cell {
  color: #000;
}

/deep/ .el-dialog__body {
  padding-top: 20px;
  padding-bottom: 20px;
}
table {
  width: 100%;
  border-collapse: collapse;
}

table,
table tr td {
  border: 1px solid #ebeef5;
}

table tr td {
  padding: 15px 10px;
  // text-align: center;
}
.bgColor {
  background: #fafafa;
  color: #909399;
  width: 150px;
}
/deep/ .el-table thead.is-group th {
  border-color: #ebeef5;
}
.show-slected {
  font-size: 18px;
  i {
    font-style: normal;
    font-weight: 700;
    margin: 0 5px;
    color: #f40;
  }
}
/deep/ .table-column-hidden .el-checkbox__input {
  display: none;
}
/deep/ .table-column-hidden .cell > div {
  display: none;
}
</style>
