<template>
  <div class="app-container">
    <div class="title-container">
      <breadcrumb id="breadcrumb-container" class="breadcrumb-container"/>
    </div>

    <y-page-list-layout :page-list="pageData" :page-para="listQuery" :get-page-list="getList">
      <template slot="left">
        <el-input
          v-model="listQuery.itemName"
          placeholder="项目名称"
          clearable
          style="margin-left: 20px;width: 320px;float: left;"
        >
        </el-input>
        <el-button
          class="filter-item"
          style="margin-left: 10px;float: left;"
          type="primary"
          @click="searchList"
          round
        >搜索
        </el-button>
        <el-button
          class="filter-item"
          style="float: left;"
          round
          type="warning"
          @click="resetSearch()"
        >重置
        </el-button>
      </template>
      <parentTable
        v-loading="listLoading"
        :data="pageData.records"
        slot="table"
        style="width: 100%;"
      >
        <el-table-column label="事业部流水号" align="center" width="160">
          <template slot-scope="{row}">
            <span>{{ row.businessNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目编号" align="center" width="160">
          <template slot-scope="{row}">
            <span>{{ row.oaNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" width="160">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户经理" align="center" width="160">
          <template slot-scope="{row}">
            <span>{{ row.clientManager }}</span>
          </template>
        </el-table-column>
        <el-table-column label="委托单位" align="center" width="160">
          <template slot-scope="{row}">
            <span>{{ row.clientUnit}}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同签订时间" align="center" width="160">
          <template slot-scope="{row}">
            <span>{{ row.signDate}}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同金额（元）" align="center" width="160">
          <template slot-scope="{row}">
            <span>{{ row.amount}}</span>
          </template>
        </el-table-column>
        <el-table-column label="已收款金额（元）" align="center" width="160">
          <template slot-scope="{row}">
            <span style="color: green">{{ row.payedAmount}}</span>
          </template>
        </el-table-column>
        <el-table-column label="未收款金额（元）" align="center" width="160">
          <template slot-scope="{row}">
            <span style="color: red">{{ row.notPayedAmount}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="{row}">
            <PermissionButton
              menu-code="_views_market_payment_list"
              class-name="filter-item"
              type="primary"
              :page-jump="true"
              :page-query="{id: row.id}"
              round
              size="mini"
            />
            <PermissionButton
              menu-code="_views_market_payment_add"
              class-name="filter-item"
              type="success"
              round
              size="mini"
              @click="handleAdd(row.id,row.name)"
            />
          </template>
        </el-table-column>
      </parentTable>
    </y-page-list-layout>
    <el-dialog
      width="600px"
      title="新增回款"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      custom-class="paymentDialog"
    >
      <el-form
        ref="dataForm"
        v-loading="dialogLoading"
        :model="temp"
        label-position="right"
        label-width="110px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="项目名称：" prop="itemName">
          <el-input v-model="temp.itemName" class="filter-item" readonly disabled/>
        </el-form-item>

        <el-form-item label="回款名称：" prop="name" :rules="{required: true, message: '请填写回款名称', trigger: 'blur'}">
          <el-input v-model="temp.name" class="filter-item"/>
        </el-form-item>

        <el-form-item label="回款金额：" prop="amount" :rules="{required: true, message: '请填写回款金额', trigger: 'blur'}">
          <el-input v-model.number="temp.amount" class="filter-item" type="number">
            <i slot="suffix" style="font-size:normal;margin-right: 10px;line-height: 30px">元</i>
          </el-input>
        </el-form-item>
        <el-form-item
          label="回款日期："
          prop="paymentDate"
          :rules="{required: true, message: '请选择日期', trigger: 'blur'}"
          class="filter-item"
        >
          <el-date-picker
            v-model="temp.paymentDate"
            type="date"
            value-format="yyyy-MM-dd"
            style="width: 100%"
            placeholder="选择日期"
            class="filter-item"
          />
        </el-form-item>
        <el-form-item label="">
          <el-button @click="dialogFormVisible = false">
            取消
          </el-button>
          <el-button type="primary" @click="save()">
            保存
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import YPageListLayout from '@/components/YPageListLayout'
  import Breadcrumb from '@/components/Breadcrumb'
  import PermissionButton from '@/components/PermissionButton/PermissionButton'

  export default {
    name: 'ViewsMarketPaymentList',
    components: {
      Breadcrumb,
      YPageListLayout,
      PermissionButton,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        isDisable:false,
        tableKey: 0,
        pageData: { records: [] },
        total: 20,
        listLoading: true,
        dialogLoading:false,
        dialogFormVisible:false,
        listQuery: {
          page: 1,
          size: 10,
          // name: '',
          // staffNo: '',
          descs: 'id',
          itemName:null
        },
        listQueryKey: 'keyword',
        importLoading: false,
        temp:{
          name:'',
          amount:'',
          itemName:'',
          itemId:''
        }
      }
    },
    created() {
      const that = this;
      that.getList()
    },
    methods: {
      save(){
        if (this.temp.itemId){
          this.$refs.dataForm.validate(valid => {
            if (valid) {
              this.$api.payment.add(Object.assign({}, this.temp, {
              })).then(res => {
                if (res.code === 200) {
                  this.$notify({
                    title: '成功',
                    message: '新增成功',
                    type: 'success',
                    duration: 2000
                  });
                  this.getList();
                  this.temp.itemId = null;
                  this.temp.name = null;
                  this.temp.amount = null;
                  this.temp.paymentDate = null;
                  this.dialogFormVisible = false
                }
              }).catch(() => {
                this.dialogFormVisible = false
              })
            }
          })
        }
      },
      handleAdd(itemId, itemName) {
        const that = this;
        that.temp.itemId = itemId;
        that.temp.itemName = itemName;
        that.dialogFormVisible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      resetSearch() {
        this.listQuery = {
          current: 1,
          size: 10,
          descs: 'id',
          me: false
        }
        this.getList()
      },
      searchList() {
        // 重置分页
        this.listQuery.page = 1
        this.listQuery.size = 20
        this.getList()
      },
      getList() {
        const that = this
        this.listLoading = true
        const key = {}
        this.$api.payment.itemPayment(Object.assign({}, that.listQuery, key)).then((res) => {
            that.pageData = res.data
            setTimeout(() => {
              that.listLoading = false
            }, 200)
          })
          .catch(() => {
            that.listLoading = false
          })
      },
    },
  }
</script>
<style lang="scss" scoped>
  .right {
    flex: 1;

    .title {
      font-size: 16px;
      font-weight: 500;
      color: rgba(51, 51, 51, 1);
      line-height: 35px;
      margin-bottom: 8px;
    }

    .menu-2-box {
      display: flex;
      flex-wrap: wrap;
      width: 100%;
    }

    .menu-2-item {
      display: flex;
      align-items: center;
      color: #656565;
      font-size: 12px;
      width: 230px;
      height: 101px;
      background: rgb(255, 185, 129);
      border-radius: 3px;
      padding-left: 20px;
      margin-right: 10px;
      margin-bottom: 10px;
      cursor: pointer;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);

      .text {
        margin-left: 16px;
      }
    }
  }
  /deep/.paymentDialog{
    border-radius: 20px;
  }
</style>
