<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">
        <PermissionButton
          menu-code="_views_item_add"
          class-name="filter-item"
          type="primary"
          size="mini"
          :page-jump="true"
          round
          style="float: left"
        />
        <el-select
          v-model="listQuery.state"
          placeholder="状态"
          clearable
          filterable
          style="margin-left: 20px;width: 100px;float: left;"
          class="filter-item"
        >
          <el-option label="进行中" value="进行中"/>
          <el-option label="已完成" value="已完成"/>
          <el-option label="未开始" value="未开始"/>
        </el-select>
        <el-input
          v-model="listQuery.name"
          placeholder="项目名称"
          clearable
          style="margin-left: 20px;width: 320px;float: left;"
        >
        </el-input>
        <el-input
          v-model="listQuery.businessNo"
          placeholder="流水号"
          clearable
          style="margin-left: 20px;width: 320px;float: left;"
        >
        </el-input>
        <el-input
          v-model="listQuery.clientManager"
          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="200">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="事业部流水号" align="center" width="135">
          <template slot-scope="{row}">
            <span>{{ row.businessNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目编号" align="center" width="130">
          <template slot-scope="{row}">
            <span>{{row.belongTo}}{{ row.oaNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属部门" align="center" width="130">
          <template slot-scope="{row}">
            <span>{{ row.departmentName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目类型" align="center" width="130">
          <template slot-scope="{row}">
            <span>{{ row.cate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="业务来源" align="center" width="130">
          <template slot-scope="{row}">
            <span>{{ row.businessSource }}</span>
          </template>
        </el-table-column>
        <el-table-column label="委托单位" align="center" width="130">
          <template slot-scope="{row}">
            <span>{{ row.clientUnit}}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户名字" align="center" width="130">
          <template slot-scope="{row}">
            <span>{{ row.clientName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同金额" align="center" width="100">
          <template slot-scope="{row}">
            <span>{{ row.amount}}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户经理" align="center" width="100">
          <template slot-scope="{row}">
            <span>{{ row.clientManager}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="{row}">
            <el-tag>{{ row.state}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="当前阶段" align="center" width="100">
          <template slot-scope="{row}">
            <el-tag>{{computedStageName(row.stageName)}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="电子合同" align="center" width="100">
          <template slot-scope="{row}">
            <el-button v-if="row.contractUrl"  type="success" plain @click="downloadContract(row.contractUrl)">下载</el-button>
            <el-button v-else plain disabled>下载</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300">
          <template slot-scope="{row}">
            <PermissionButton
              menu-code="_views_item_plan"
              class-name="filter-item"
              name="排期"
              type="info"
              :page-jump="true"
              :page-query="{id: row.id,itemName: row.name}"
              round
              size="mini"
            />
            <PermissionButton
              menu-code="_view_item_log_write"
              class-name="filter-item"
              name=""
              type="success"
              :page-jump="true"
              :page-query="{id: row.id,itemName:row.name,stageName:row.stageName,stageId:row.stageId}"
              round
              size="mini"
            />
            <PermissionButton
              menu-code="_views_item_rate"
              class-name="filter-item"
              name="日志"
              type="info"
              :page-jump="true"
              :page-query="{id: row.id}"
              round
              size="mini"
            />
            <PermissionButton
              menu-code="_views_item_detail"
              class-name="filter-item"
              name=""
              type="primary"
              :page-jump="true"
              :page-query="{id: row.id}"
              round
              size="mini"
            />
            <PermissionButton
              menu-code="_views_item_detail_readonly"
              class-name="filter-item"
              name=""
              type="primary"
              :page-jump="true"
              :page-query="{id: row.id}"
              round
              size="mini"
            />
            <PermissionButton
              menu-code="_views_item_remove"
              class-name="filter-item"
              name=""
              type="danger"
              round
              size="mini"
              @click="deleteInfo(row.id)"
            />
          </template>
        </el-table-column>
      </parentTable>
    </y-page-list-layout>
  </div>
</template>
<script>
  import YPageListLayout from '@/components/YPageListLayout'
  import Breadcrumb from '@/components/Breadcrumb'
  import PermissionButton from '@/components/PermissionButton/PermissionButton'
  import { fileDown } from '../../utils/file'

  export default {
    name: 'ViewsItemList',
    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,
        listQuery: {
          page: 1,
          size: 10,
          // name: '',
          // staffNo: '',
          descs: 'id',
          state:null
        },
        listQueryKey: 'keyword',
        importLoading: false,
      }
    },
    created() {
      const that = this;
      if (this.$route.query.businessNo) {
        this.listQuery.businessNo = this.$route.query.businessNo;
      }
      that.getList()
    },
    methods: {
      downloadContract(url){
        var a = document.createElement('a');
        var event = new MouseEvent('click');
        a.download = url;
        a.href = url;//路径前拼上前缀，完整路径
        a.dispatchEvent(event);
      },
      resetSearch() {
        this.listQuery = {
          current: 1,
          size: 10,
          descs: 'id',
          me: false
        }
        if (this.$router.currentRoute.path === '/item/list/me'){
          this.listQuery.me = true;
        }
        this.getList()
      },
      computedStageName(val){
        if (val===null || val===undefined || val === ''){
          return "无";
        }else {
          return val;
        }
      },
      removeHandle(row) {
        // console.log(data)
        const that = this
        that
          .$confirm('确认删除当前记录吗?', '警告', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          })
          .then(async () => {
            this.$api.term.delete(row.id).then((res) => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功',
                })
                this.getList()
              }
            })
          })
          .catch((err) => {
            console.error(err)
          })
      },
      searchList() {
        // 重置分页
        this.listQuery.page = 1
        this.listQuery.size = 20
        this.getList()
      },
      getList() {
        const that = this
        this.listLoading = true
        const key = {}
        if (this.$router.currentRoute.path === '/item/list/me'){
          this.$api.item.mine(Object.assign({}, that.listQuery, key)).then((res) => {
            that.pageData = res.data;
            setTimeout(() => {
              that.listLoading = false
            }, 200)
          })
            .catch(() => {
              that.listLoading = false
            })
        }else{
          this.$api.item.list(Object.assign({}, that.listQuery, key)).then((res) => {
            that.pageData = res.data
            setTimeout(() => {
              that.listLoading = false
            }, 200)
          })
            .catch(() => {
              that.listLoading = false
            })
        }
      },
      deleteInfo(id) {
        const that = this
        that.$confirm('请确认是否删除该数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          that.$api.item.delete(id).then(data => {
            that.loading = false
            if (data.code === 200) {
              that.getList()
            } else {
              this.$message({
                type: 'error',
                message: data.msg
              })
            }
          })
        }).catch(() => {
        })
      }
    },
  }
</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;
      }
    }
  }
</style>
