<template>
  <div class="app-container">
    <el-card>
       <el-row :gutter="6">
      <el-col :xs="12" :md="2">
      <el-button type="primary" icon="el-icon-plus" @click="handleAddContent" v-if="checkPermission(['validation_create'])"
        >新增</el-button
      >
      </el-col>
       <el-col :xs="12" :md="4">
      <el-select
          v-model="listQuery.provinces"
          placeholder="省份"
          @change="handleFilter2"
          clearable
          style="width: 100%"
        >
           <el-option
      v-for="item in options"
      :key="item.label"
      :label="item.label"
      :value="item.label">
    </el-option>
        </el-select>
          </el-col>
       </el-row>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table
        v-loading="listLoading"
        :data="contentList.results"
        border
        fit
        stripe
        highlight-current-row
        max-height="600"
      >
        <el-table-column type="index" width="50" />
        <el-table-column label="名称">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
      <el-table-column label="文件类型" :filters="[{ text: '征集通知', value: 1 }, { text: '结果通报', value: 2 }]"
      :filter-method="filterTag1">
          <template slot-scope="scope">
              {{filetype_[scope.row.filetype] }}</template>
        </el-table-column>



          <el-table-column label="省份">
          <template slot-scope="scope">{{ scope.row.provinces }}</template>
        </el-table-column>
      <el-table-column align="center" label="文件下载">
          <template slot-scope="scope" v-if="scope.row.file_.file">
            <el-link :href="scope.row.file_.file" type="primary">{{ scope.row.file_.name }}</el-link>
          </template>
        </el-table-column>
         <el-table-column label="文件描述"  >
          <template slot-scope="scope">{{ scope.row.description }}</template>
        </el-table-column>
           <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.create_time }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="200px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="!checkPermission(['validation_update'])"
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope)"
            />
            <el-button
              :disabled="!checkPermission(['validation_delete'])"
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
            />
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="contentList.count > 0"
        :total="contentList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑能力验证' : '新增能力验证'"
    >
      <el-form
        ref="Form"
        :model="Content"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="Content.name" placeholder="文档名称" />
        </el-form-item>
         
         

     <el-form-item label="文件类型" prop="filetype">
        <el-radio-group v-model="Content.filetype">
    <el-radio :label="1">征集通知</el-radio>
    <el-radio :label="2">结果通报</el-radio>
  </el-radio-group>
   </el-form-item>
     <el-form-item label="省份" prop="provinces">
   <el-select v-model="Content.provinces" filterable placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.label"
      :label="item.label"
      :value="item.label">
    </el-option>
  </el-select>
    </el-form-item>
        <el-form-item label="文档描述" prop="description">
          <el-input
            type="textarea"
            :rows="4"
            v-model="Content.description"
            placeholder="文档描述"
          />
        </el-form-item>
        <el-form-item label="文档上传" prop="file" v-if="dialogVisible">
          <el-upload
          ref="upload"
          :action="upUrl"
          :on-preview="handlePreview"
          :on-success="handleUpSuccess"
          :on-remove="handleRemove"
          :headers="upHeaders"
          :file-list="fileList"
          :limit="1"
          accept=".doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.zip"
        >
          <el-button size="small" type="primary">上传文件</el-button>
        </el-upload>
        </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 {
  getValidationList,
  createValidation,
  deleteValidation,
  updateValidation,
} from "@/api/validation";
import { genTree } from "@/utils";
import {provinceAndCityData,CodeToText} from "element-china-area-data";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { upUrl, upHeaders } from "@/api/file";
const defaultContent = {
  name: "",
  description: "",
  file:null
};
export default {
  components: { Pagination, Treeselect },
  data() {
    return {
      upHeaders: upHeaders(),
      upUrl: upUrl(),
      fileList:[],
      Content: defaultContent,
      type_:{1:"通知",2:"结果"},
      filetype_:{1:"征集通知",2:"结果通报"},
      options:[
        {value: "市场监管总局",label: "市场监管总局"},
        {value: "北京市", label: "北京市"},
        {value: "上海市",label: "上海市"},
        {value: "天津市", label: "天津市"},
        {value: "重庆市",label: "重庆市"},
        {value: "黑龙江省", label: "黑龙江省"},
        {value: "辽宁省",label: "辽宁省"},
        {value: "吉林省", label: "吉林省"},
        {value: "河北省",label: "河北省"},
        {value: "河南省", label: "河南省"},
        {value: "湖北省",label: "湖北省"},
        {value: "湖南省", label: "湖南省"},
        {value: "山东省",label: "山东省"},
        {value: "山西省", label: "山西省"},
        {value: "陕西省",label: "陕西省"},
        {value: "安徽省", label: "安徽省"},
        {value: "浙江省",label: "浙江省"},
        {value: "江苏省", label: "江苏省"},
        {value: "福建省",label: "福建省"},
        {value: "广东省", label: "广东省"},
        {value: "海南省",label: "海南省"},
        {value: "四川省", label: "四川省"},
        {value: "云南省",label: "云南省"},
        {value: "贵州省", label: "贵州省"},
        {value: "青海省",label: "青海省"},
        {value: "甘肃省", label: "甘肃省"},
        {value: "江西省",label: "江西省"},
        {value: "内蒙古自治区",label: "内蒙古自治区"},
        {value: "宁夏回族自治区", label: "宁夏回族自治区"},
        {value: "新疆维吾尔自治区",label: "新疆维吾尔自治区"},
        {value: "西藏自治区", label: "西藏自治区"},
        {value: "广西壮族自治区", label: "广西壮族自治区"},
        {value: "香港特别行政区", label: "香港特别行政区"},
        {value: "澳门特别行政区", label: "澳门特别行政区"},
         {value: "台湾省", label: "台湾省"},
        ],
      dialogVisible: false,
      listQuery: {
        page: 1,
        page_size: 20,
      },
       contentList: {
        count:0
      },
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {
    filterOrgText(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
  
  },
  methods: {
    filterTag(value, row) {
        return row.type === value;
      },
      filterTag1(value, row) {
        return row.filetype === value;
      },
    handlePreview(file) {
      if ("url" in file) {
        window.open(file.url);
      } else {
        window.open(file.response.data.path);
      }
    },
    handleUpSuccess(res, file, filelist) {
      this.Content.file = res.data.id;
      
    },
    handleRemove(file, filelist){
      this.Content.file = null;
    },
    checkPermission,

    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    getList() {
     
      getValidationList(this.listQuery).then((response) => {
        if (response.data) {
          this.contentList = response.data;
        }
      
      });
    },
   
 handleFilter2() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleAddContent() {
      this.Content = Object.assign({}, defaultContent);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.fileList=[]
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleEdit(scope) {
      this.Content = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      if (this.Content.file) {
        this.fileList = [
          {
            name:this.Content.file_.name,
            url: this.Content.file,
          },
        ];
      }
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteValidation(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) {
            console.log(this.Content)
            updateValidation(this.Content.id, this.Content).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("编辑成功");
              }
            });
          } else {
            createValidation(this.Content).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("新建成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
