<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="生产令号" prop="productionCode">
        <el-input
          v-model="queryParams.productionCode"
          placeholder="请输入生产令号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图号" prop="figureNumber">
        <el-input
          v-model="queryParams.figureNumber"
          placeholder="请输入图号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="figureName">
        <el-input
          v-model="queryParams.figureName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="oneNumber">
        <el-input
          v-model="queryParams.oneNumber"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="交货时间" prop="deliveryTime">
        <el-date-picker clearable
                        v-model="queryParams.deliveryTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择交货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库状态" prop="imVendor">
        <el-select v-model="queryParams.imVendor" placeholder="入库状态" clearable @keyup.enter.native="handleQuery">
          <el-option label="已入库" value="已入库"></el-option>
          <el-option label="未入库" value="未入库"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="单据状态" prop="documentStatus">
        <el-select v-model="queryParams.documentStatus" placeholder="单据状态" clearable
                   @keyup.enter.native="handleQuery">
          <el-option label="计划" value="计划"></el-option>
          <el-option label="计划确认" value="计划确认"></el-option>
          <el-option label="下达" value="下达"></el-option>
          <el-option label="开工" value="开工"></el-option>
          <el-option label="完工" value="完工"></el-option>
          <el-option label="结案" value="结案"></el-option>
          <el-option label="结算" value="结算"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单标记" prop="orderFlag">
        <el-select v-model="queryParams.orderFlag" placeholder="订单标记" clearable @keyup.enter.native="handleQuery">
          <el-option label="生产订单" value="生产订单"></el-option>
          <el-option label="采购订单" value="采购订单"></el-option>
          <el-option label="委外订单" value="委外订单"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="领料状态" prop="pickmtrlStatus">
        <el-select v-model="queryParams.pickmtrlStatus" placeholder="领料状态" clearable
                   @keyup.enter.native="handleQuery">
          <el-option label="未领料" value="已审核"></el-option>
          <el-option label="部分领料" value="部分领料"></el-option>
          <el-option label="全部领料" value="全部领料"></el-option>
          <el-option label="超额领料" value="超额领料"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:productionPlanPro:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:productionPlanPro:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:productionPlanPro:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:productionPlanPro:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
              :data="productionPlanProList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="序号" align="center" prop="id" width="65" v-if="true"/>-->
      <el-table-column label="单据编码" align="center" prop="fidNumber"/>
      <el-table-column label="生产令号" align="center" prop="productionCode"/>
      <el-table-column label="图号" align="center" prop="figureNumber"/>
      <el-table-column label="名称" align="center" prop="figureName"/>
      <el-table-column label="数量" align="center" prop="oneNumber"/>

      <el-table-column label="订单类型" align="center" prop="orderFlag">
        <template slot-scope="scope">
          <div :style="{
      color: scope.row.orderFlag === '采购订单' ? '#DDAA00' : (
      scope.row.orderFlag === '委外订单' ? 'green' : '#FF3EFF'),
      fontWeight: scope.row.orderFlag === '生产订单' ? '#00FFFF' : 'normal',
      fontFamily: '等线, sans-serif'}">{{ scope.row.orderFlag }}</div>
        </template>
      </el-table-column>
      <!--      <el-table-column label="单价" align="center" prop="itemPrice" />-->
      <el-table-column label="入库状态" align="center" prop="imVendor">
        <template slot-scope="scope">
          <div :class="{'row-green': scope.row.imVendor === '已入库', 'row-red': scope.row.imVendor === '未入库'}">
            {{ scope.row.imVendor }}
          </div>
        </template>
      </el-table-column>

      <!--      <el-table-column label="采购时间" align="center" prop="procureTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.procureTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>-->
      <el-table-column label="交货时间" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="金额" align="center" prop="taxPrice" />-->
      <el-table-column label="单据状态" align="center" prop="documentStatus"/>
      <el-table-column label="领料状态" align="center" prop="pickmtrlStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <!--        <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['system:productionPlanPro:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['system:productionPlanPro:remove']"
                  >删除</el-button>
                </template>-->
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改金蝶生产计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="金蝶单据编码" prop="fidNumber">
          <el-input v-model="form.fidNumber" placeholder="请输入金蝶单据编码"/>
        </el-form-item>
        <el-form-item label="生产令号" prop="productionCode">
          <el-input v-model="form.productionCode" placeholder="请输入生产令号"/>
        </el-form-item>
        <el-form-item label="图号" prop="figureNumber">
          <el-input v-model="form.figureNumber" placeholder="请输入图号"/>
        </el-form-item>
        <el-form-item label="名称" prop="figureName">
          <el-input v-model="form.figureName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="数量" prop="oneNumber">
          <el-input v-model="form.oneNumber" placeholder="请输入数量"/>
        </el-form-item>
        <el-form-item label="物料单价" prop="itemPrice">
          <el-input v-model="form.itemPrice" placeholder="请输入物料单价"/>
        </el-form-item>
        <el-form-item label="供应商" prop="imVendor">
          <el-input v-model="form.imVendor" placeholder="请输入供应商"/>
        </el-form-item>
        <el-form-item label="采购时间" prop="procureTime">
          <el-date-picker clearable
                          v-model="form.procureTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择采购时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交货时间" prop="deliveryTime">
          <el-date-picker clearable
                          v-model="form.deliveryTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择交货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="金额" prop="taxPrice">
          <el-input v-model="form.taxPrice" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item label="订单标记" prop="orderFlag">
          <el-input v-model="form.orderFlag" placeholder="请输入订单标记"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProductionPlanPro,
  getProductionPlanPro,
  delProductionPlanPro,
  addProductionPlanPro,
  updateProductionPlanPro
} from "@/api/system/productionPlanPro";

export default {
  name: "ProductionPlanPro",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 金蝶生产计划表格数据
      productionPlanProList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fidNumber: undefined,
        productionCode: undefined,
        figureNumber: undefined,
        figureName: undefined,
        oneNumber: undefined,
        itemPrice: undefined,
        imVendor: undefined,
        procureTime: undefined,
        deliveryTime: undefined,
        taxPrice: undefined,
        documentStatus: undefined,
        pickmtrlStatus: undefined,
        orderFlag: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "序号不能为空", trigger: "blur"}
        ],
        fidNumber: [
          {required: true, message: "金蝶单据编码不能为空", trigger: "blur"}
        ],
        productionCode: [
          {required: true, message: "生产令号不能为空", trigger: "blur"}
        ],
        figureNumber: [
          {required: true, message: "图号不能为空", trigger: "blur"}
        ],
        figureName: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        oneNumber: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        itemPrice: [
          {required: true, message: "物料单价不能为空", trigger: "blur"}
        ],
        imVendor: [
          {required: true, message: "供应商不能为空", trigger: "blur"}
        ],
        procureTime: [
          {required: true, message: "采购时间不能为空", trigger: "blur"}
        ],
        deliveryTime: [
          {required: true, message: "交货时间不能为空", trigger: "blur"}
        ],
        taxPrice: [
          {required: true, message: "金额;未税单价和数量的总金额不能为空", trigger: "blur"}
        ],
        documentStatus: [
          {required: true, message: "单据状态不能为空", trigger: "change"}
        ],
        pickmtrlStatus: [
          {required: true, message: "领料状态不能为空", trigger: "change"}
        ],
        orderFlag: [
          {required: true, message: "订单标记;生产订单1，采购订单2不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询金蝶生产计划列表 */
    getList() {
      this.loading = true;
      listProductionPlanPro(this.queryParams).then(response => {
        this.productionPlanProList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fidNumber: undefined,
        productionCode: undefined,
        figureNumber: undefined,
        figureName: undefined,
        oneNumber: undefined,
        itemPrice: undefined,
        imVendor: undefined,
        procureTime: undefined,
        deliveryTime: undefined,
        taxPrice: undefined,
        documentStatus: undefined,
        updateTime: undefined,
        createTime: undefined,
        createBy: undefined,
        updateBy: undefined,
        pickmtrlStatus: undefined,
        orderFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加金蝶生产计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getProductionPlanPro(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改金蝶生产计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateProductionPlanPro(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addProductionPlanPro(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      //这没拿到id
      this.$modal.confirm('是否确认删除金蝶生产计划编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delProductionPlanPro(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/productionPlanPro/export', {
        ...this.queryParams
      }, `productionPlanPro_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.row-green {
  color: #00DD00;
}

.row-red {
  color: #FF3333;
}
</style>
