<template>
  <!-- 待审核订单 -->
  <div>
    <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>
    <!-- 处方 -->
    <prescriptionPicture ref="picture" :srcList="srcList" :fileList="fileList" :orderID="orderID"
      @prescription="prescription" @closeDialog="closeDialog"></prescriptionPicture>
    <el-dialog top="5vh" title="订单审核" :visible.sync="auditDialogVisible" width="800px" :close-on-click-modal="false"
      @close="closeDialog1">
      <forms ref="auditRules" beyondHeight="1000px" :formNewList="auditRuleList" :ruleForm="auditRuleForm"
        labelWidth="130px">
      </forms>
      <el-divider>复核内容审批</el-divider>
      <forms ref="auditRules1" :formNewList="reviewContent" :ruleForm="reviewRuleForm" labelWidth="180px">
      </forms>
      <el-dialog width="500px" title="驳回" :visible.sync="causeVisible" append-to-body>
        <div style="display: flex;align-items: center;">
          <span style="flex: none;margin-right: 10px;">
            <span style="color: red;">*</span>
            驳回原因
          </span>
          <el-input v-model="auditRemark" placeholder="请输入驳回原因"></el-input>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button plain @click="causeVisible = false">取 消</el-button>
          <el-button type="primary" :loading="causeLoading" @click="auditHandleAdd(7)">确 定</el-button>
        </span>
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button plain @click="auditDialogVisible = false">取 消</el-button>
        <el-button type="danger" plain @click="turnDown">驳 回</el-button>
        <el-button type="primary" :loading="auditConfirmLoading" @click="auditHandleAdd(3)">确 定</el-button>
      </span>
    </el-dialog>
    <addEditOrder ref="order" :orderId="orderID" :staffTitle="staffTitle" :orderInformation="orderInformation"
      :operationType="operationType" @refreshList="refreshList">
    </addEditOrder>
  </div>
</template>

<script>
  import {
    getWaybill,
    auditWaybill,
    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 forms from '@/components/forms'
  import addEditOrder from '@/components/addEditOrder'
  import {
    employee,
    auditList,
    examineList
  } from "./uncertain.js";
  import {
    WaybillStatus
  } from '@/assets/js/blockSort'
  export default {
    name: 'OrderManagement',
    components: {
      actionBar,
      tables,
      pagination,
      orderDetails,
      prescriptionPicture,
      forms,
      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: 'send',
          label: '处方',
          icon: 'el-icon-tickets',
          style: 'primary',
        }, {
          type: 'edit',
          label: '修改',
          icon: 'el-icon-edit',
          style: 'success',
        }, {
          type: 'audit',
          label: '审核',
          icon: 'el-icon-share',
          style: 'warning',
        }, {
          type: 'delOrder',
          label: '取消订单',
          icon: 'el-icon-delete',
          style: 'danger',
        }],
        srcList: [],
        fileList: [],

        auditDialogVisible: false,
        auditRuleList: auditList(),
        auditRuleForm: {
          confirmName: '',
          confirmPhoneNumber: '',
          orderNo: '',
          type: '',
          deliveryTime: '',
          status: '',
          remark: '',
          consigneeAddressName: '',
          consigneeAddressPhone: '',
          consigneeAddressDetails: '',
          domicile: '',
          tamperProofLabelImg: '',
        },
        auditConfirmLoading: false,
        reviewContent: examineList(),
        reviewRuleForm: {
          prescriptionIssue: 1,
          invoiceIssue: 1,
          inspectionReportIssue: 1,
          dispensingMedicinesIssue: 1,
          isCold: 1,
        },
        orderID: null,
        causeVisible: false,
        causeLoading: false,
        auditRemark: '',
        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: 2,
          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
      },
      // 驳回原因
      turnDown() {
        this.causeVisible = true
      },
      // 订单审核
      auditHandleAdd(event) {
        let flag = this.$refs['auditRules'].validateForm();
        let flag1 = this.$refs['auditRules1'].validateForm();
        if (flag && flag1) {
          let params = {
            id: this.orderID,
            status: null,
            ...this.reviewRuleForm,
          }
          if (event == 7) {
            this.causeLoading = true
            params.status = event
            params.audit_remark = this.auditRemark
          } else {
            this.auditConfirmLoading = true
            params.status = event
          }
          if (params.status == 7 && !this.auditRemark) {
            this.$message.error('请填写驳回原因');
          } else {
            auditWaybill(params).then(res => {
              if (res.code == 200) {
                this.auditConfirmLoading = false
                this.causeLoading = false
                this.causeVisible = false
                this.auditDialogVisible = false
                this.getList()
              }
            })
          }
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      buttonData(row, type) {
        this.orderID = row.id
        this.operationType = type
        if (type == 'send') {
          if (row.prescription) {
            this.srcList = row.prescription
          }
          this.$refs.picture.prescriptionVisible = true
        } else if (type == 'audit') {
          this.auditRuleForm = JSON.parse(JSON.stringify(row))
          this.$nextTick(() => {
            if (row.prescription) {
              this.auditRuleList.forEach(item => {
                if (item.field == 'tamperProofLabelImg') {
                  item.urlList = row.prescription
                }
              })
            }
            this.$refs.auditRules.commodityList = row.drugs
          })
          this.auditDialogVisible = 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.order.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()
      },
      // 清空表单
      closeDialog1() {
        this.$refs.auditRules.resetCheck();
      },
      closeDialog() {
        this.srcList = []
        this.fileList = []
      }
    },
  }
</script>

<style>
</style>
