<template>
  <div class="detail-container">
    <el-page-header content="详情" @back="goBack" />

    <div class="main-title">
      <div class="title">工程信息单</div>
    </div>

    <div class="diy-table-1">
      <el-row>
        <el-col :span="8" class="item">
          <div class="label">工程登录编号</div>
          <div class="value">{{ detailData.enginInfoNo }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">订单日期</div>
          <div class="value">{{ detailData.orderDate }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">业务员</div>
          <div class="value">{{ detailData.serviceName }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">经销商编码</div>
          <div class="value">{{ detailData.customerNumber }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">项目名称</div>
          <div class="value">{{ detailData.projectName }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">机型类别</div>
          <div class="value">{{ detailData.machineType }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">经销商名称</div>
          <div class="value">{{ detailData.customerName }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">使用单位</div>
          <div class="value">{{ detailData.useUnit }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">行业类别</div>
          <div class="value">{{ detailData.tradeCategory }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">工程编号</div>
          <div class="value">{{ detailData.projectNo }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">厂工程编码</div>
          <div class="value">{{ detailData.enginFactoryNo }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">工程登录类型</div>
          <div class="value">{{ detailData.enginSignType }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">联系人</div>
          <div class="value">{{ detailData.linkman }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">联系电话</div>
          <div class="value">{{ detailData.phone }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">固定电话</div>
          <div class="value">{{ detailData.tel }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">文件编号</div>
          <div class="value">{{ detailData.fileNo }}</div>
        </el-col>
        <el-col :span="24" class="item">
          <div class="label">安装地址</div>
          <div class="value">{{ detailData.installAddress }}</div>
        </el-col>

        <template v-if="!isCustomer">
          <el-col :span="16" class="item">
            <div class="label">格力内部备注</div>
            <div class="value">{{ detailData.geLiInerNote }}</div>
          </el-col>
          <el-col v-if="!isDealer" :span="8" class="item">
            <div class="label">权限分类</div>
            <div class="value">{{ detailData.powerCategory }}</div>
          </el-col>
        </template>
        <el-col :span="24" class="item">
          <div class="label">格力回复</div>
          <div class="value">{{ detailData.geLiNote }}</div>
        </el-col>
        <el-col :span="24" class="item">
          <div class="label">备注</div>
          <div class="value">{{ detailData.remark }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">制单人</div>
          <div class="value">{{ detailData.createName }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">制单日期</div>
          <div class="value">{{ detailData.createTime }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">合同有效期</div>
          <div class="value">{{ detailData.contractExpireDate }}</div>
        </el-col>
      </el-row>
    </div>

    <div class="main-title">
      <div class="title">货品信息</div>
    </div>

    <div class="table" style="margin-top: 20px">
      <el-table
        :data="detailData.items"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        stripe
        max-height="400"
        show-summary
        :summary-method="$getSummaries"
      >
        <el-table-column align="center" label="序号" type="index" width="50" />
        <el-table-column
          align="center"
          label="销售类型"
          prop="saleTypeName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          label="物料编码"
          prop="materialNumber"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          label="产品编码"
          prop="materialOldNumber"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          label="产品名称"
          prop="materialName"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          label="规格型号"
          prop="specification"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column align="center" label="单位" prop="unit" min-width="100" show-overflow-tooltip />
        <el-table-column align="right" label="单价" prop="price" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.price | numToFixed }}
          </template>
        </el-table-column>
        <el-table-column align="right" label="数量" prop="qty" min-width="100" show-overflow-tooltip />
        <el-table-column align="right" label="订单金额" prop="totalAmount" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.totalAmount | numToFixed }}
          </template>
        </el-table-column>
        <el-table-column align="right" label="已订数量" prop="hasOrderQty" min-width="100" show-overflow-tooltip />
        <el-table-column align="right" label="已发货数" prop="hasDeliverQty" min-width="100" show-overflow-tooltip />
        <el-table-column align="center" label="备注" prop="remark" min-width="200" show-overflow-tooltip />
        <el-table-column align="center" label="税率" prop="taxRate" min-width="100" show-overflow-tooltip />
      </el-table>
    </div>

    <div>
      <div class="main-title">
        <div class="title">审批记录</div>
      </div>
      <div class="diy-table-1">
        <el-row :gutter="0">
          <el-col :span="12" class="item">
            <div class="label">审批人</div>
            <div class="value">{{ detailData.confirmName }}</div>
          </el-col>
          <!--          <el-col :span="8" class="item">-->
          <!--            <div class="label">是否计划单</div>-->
          <!--            <div class="value">-->
          <!--              <el-radio-group v-model="detailData.isPlanOrder" disabled>-->
          <!--                <el-radio :label="true">是</el-radio>-->
          <!--                <el-radio :label="false">否</el-radio>-->
          <!--              </el-radio-group>-->
          <!--            </div>-->
          <!--          </el-col>-->
          <el-col :span="12" class="item">
            <div class="label">审批结果</div>
            <div class="value">
              {{ detailData.examineStatus | statusFilter }}
            </div>
          </el-col>
          <el-col :span="24" class="item">
            <div class="label">审批说明</div>
            <div class="value">{{ detailData.examineNote }}</div>
          </el-col>
        </el-row>
      </div>
    </div>

    <div class="page-footer">
      <div class="footer">
        <el-button v-if="!isDealer" type="primary" :disabled="!listItem.isClose" @click="setOrderIsClose(false)"
          >启用</el-button
        >
        <el-button v-if="!isDealer" type="primary" :disabled="listItem.isClose" @click="setOrderIsClose(true)"
          >关单</el-button
        >
        <el-button
          v-if="!isDealer"
          type="primary"
          :disabled="detailData.examineStatus !== 'OK' || detailData.directTransferStatus"
          @click="overData"
          >直调完结
        </el-button>
        <el-button @click="goBack">返回列表</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getEnginDetail, overEngin, setOrderIsClose } from '@/api/supply/engin'
import { mapGetters } from 'vuex'

export default {
  name: 'EnginDetail',
  componentName: 'EnginDetail',
  filters: {
    statusFilter(val) {
      const statusList = [
        { label: '已保存', value: 'SAVE' },
        { label: '待审核', value: 'WAIT' },
        { label: '审核通过', value: 'OK' },
        // { label: '审核驳回', value: 'FAIL' },,
        { label: '已关闭', value: 'CLOSE' }
      ]
      const obj = statusList.find(o => o.value == val)
      return obj ? obj.label : ''
    }
  },
  props: ['listItem'],
  data() {
    return {
      detailData: {}
    }
  },

  computed: {
    ...mapGetters(['isCustomer']),
    isExamine() {
      return this.detailData.examineStatus === 'OK' || this.detailData.examineStatus === 'FAIL'
    },
    isDealer() {
      return JSON.parse(localStorage.getItem('supply_user')).isCustomer
    }
  },

  created() {
    this.getDetail()
  },

  methods: {
    // 返回列表
    goBack() {
      this.$emit('backListFormDetail')
    },

    // 获取详情
    getDetail() {
      getEnginDetail({ id: this.listItem.enginInfoId }).then(res => {
        if (res.data.items) {
          res.data.items.forEach(item => {
            item.sums1 = ['qty', 'hasOrderQty', 'hasDeliverQty']
            item.sums2 = ['totalAmount', 'price']
          })
        }
        this.detailData = res.data
      })
    },
    // 打开/关闭信息单
    setOrderIsClose(status) {
      setOrderIsClose({
        enginInfoId: this.detailData.enginInfoId,
        isClose: status
      }).then(res => {
        this.$successMsg(status ? '关闭' : '启用')
        this.goBack()
      })
    },
    // 直调完结
    overData() {
      this.$confirm('此操作将直调完结订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          overEngin({ id: this.listItem.enginInfoId }).then(res => {
            this.$successMsg()
            this.getDetail()
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  width: 100%;
  height: 100%;
}

.main-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  height: 60px;
  border-bottom: 1px solid #dcdfe6;
  margin-bottom: 20px;

  .title {
    font-size: 16px;
    font-weight: 600;
    padding-left: 10px;
  }
}
</style>
