<template>
  <div class="app-container">
    <el-card>
      <div>
         <el-button type="primary" icon="el-icon-plus" @click="handleCreate"
        >新增订单</el-button
      >
      <el-input
              v-model="listQuery.search"
              placeholder="订单名称"
              style="width: 300px"
              class="filter-item"
              @keyup.enter.native="handleFilter"
            />
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter"
              >搜索</el-button
            >
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-refresh-left"
              @click="resetFilter"
              >重置</el-button
            >
    </div>
   
    </el-card>
    <el-card style="margin-top: 2px">
      <el-table
        v-loading="listLoading"
        :data="orderList.results"
        border
        fit
        stripe
        highlight-current-row
       height="100"
        v-el-height-adaptive-table="{bottomOffset: 50}"
      >
        <el-table-column type="index" width="50" />
        
        <el-table-column label="订单编号"  width="160" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.number }}</template>
        </el-table-column>
         <el-table-column label="客户"  width="200" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.customer_.name }}</template>
        </el-table-column>
          <el-table-column label="所属合同"  width="200" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.contract_.name }}</template>
        </el-table-column>
         <el-table-column label="产品名称"  width="200" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.product_.name }}</template>
        </el-table-column>
          <el-table-column label="产品型号"  width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.product_.specification }}</template>
        </el-table-column>
       <el-table-column label="产品数量"  width="90" >
          <template slot-scope="scope">{{ scope.row.count }}</template>
        </el-table-column>
         <el-table-column label="交货日期"  width="150" >
          <template slot-scope="scope">{{ scope.row.delivery_date }}</template>
        </el-table-column>
        <el-table-column label="已交货数量"  width="150" >
          <template slot-scope="scope">{{ scope.row.delivered_count }}</template>
        </el-table-column>
        
  <el-table-column label="创建时间"  width="160" >
          <template slot-scope="scope">{{ scope.row.create_time }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
           fixed="right"
           width="130"
        >
          <template slot-scope="scope">
           
            <el-link
              v-if="scope.row.planed_count==0&&checkPermission(['warehouse_update'])"
              @click="handleEdit(scope)"
              >编辑</el-link
            >
            <el-link
              v-if="checkPermission(['warehouse_delete'])"
              type="danger"
              @click="handleDelete(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="orderList.count > 0"
        :total="orderList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :title="dialogType === 'edit' ? '编辑订单' : '新增订单'"
    >
      <el-form
        ref="Form"
        :model="order"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
       
        <el-form-item label="订单编号" prop="number">
          <el-input v-model="order.number" placeholder="订单编号" />
        </el-form-item>
          <el-form-item label="所需产品" prop="product">
         <el-select  style="width: 100%" v-model="order.product" placeholder="请选择">
             <el-option
                v-for="item in productoptions"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
          </el-form-item>
          
        <el-form-item label="所需数量" prop="count">
           <el-input type="number" v-model="order.count"></el-input>
        </el-form-item>
         <el-form-item label="交货日期" prop="delivery_date">
           <el-date-picker
            v-model="order.delivery_date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
         <el-form-item label="客户" prop="customer">
         <el-select  style="width: 100%" v-model="order.customer" placeholder="请选择">
             <el-option
                v-for="item in customeroptions"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
          </el-form-item>
         <el-form-item label="所属合同" prop="contract">
         <el-select  style="width: 100%" v-model="order.contract" placeholder="请选择">
             <el-option
                v-for="item in contractoptions"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
          </el-form-item>
         
        
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getOrderList, createOrder,updateOrder,deleteOrder,getContractList,getCustomerList } from "@/api/sam";
import { getMaterialList } from "@/api/mtm";
import checkPermission from "@/utils/permission";


import { genTree } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
const defaulteorder = {
};
export default {
  components: { Pagination },
  data() {
    return {
      order: defaulteorder,
      orderList: {
        count: 0,
      },
      listQuery: {
        page: 1,
        page_size: 20,
      },
    contractoptions:[],
    productoptions:[],
    customeroptions:[],
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        number: [{ required: true, message: "请输入", trigger: "blur" }],
        customer: [{ required: true, message: "请输入", trigger: "blur" }],
        product: [{ required: true, message: "请输入", trigger: "blur" }],
        delivery_date: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
     this.getListm();
     this.getListc();
    this.getListcm();
    this.getList();
  },
  methods: {
    checkPermission,
  
    getList() {
      this.listLoading = true;
      getOrderList(this.listQuery).then((response) => {
        if (response.data) {
          this.orderList = response.data;
        }
        this.listLoading = false;
      });
    },
   
     getListm() {
      
      getMaterialList({pageoff:true,type:1}).then((response) => {
        
          this.productoptions = genTree(response.data);
       
      });
    },
     getListc() {
      
      getContractList({pageoff:true}).then((response) => {
        
          this.contractoptions = genTree(response.data);
       
      });
    },
     getListcm() {
      
      getCustomerList({pageoff:true}).then((response) => {
        
          this.customeroptions = genTree(response.data);
       
      });
    },
   
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
      }
      this.getList();
    },
    handleCreate() {
      this.order = Object.assign({}, defaulteorder);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
   
    handleEdit(scope) {
      this.order = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteOrder(scope.row.id);
          this.getList();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
   
    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updateOrder(this.order.id, this.order).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          } else {
            createOrder(this.order).then((res) => {
              if (res.code >= 200) {
                this.getList();
                 this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
