<template>
  <!-- 配送中订单 -->
  <div class="home">
    <div ref="grabble">
      <actionBar :formList="formList" :ruleForm="searchRuleForm" @openModel="openModel"
        @searchProtocol="searchProtocol"></actionBar>
    </div>
    <div class="card_content">
      <!-- 表单 -->
      <tables ref="refWaybill" controlswidth="300px" tableHeight="68vh" :suspension="true" :tableList="tableList"
        :tableData="tableData" @buttonData="buttonData">
      </tables>
      <!-- 分页 -->
      <div v-if="Total">
        <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
          @changeCurrent="changeCurrent">
        </pagination>
      </div>
    </div>
    <orderDetails ref="order" :waybillData="waybillData"></orderDetails>
    <prescriptionPicture ref="picture" :srcList="srcList" :fileList="fileList" :orderID="orderId"
      @prescription="prescription" @closeDialog="closeDialog"></prescriptionPicture>
    <!-- 修改订单 -->
    <addEditOrder ref="orderEdit" :orderId="orderId" :staffTitle="staffTitle" :orderInformation="orderInformation"
      :operationType="operationType" @refreshList="refreshList">
    </addEditOrder>
  </div>
</template>

<script>
  import {
    getWaybill,
    putUpdateStatus
  } from '@/api/waybill'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import orderDetails from '@/components/orderDetails'
  import prescriptionPicture from '@/components/prescriptionPicture'
  import addEditOrder from '@/components/addEditOrder'
  import {
    employee,
  } from "./ordered.js";
  import {
    WaybillStatus
  } from '@/assets/js/blockSort'
  export default {
    name: 'OrderManagement',
    components: {
      actionBar,
      tables,
      pagination,
      orderDetails,
      prescriptionPicture,
      addEditOrder
    },
    data() {
      return {
        formList: [{
          type: 'input',
          label: '订单编号',
          field: 'waybillNo',
          placeholder: '请输入订单编号',
        }, {
          type: 'input',
          label: '配送员姓名',
          field: 'delivery_name',
          placeholder: '请输入配送员姓名',
        }, {
          type: 'input',
          label: '患者名称',
          field: 'Consignee_address_name',
          placeholder: '请输入患者名称',
        }, {
          type: 'input',
          label: '患者手机号',
          field: 'Consignee_address_phone',
          placeholder: '请输入患者手机号',
        }],
        searchRuleForm: {
          waybillNo: '',
          delivery_name: '',
          Consignee_address_name: '',
          Consignee_address_phone: '',
        },
        searchValue: {},
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        Total: 0,
        operationType: '',
        tableData: [],
        tableList: [],
        btnData: [{
          type: 'logs',
          label: '详情',
          icon: 'el-icon-tickets',
          style: 'success',
        }, {
          type: 'edit',
          label: '修改',
          icon: 'el-icon-edit',
          style: 'success',
        }, {
          type: 'send',
          label: '处方',
          icon: 'el-icon-tickets',
          style: 'primary',
        }, {
          type: 'delOrder',
          label: '取消订单',
          icon: 'el-icon-delete',
          style: 'danger',
        }],
        waybillData: {},
        srcList: [],
        fileList: [],
        orderId: null,
        staffTitle: '',
        orderInformation: {},
      }
    },
    mounted() {
      let arr = employee()
      arr.forEach(item => {
        if (item.field == 'unpackBtn') {
          item.labelButton = this.btnData
        }
      })
      this.tableList = arr
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.Pagination.PageIndex = 1
        this.searchValue = value
        if (value.time) {
          this.searchValue.orderStartTime = value.time[0]
          this.searchValue.orderEndTime = value.time[1]
        }
        this.getList()
      },
      // 获取运单列表
      getList() {
        var params = {
          status: 4,
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        delete params.time
        getWaybill(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.tableData.forEach(item => {
              if (item.iceRaftCode) {
                item.iceRaftCode = item.iceRaftCode.toString()
              }
              item.visible = false
            })
            this.Total = res.data.count
          }
        })
      },
      openModel(type) {
        this.operationType = type
      },
      buttonData(row, type) {
        this.orderId = row.id
        this.operationType = type
        if (type == 'logs') {
          this.$refs.order.staffDialogVisible = true
          this.$refs.order.tableData = row.drugs
          this.waybillData = row
          this.waybillData.coolerBoxName = row.coolerBox.name
          this.waybillData.courierName = row.delivery.nickName
          this.waybillData.courierPhone = row.delivery.phone
          this.waybillData.reCheckName = row.reCheck.nickName
          this.waybillData.reCheckPhone = row.reCheck.phone
        } else if (type == 'send') {
          if (row.prescription) {
            this.srcList = row.prescription
          }
          this.$refs.picture.prescriptionVisible = true
        } else if (type == 'delOrder') {
          this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let arrOrderId = []
            arrOrderId.push(row.id)
            putUpdateStatus({
              id: arrOrderId,
              status: 8,
            }).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
            })
          }).catch(() => {})
        } else if (type == "edit") {
          this.staffTitle = '修改'
          this.orderInformation = {}
          this.$refs.orderEdit.staffDialogVisible = true
          this.$nextTick(() => {
            this.orderInformation = row
          })
        }
      },
      // 添加修改刷新列表
      refreshList() {
        this.getList()
      },
      // 处方更新
      prescription() {
        this.Pagination.PageIndex = 1
        this.getList()
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      closeDialog() {
        this.srcList = []
        this.fileList = []
      }
    },
  }
</script>

<style>
</style>
