<!--
 * @Author: @liulin
 * @Date: 2022-05-19 11:29:11
 * @LastEditTime: 2022-08-10 16:20:34
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: \cqxy_pm_web\src\views\ProcessManagement\ScheduleManagement\contractPay.vue
-->
<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    :default-querys="[{property: 'pm.ID_', value: $route.params.id}]"
    :default-sorter="[{direction: 'DESC', property: 'registerDate'}]"
    :dialogTitle="dialogTitle"
    :dialogVisible="dialogVisible"
    :isDrag="false"
    :exportText="'导出审批单'"
    @select-all="handleSelect"
    @select="handleSelect"
    @handleDialogCancel="handleDialogCancel"
    @handleDialogClose="handleDialogCancel"
    @handleDialogSubmit="handleDialogSubmit"
    @handleDialogExport="handleDialogExport"
    @row-dblclick="handleRowDBClick"
  >
    <template v-slot:searchInput>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="80px"
        class="search-top"
        @submit.native.prevent
      >
        <el-date-picker
          v-model="searchForm.registerDate"
          type="date"
          placeholder="选择审批日期"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          style="width: 100%"
          @keyup.enter.native="findinfo"
        >
        </el-date-picker>
        <dict-select
          dictCode="jdgl-yt"
          :hasDefaultOption="true"
          clearable
          placeholder="请选择用途"
          v-model="searchForm.purpose"
          style="width: 100%"
          @keyup.enter.native="findinfo"
        />
        <el-button
          size="small"
          @click="findinfo"
          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-form>
    </template>
    <template v-slot:operationBtn>
      <el-button
        size="small"
        type="primary"
        @click="handleAdd"
        icon="el-icon-plus"
        >添加</el-button
      >
      <ht-delete-button
        :url="deleteUrl"
        :requestMethod="'POST'"
        :htTable="$refs.AtTable"
        >删除</ht-delete-button
      >
      <import-file :importData="importData" @load="load"></import-file>
      <el-button
        size="small"
        type="primary"
        @click="handleExport"
        icon="el-icon-download"
        plain
        style="margin-left: 10px"
        :loading="loading2"
        >导出</el-button
      >
      <el-button
        class="back"
        @click="handleBack"
        size="small"
        type="primary"
        icon="el-icon-back"
        plain
        >返回</el-button
      >
    </template>
    <template>
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column type="index" label="序号" align="center" width="55">
      </el-table-column>
      <el-table-column
        prop="projectInfo.projectName"
        label="项目名称"
        :show-overflow-tooltip="true"
        min-width="150"
        align="center"
      >
      </el-table-column>
      <ht-table-column
        prop="purpose"
        label="用途"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{row}">
          <div>{{ getDictName('jdgl-yt', row.purpose) }}</div>
        </template>
      </ht-table-column>
      <el-table-column
        prop="registerDate"
        label="审批日期"
        align="center"
        min-width="100"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="totalInvestment"
        label="总投资金额(元)"
        align="center"
        min-width="120"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="payeeAmountTotal"
        label="本次拨款额(元)"
        align="center"
        min-width="120"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{row}">
          {{ row.payeeAmountTotal && moneyFormatter(row.payeeAmountTotal) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="amountAppropriated"
        label="已拨款额(元)"
        align="center"
        min-width="120"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{row}">
          {{ row.amountAppropriated && moneyFormatter(row.amountAppropriated) }}
        </template>
      </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>
      <el-form
        ref="form"
        v-form
        data-vv-scope="form"
        :model="form"
        :show-message="false"
        :status-icon="true"
        v-if="dialogVisible"
        v-loading.fullscreen="loading"
      >
        <el-descriptions class="margin-top" :column="2" border>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 项目名称 </template>
            <el-input
              v-model="form.projectInfo.projectName"
              placeholder="请选择项目名称"
              clearable
              disabled
            >
              <el-button
                disabled
                type="primary"
                @click="handleFocusProject"
                slot="append"
                icon="el-icon-search"
              ></el-button>
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 合同名称 </template>
            <el-input
              v-model="form.contractInfo.name"
              placeholder="请选择合同名称"
              clearable
              disabled
            >
              <el-button
                disabled
                type="primary"
                @click="handleFocusContract"
                slot="append"
                icon="el-icon-search"
              ></el-button>
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 部室(子公司) </template>
            <el-input
              v-model="form.dept"
              placeholder="请输入部室"
              clearable
            ></el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 审批日期 </template>
            <el-date-picker
              v-model="form.registerDate"
              type="date"
              placeholder="选择审批日期"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              style="width: 100%"
            >
            </el-date-picker>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 用途 </template>
            <dict-select
              dictCode="jdgl-yt"
              :hasDefaultOption="true"
              clearable
              placeholder="请选择用途"
              v-model="form.purpose"
              style="width: 100%"
            />
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 总投资额 </template>
            <el-input
              v-model="form.totalInvestment"
              placeholder="请输入总投资额"
              clearable
            >
              <i slot="suffix">元</i>
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 已拨款额 </template>
            <el-input
              v-model="form.amountAppropriated"
              placeholder="请输入已拨款额"
              clearable
            >
              <i slot="suffix">元</i>
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 票据号码 </template>
            <el-input
              v-model="form.settlementBillNum"
              placeholder="请输入票据号码"
              clearable
            >
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :span="2"
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 本次拨款额 </template>
            <el-input
              v-model="form.payeeAmountTotal"
              placeholder="请输入本次拨款额"
              clearable
            >
              <i slot="suffix">元</i>
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :span="2"
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 项目进度情况及已完成工作量 </template>
            <el-input
              v-model="form.measurementData"
              placeholder="请输入项目进度情况及已完成工作量"
              type="textarea"
              rows="4"
              clearable
            >
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 收款单位 </template>
            <el-input
              v-model="form.registerPersonName"
              placeholder="请输入收款单位"
              type="textarea"
              rows="2"
              clearable
            >
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 账号 </template>
            <el-input
              v-model="form.handlerPerson"
              placeholder="请输入账号"
              type="textarea"
              rows="2"
              clearable
            >
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :span="2"
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 开户行 </template>
            <el-input
              v-model="form.registerPersonId"
              placeholder="请输入开户行"
              type="textarea"
              rows="2"
              clearable
            >
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item
            :span="2"
            :labelStyle="{width: '100px', 'text-align': 'center'}"
          >
            <template slot="label"> 备注 </template>
            <el-input
              v-model="form.remarks"
              placeholder="请输入备注"
              type="textarea"
              clearable
            >
            </el-input>
          </el-descriptions-item>
        </el-descriptions>
        <file-table
          @handleFile="handleFile"
          :file="form.accessoryInfo"
          :info="'上传支付审批相关文件'"
          style="margin-top: 10px"
        />
      </el-form>
    </template>
    <project-info-dialog
      ref="project"
      :single="true"
      @select="handleSelectProject"
    ></project-info-dialog>
    <project-contract-dialog
      ref="contract"
      :single="true"
      @select="handleSelectContract"
    ></project-contract-dialog>
  </at-table>
</template>
<script>
const eipUserSelector = () =>
  import('@/components/selector/EipUserSelector.vue')
import ProjectInfoDialog from '@/components/dialog/ProjectInfoDialog.vue'
import ProjectContractDialog from '@/components/dialog/ProjectContractDialog.vue'
import {duration} from 'moment'
import utils from '@/utils.js'
import req from '@/request.js'
export default {
  name: 'contractPay',
  components: {
    ProjectInfoDialog,
    ProjectContractDialog,
    // eipUserSelector
  },
  props: {
    pid: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      importData: {
        title: '支付审批导入',
        templateName: '支付审批模板.xls',
        downloadUrl: '/../fvue/static/excel/PaymentApproval.xlsx',
        importUrl:
          window.context.cqxy +
          '/biz/processManagement/progressManage/v1/importExcelData',
        // showProject: true,
        // showContract: false,
        projectId: '',
        contractId: '',
      },

      searchForm: {
        purpose: '',
        registerDate: '',
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      form: {}, //基本信息表单
      dialogVisible: false,
      dialogTitle: '添加',
      loading: false,
      loading2: false,
      rules: {
        'projectInfo.projectName': {
          required: true,
          message: '请选择项目名称',
          trigger: 'blur',
        },
        purpose: {
          required: true,
          message: '请选择用途',
          trigger: 'change',
        },
      },
      routerParams: null,
    }
  },
  computed: {
    deleteUrl: function () {
      return (
        window.context.cqxy + '/biz/processManagement/progressManage/v1/delete'
      )
    },
    // isShowRouter() {
    //   return this.routerParams && this.routerParams.rows ? false : ''
    // },
  },
  mounted() {
    this.routerParams = JSON.parse(sessionStorage.getItem('routerParams'))
    this.importData.projectId = this.routerParams.project.id
    this.importData.contractId = this.routerParams.contract.id
  },
  methods: {
    //重置
    handleReset() {
      this.searchForm.purpose = ''
      this.searchForm.registerDate = ''
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0,
      }
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    //打开合同信息
    handleFocusContract() {
      this.$refs.contract.open(this.form.projectId)
    },
    //选择合同信息
    handleSelectContract(data) {
      this.form.totalInvestment = data.contractAmount
      if (!data.payee) {
        this.form.registerPersonName = data.payeeAccountPhone
      } else {
        this.form.registerPersonName = data.payee
      }
      this.form.contractId = data.id
      this.form.contractInfo.name = data.name
      this.form.contractInfo.id = data.id
      this.form.handlerPerson = data.payeeAccount
      this.form.registerPersonId = data.payeeContactPhone
      this.$http
        .post(
          '${cqxy}/biz/processManagement/progressManage/v1/getAppropriatedl',
          params
        )
        .then((res) => {
          if (res.data.state) {
            this.form.amountAppropriated = res.data.value
          }
        })
    },
    //导入后重新加载数据
    load() {
      this.$refs.AtTable.load()
    },
    //导出
    handleExport() {
      this.loading2 = true
      let query = []
      let search = {
        contract_id_: this.routerParams.contract.id,
        purpose_: this.searchForm.purpose,
        register_date_: this.searchForm.registerDate,
      }
      for (let i in search) {
        if (search[i]) {
          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: [],
      }
      if (this.data.length > 0) {
        req
          .downloadPostCopy(
            '${cqxy}/biz/processManagement/progressManage/v1/exportDataToExcel',
            param
          )
          .then((res) => {
            if (res.status === 200) {
              this.loading2 = false
              this.$message.success('导出成功')
            } else {
              this.loading2 = false
              this.$message.error('导出失败')
            }
          })
      } else {
        this.loading2 = false
        this.$message.warning('暂无数据导出')
      }
    },
    // 导出审批单
    handleDialogExport() {
      console.log(this.form)
      if (this.form.id) {
        req
          .downloadGetCopy(
            '${cqxy}/biz/processManagement/progressManage/v1/exportApprovalForm?id=' +
              this.form.id
          )
          .then((res) => {
            if (res.status == 200) {
              this.$message.success('导出成功')
            } else {
              this.$message.error('导出失败')
            }
          })
      } else {
        this.$message.error('请先保存这条数据')
      }
    },
    handleAdd() {
      this.dialogVisible = true
      this.dialogTitle = '添加'
      let date = new Date()
      this.form = {
        purpose: 1,
        haveFundPlan: 1,
        handlerPerson: this.routerParams.contract.payeeAccount,
        projectId: this.routerParams.project.id,
        contractId: this.routerParams.contract.id,
        payeeAmountTotal: 0,
        registerPersonId: this.routerParams.contract.payeeContactPhone,
        registerPersonName: this.routerParams.contract.payeeAccountPhone,
        totalInvestment: this.routerParams.contract.contractAmount,
        amountAppropriated: '',
        registerDate: utils.formatDateYear(date),
        month: (date.getMonth() + 1).toString(),
        year: date.getFullYear().toString(),
        contractInfo: {
          name: this.routerParams.contract.name,
          id: this.routerParams.contract.id,
        },
        definiteInfo: [],
        dept: '建设部',
        projectInfo: {
          projectName: this.routerParams.project.projectName,
          projectCode: this.routerParams.project.projectCode,
        },
        accessoryInfo: [],
      }
      let params = {
        projectId: this.form.projectId,
        registerDate: this.form.registerDate,
        contractId: this.form.contractId,
      }
      this.$http
        .post(
          '${cqxy}/biz/processManagement/progressManage/v1/getAppropriatedl',
          params
        )
        .then((res) => {
          if (res.data.state) {
            this.form.amountAppropriated = res.data.value
          }
        })
    },
    handleRowDBClick(data) {
      this.dialogTitle = '修改'
      this.$http
        .post(
          '${cqxy}/biz/processManagement/progressManage/v1/detail/' + data.id
        )
        .then((res) => {
          this.dialogVisible = true
          let response = res.data.value
          if (!response.contractInfo) {
            let obj = {
              contractInfo: {},
            }
            this.form = {...response, ...obj}
          } else {
            this.form = response
          }
          this.form.year = this.form.year.toString()
          this.form.month = this.form.month.toString()
        })
    },
    handleFocusProject() {
      this.$refs.project.open()
    },
    handleSelectProject(data) {
      this.form.contractInfo.name = ''
      this.form.contractInfo.id = ''
      this.form.projectId = data.id
      this.form.projectInfo.projectCode = data.projectCode
      this.form.projectInfo.projectName = data.projectName
      // this.form.handlerPerson = data.projectManager
      this.$refs.form.clearValidate()
    },
    handleSelect(selection) {},
    handleDialogCancel() {
      this.dialogVisible = false
      /* 发送一个事件用于侧边栏按钮监听是否显示 */
      this.$bus.$emit('handleAsideBtn', true)
    },
    handleFile(file) {
      this.form.accessoryInfo = file
    },
    //保存
    handleDialogSubmit() {
      if (this.form.accessoryInfo && this.form.accessoryInfo.length > 0) {
        this.form.accessoryInfo.forEach((item) => {
          if (item.id) {
            delete item.id
          }
        })
      }
      this.$http
        .post('${cqxy}/biz/processManagement/progressManage/v1/save', this.form)
        .then((res) => {
          if (res.data.state) {
            this.$message.success(res.data.message)
            this.$refs.AtTable.load()
            this.form = res.data.value
          } else {
            this.$message.error(res.data.message)
          }
        })
    },
    loadData(param, cb) {
      this.routerParams = JSON.parse(sessionStorage.getItem('routerParams'))
      let querys = []
      let search = {
        contract_id_: this.routerParams.contract.id,
        purpose_: this.searchForm.purpose,
        register_date_: this.searchForm.registerDate,
      }
      for (let i in search) {
        if (search[i]) {
          querys.push({
            group: 'main',
            operation: 'EQUAL',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
      if (param.querys && param.querys.length) {
        param.querys.push(...querys)
      } else {
        param.querys = querys
      }
      this.$http
        .post('${cqxy}/biz/processManagement/progressManage/v1/page', param)
        .then(
          (resp) => {
            let response = resp.data.value
            this.data = response.rows
            this.pageResult = {
              page: response.page,
              pageSize: response.pageSize,
              total: response.total,
            }
          },
          (error) => {
            reject(error)
          }
        )
        .finally(() => {
          if (cb) {
            cb()
          }
        })
    },
    findinfo() {
      this.$refs.AtTable.load()
    },
    handleBack() {
      this.$router.push({
        name: 'ProjectFlowChart',
        params: {
          id: this.routerParams.project.id,
          alias: this.routerParams.alias,
        },
      })
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';

/deep/ .el-step.is-simple:not(:last-of-type) .el-step__title {
  max-width: 60% !important;
}
.button-box {
  display: flex;
  justify-content: flex-end;
  // position: absolute;
  // bottom: 30px;
  // right: 30px;
  div {
    padding: 10px;
    background: #2f86ff;
    margin: 10px;
    border-radius: 4px;
    cursor: pointer;
    color: #ffffff;
  }
}
.loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  i {
    font-size: 30px;
  }
}
.modelDiv {
  position: absolute;
  width: 1000px;
  height: 700px;
  top: 50px;
  left: 0;
  background-color: rgba($color: #000000, $alpha: 0.2);
}
.btnDetailed {
  display: flex;
  // margin-bottom: 20px;
}
// .totalBorder {
//   width: 100%;
// }
.totalBorder > div {
  padding-right: 10px;
  float: right;
  font-size: 16px;
}
.totalBorder > div > span {
  color: red;
}
</style>
