<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建设备类型</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="state_value">
          <el-select
            class="mr10"
            v-model="state_value"
            placeholder="全部"
            @change="getListData"
          >
            <el-option
              v-for="item in state_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="keyword"
            clearable
            @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索
        </el-button>
      </el-form>
    </div>
    <el-table
      :data="applyTable"
      tooltip-effect="dark"
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="60">
      </el-table-column>
      <el-table-column
        label="设备类型名称"
        show-overflow-tooltip
        prop="lb_name"
      >
      </el-table-column>
      <el-table-column label="创建人" prop="create_name"></el-table-column>
      <el-table-column
        label="创建时间"
        width="220"
        prop="create_time"
      ></el-table-column>
      <el-table-column label="是否启用" >
        <template slot-scope="scope">
          <el-switch
            @change="isOpen(scope.row)"
            v-model="scope.row.lb_status"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="right">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="deleteData(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="applyTable.length"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="580px">
      <el-form
        :model="dialogForm"
        label-width="100px"
        style="min-height: 280px"
      >
        <el-form-item label="设备类型名称" prop="lb_name">
          <el-input
            v-model="dialogForm.lb_name"
            placeholder="请输入设备类型名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否启用" v-model="dialogForm.lb_status">
          <el-switch v-model="dialogForm.lb_status"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { useUserStore } from "@/stores/user";

const { real_name, dept_ids } = useUserStore();
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import { org_users, sblx_list, sblx_add, sblx_edit, sblx_del } from "./api";

export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      state_value: "",
      loading: false,
      state_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "启用",
          value: "1",
        },
        {
          label: "未启用",
          value: "2",
        },
      ],
      keyword: "",
      title: "",
      activeName: "first",
      shjd: [],
      applyTable: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      auditVisible: false,
      lbList: [
        {
          name: "测试1",
          id: 0,
        },
        {
          name: "测试2",
          id: 1,
        },
        {
          name: "测试3",
          id: 2,
        },
      ],

      send: true,
      code: false,
      dialogForm: {
        lb_name: "",
        lb_id: "",
        cjr: "",
        cjsj: "",
        type: "",
        cgje: "",
        xmqd: "",
        lb_status: 2,
      },
      isEdit: false,
      timestamp: Date.now(),
      userList: [],
    };
  },
  methods: {
    getUserList() {
      let data = {
        dept_id: dept_ids[0],
        page: 1,
        limit: 9999,
      };
      org_users(data).then((res) => {
        if (res.code == 1) {
          this.userList = res.data.page_data;
        }
      });
    },
    // getsblxList() {
    //   this.loading = true;
    //   let data = {
    //     page: 1,
    //     limit: 999,
    //   };
    //   sblx_list(data).then((res) => {
    //     console.log(res);
    //     this.loading = false;
    //     this.applyTable = res.data.page_data;
    //   });
    // },
    getListData() {
      this.loading = true;
      let data = {
        site: "jnmf",
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        lb_status: this.state_value,
      };
      sblx_list(data).then((res) => {
        this.loading = false;
        this.applyTable = res.data.page_data;
        this.getUserList();
        this.applyTable.forEach((item) => {
          item.lb_status = item.lb_status == 1 ? true : false;
          //   this.userList.forEach((user) => {
          //     console.log(user);
          //   });
        });
        //   this.auditTable = res.data.page_data;
        //   this.cur_page = Number(res.data.page_now);
        //   this.total = Number(res.data.total_rows);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      //   this.getsblxList();
    },
    createNew() {
      this.isEdit = false;
      this.title = "新建设备类型";
      this.dialogForm = {
        lb_name: "",
        cjr: "",
        cjsj: "",
        type: "",
        cgje: "",
        xmqd: "",
        lb_status: false,
      };
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.isEdit = true;
      this.title = "编辑设备类型";
      this.dialogForm = {
        lb_name: item.lb_name,
        lb_status: item.lb_status,
        lb_id: item.lb_id,
      };
      this.timestamp = Date.now();
      this.dialogFormVisible = true;
    },
    // 是否启用
    isOpen(item) {
      let status;
      if (item.lb_status == false) {
        status = 2;
      } else {
        status = 1;
      }
      let data = {
        jdbg_sbcg_sblx: {
          lb_name: item.lb_name,
          lb_status: status,
        },
      };
      Object.assign(data, {
        lb_id: item.lb_id,
        site: "jnmf",
        issubmit: 1,
      });
      console.log(data, 1111111);
      sblx_edit(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "操作成功",
            type: "success",
          });
        }
      });
    },
    deleteData(item) {
      let data = {
        lb_id: item.lb_id,
      };
      sblx_del(data).then((res) => {
        console.log(res);
        this.getListData();
      });
    },
    formSubmit() {
      let flag = false;
      if (!this.dialogForm.lb_name) {
        flag = true;
      }
      if (flag) {
        return this.$message.error("设备类型名称必填");
      }
      let data = {
        site:'jnmf',
        jdbg_sbcg_sblx: {
          lb_name: this.dialogForm.lb_name,
          lb_status: this.dialogForm.lb_status === true ? "1" : "2",
          create_name: real_name,
        },
      };
      if (!this.isEdit) {
        sblx_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "设备类型新建成功！",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        Object.assign(data, {
          lb_id: this.dialogForm.lb_id,
        });
        sblx_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "设备类型编辑成功！",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    // detailData(row) {
    //   this.$router.push({
    //     name: "hysyd_detail",
    //     params: {
    //       yd_id: row.lb_id,
    //     },
    //   });
    // },
    verify(type, item) {
      let data = {
        kylx_zt: type,
      };
      //   kylx_verify(data).then((res) => {
      //     if (res.code == 1) {
      //       this.$message({
      //         message: "审核成功！",
      //         type: "success",
      //       });
      //       this.getListData();
      //     }
      //   });
    },
  },
  mounted() {
    this.getUserList();
    // this.getsblxList();
    this.getListData();
  },
};
</script>
