<!-- 委外列表 -->
<template>
  <div class="sale">
    <template v-if="!detailVisible && !addOrUpdateVisible && !changeFormVisible &&!changeAttachVisible && !attachVisible && !noticeChangeAttachVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="类别">
          <el-select
            v-model="dataForm.commissionType"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsType"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称或说明" prop="goodsName">
          <el-input v-model="dataForm.goodsName" placeholder="产品名称" clearable/>
        </el-form-item>
        <el-form-item label="申请日期">
          <el-date-picker
            v-model="dataForm.date"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="dataForm.purchaseState"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsOutsourceState"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        :row-class-name="tableRowClassName"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="purchaseCode"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="采购编码">
        </el-table-column>
        <el-table-column
          prop="goodsName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="产品名称">
        </el-table-column>
        <el-table-column
          prop="goodsId"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="产品编号">
        </el-table-column>
        <el-table-column
          prop="commissionType"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          :formatter="formatType"
          label="委外类别">
        </el-table-column>
        <el-table-column
          prop="specification"
          header-align="center"
          align="center"
          min-width="100"
          :show-tooltip-when-overflow="true"
          label="型号及规格">
        </el-table-column>
        <el-table-column
          prop="cnt"
          header-align="center"
          align="center"
          label="数量">
        </el-table-column>
        <el-table-column
          prop="qualifiedCnt"
          header-align="center"
          align="center"
          label="合格数量">
        </el-table-column>
        <el-table-column
          prop="unitName"
          header-align="center"
          align="center"
          min-width="100"
          :show-tooltip-when-overflow="true"
          label="单位">
        </el-table-column>
        <el-table-column
          prop="deadline"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="采购期限">
        </el-table-column>
        <el-table-column
          prop="arrivedTime"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="到料时间">
        </el-table-column>
        <el-table-column
          prop="batchNumber"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="批次号/用途">
        </el-table-column>
        <el-table-column
          prop="supplierName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="供应商">
        </el-table-column>
        <el-table-column
          prop="technologyFile"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="工艺文件">
<!--          <template slot-scope="scope">-->
<!--            <el-button :disabled="!scope.row.noticeAttachList || scope.row.noticeAttachList.length === 0" type="text" size="small" @click="changeDetails(scope.row)">查看</el-button>-->
<!--          </template>-->
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="附件">
          <template slot-scope="scope">
            <el-button :disabled="!scope.row.attachList || scope.row.attachList.length === 0" type="text" size="small" @click="attachDetails(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="applyTime"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="申请时间">
        </el-table-column>
        <el-table-column
          prop="purchaseCommission.applierId"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="申请人">
        </el-table-column>
        <el-table-column
          prop="purchaseCommission.orgId"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="申请部门">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          min-width="180"
          :show-overflow-tooltip="true"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="approvalState"
          header-align="center"
          align="center"
          :formatter="formatState"
          label="审批状态">
        </el-table-column>
        <el-table-column
          prop="purchaseState"
          header-align="center"
          align="center"
          :formatter="formatOutsourceState"
          label="委外状态">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('cus:contractBook:info')" type="text" size="small" @click="detailHandle(scope.row.purComDetailId)">查看</el-button>
            <el-button v-if="isAuth('cus:contractBook:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.purComDetailId, false)">编辑</el-button>
            <el-button v-if="isAuth('cus:contractBook:changeContract')" type="text" size="small" @click="changeHandle(scope.row.purComDetailId)">变更</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @onChose="onChose"></add-or-update>
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"/>
    <attach-detail v-if="changeAttachVisible" ref="changeDetail" @onChose="onChose"/>
    <attach-detail v-if="attachVisible" ref="attachDetail" @onChose="onChose"/>
    <notice-change-setting v-if="noticeChangeAttachVisible" ref="noticeChangeSetting" @onChose="onChose"/>
    <change-form v-if="changeFormVisible" ref="changeForm" @refreshDataList="getDataList" @onChose="onChose"/>
  </div>
</template>

<script>
import AddOrUpdate from '../cus/contract-record-add-or-update'
import Detail from './outsource-detail'
import { getOutsourceList } from '@/api/sale'
import AttachDetail from '../common/attach-detail'
import NoticeChangeSetting from '../cus/contract-record-notice-change-setting'
import ChangeForm from '../cus/contract-record-change'
import { getDictList } from '@/api/dict'
export default {
  name: 'outsource',
  components: {
    AttachDetail,
    AddOrUpdate,
    Detail,
    NoticeChangeSetting,
    ChangeForm
  },
  data () {
    return {
      addOrUpdateVisible: false,
      detailVisible: false,
      attachVisible: false,
      changeAttachVisible: false,
      noticeChangeAttachVisible: false,
      changeFormVisible: false,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      optionsType: [], // 类别
      optionsOutsourceState: [
        {
          code: null,
          value: '全部'
        },
        {
          code: '0',
          value: '待委外'
        },
        {
          code: '1',
          value: '委外中'
        },
        {
          code: '2',
          value: '已入库'
        },
        {
          code: '3',
          value: '入库中'
        },
        {
          code: '4',
          value: '入库异常'
        }
      ], // 委外状态
      optionsState: [] // 状态
    }
  },
  created () {
    this.optionsState = this.$store.state.common.approveStates
    this.getType()
    this.getDataList()
  },
  methods: {
    onChose () {
      this.addOrUpdateVisible = false
      this.attachVisible = false
      this.detailVisible = false
      this.noticeChangeAttachVisible = false
      this.changeFormVisible = false
      this.changeAttachVisible = false
    },
    // 查询
    search () {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取类别
    getType () {
      getDictList({type: 'commission_type'}).then(({data}) => {
        if (data) {
          this.optionsType = data
        }
      })
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      let params = {
        'current': this.pageIndex,
        'size': this.pageSize,
        'goodsName': this.dataForm.goodsName ? this.dataForm.goodsName : null,
        'applyTimeBegin': this.dataForm.date ? this.dataForm.date[0] : null,
        'applyTimeEnd': this.dataForm.date ? this.dataForm.date[1] : null,
        'purchaseState': this.dataForm.purchaseState ? this.dataForm.purchaseState : null,
        'commissionType': this.dataForm.commissionType ? this.dataForm.commissionType : null
      }
      getOutsourceList(params).then(({data}) => {
        if (data && data.code === '200' && data.data) {
          this.dataList = data.data.records
          this.totalPage = Number(data.data.total)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    deleteHandle (id) {
      if (!id) return
      let ids = []
      ids.push(id)
      this.$confirm(`确定删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/biz-service/cusContractBook/delete`),
          method: 'DELETE',
          data: ids
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 变更
    changeHandle (id) {
      this.changeFormVisible = true
      this.$nextTick(() => {
        this.$refs.changeForm.init(id)
      })
    },
    // 详情
    detailHandle (id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(id)
      })
    },
    // 变更通知人设置
    setNoticeChangeHandle () {
      this.noticeChangeAttachVisible = true
      this.$nextTick(() => {
        this.$refs.noticeChangeSetting.init()
      })
    },
    // 产品更改通知单
    changeDetails (row) {
      this.changeAttachVisible = true
      this.$nextTick(() => {
        this.$refs.changeDetail.init(row.noticeAttachList)
      })
    },
    // 转换属性“委外状态”
    formatOutsourceState (row) {
      if (!row.purchaseState) return ''
      const item1 = this.optionsOutsourceState.find((item) => item.code === row.purchaseState.toString())
      return item1 ? item1.value : ''
    },
    // 转换属性“类别”
    formatType (row) {
      if (!row.commissionType) return ''
      const item1 = this.optionsType.find((item) => item.code === row.commissionType.toString())
      return item1 ? item1.value : ''
    },
    // 转换属性“审批状态”
    formatState (row) {
      if (!row.approvalState) return ''
      const item1 = this.optionsState.find((item) => item.code === row.approvalState.toString())
      return item1 ? item1.value : ''
    },
    // 附件
    attachDetails (row) {
      this.attachVisible = true
      this.$nextTick(() => {
        this.$refs.attachDetail.init(row.attachList)
      })
    },
    // 高亮表格
    tableRowClassName ({row, rowIndex}) {
      if (!row.purchaseState) return ''
      if (Number(row.purchaseState) === 0) {
        return 'warning-row'
      }
      return ''
    }
  }
}
</script>

<style scoped>

</style>

<style>
  .el-table .warning-row {
    background: #fbc4c4;
  }
</style>
